Skip to main content
Skip table of contents

How to read bookmark titles?

I am working with a single PDF containing multiple documents. Each document has a bookmark. I need to read the bookmark names for a reconciliation application that I am building. The code below is not working for me.


PdfReader reader = new PdfReader("C:\\Work\\Input.pdf");
ListHashMapString,Object>> bookmarks = SimpleBookmark.getBookmark(reader);
for(int i = 0; i  bookmarks.size(); i++){
    HashMapString, Object> bm = bookmarks.get(i);
    String title = ((String)bm.get("Title"));
}

I am trying to place the bookmark name in the title string.

Posted on StackOverflow on Feb 20, 2015 by jcalder

You are not taking into account that bookmarks are stored in a tree structure with branches and leaves (in the PDF specification, it's called the outline tree).

Your code works for the top-level, but if you want to see all the titles, you need to use a recursive method that also looks at the "Kids".

Take a look at the FetchBookmarkTitles example:

public void manipulatePdf() throws IOException {
        PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC));

        PdfOutline outlines = pdfDoc.getOutlines(false);
        List<PdfOutline> bookmarks = outlines.getAllChildren().get(0).getAllChildren();
        StringBuffer stringBuffer = new StringBuffer();
        for (PdfOutline bookmark : bookmarks) {
            showTitle(bookmark, stringBuffer);
        }
        pdfDoc.close();

        System.out.println(stringBuffer.toString());

        Assert.assertArrayEquals(RESULT.getBytes(), stringBuffer.toString().getBytes());
    }

    public void showTitle(PdfOutline outline, StringBuffer stringBuffer) {
        System.out.println(outline.getTitle());
        stringBuffer.append(outline.getTitle() + "\n");
        List<PdfOutline> kids = outline.getAllChildren();
        if (kids != null) {
            for (PdfOutline kid : kids) {
                showTitle(kid, stringBuffer);
            }
        }
    }

The showTitle() method is recursive. It calls itself if an examined bookmark entry has kids. With this code snippet, you can walk through all the branches and leaves of the outline tree.

Click this link if you want to see how to answer this question in iText 5.

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.