Search code examples
itext7

Is it possible to remove selected bookmarks (aka outlines)?


Can existing bookmarks be removed from the outline tree of a document using iText 7? The PdfOutline class has methods to add outlines, but there are none to remove one.

I tried selectively copying outlines to a list, removing all the existing outlines with PdfDocument.getCatalog.remove(PdfName.Outlines), and then repopulating the document's outline with the elements of my list. The new outline came out the way I wanted it, but when I clicked on any of the bookmarks, they took me to incorrect locations within the document.


Solution

  • Using 7.1.12-SNAPSHOT version you can already remove an outline (and all its children recursively) with the public API:

    PdfOutline root = pdfDocument.getOutlines(true);
    // Getting third child (as indices are 0-based)
    PdfOutline toRemove = root.getAllChildren().get(2);
    // Removing outline and all its children recursively (so we are removing a subtree)
    toRemove.removeOutline();