0

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.

Alexey Subach
  • 11,903
  • 7
  • 34
  • 60
user3969107
  • 27
  • 1
  • 5
  • Hi, right now there is no good option to easily remove a single outline but this is indeed a valid use case that iText should support. You can take a look at `PdfOutline#removeOutline` in the [source code](https://github.com/itext/itext7/blob/d6745af01514a8f36260f737a6d60482e1e950c6/kernel/src/main/java/com/itextpdf/kernel/pdf/PdfOutline.java#L322) and implement something similar yourself – Alexey Subach May 03 '20 at 18:55

1 Answers1

1

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();
Alexey Subach
  • 11,903
  • 7
  • 34
  • 60