Dev Null Fin
Dev Null Fin

Reputation: 183

How to get all bookmarks in PDF file using PDFBox in Java

I am newbie in Apache PDFbox. I want to extract all bookmarks in PDF file using PDFBox library in Java. Any idea how to extract them?

Upvotes: 3

Views: 6152

Answers (1)

Tilman Hausherr
Tilman Hausherr

Reputation: 18861

From the PrintBookmarks example in the source code download

PDDocument document = PDDocument.load(new File("..."));
PDDocumentOutline outline =  document.getDocumentCatalog().getDocumentOutline();
printBookmark(outline, "");
document.close();

(...)

public void printBookmark(PDOutlineNode bookmark, String indentation) throws IOException
{
    PDOutlineItem current = bookmark.getFirstChild();
    while (current != null)
    {
        System.out.println(indentation + current.getTitle());
        printBookmark(current, indentation + "    ");
        current = current.getNextSibling();
    }
}

Upvotes: 9

Related Questions