How to get all bookmarks in PDF file using PDFBox

2019-07-11 04:13发布

问题:

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?

回答1:

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();
    }
}


标签: java pdf pdfbox