How to create hierarchical bookmarks?

Tags: BookmarksPdfOutlineiText 5

I have data in ArrayList like below :

ArrayList<DTONodeDetail> tree = new ArrayList<DTONodeDetail>();
tree.add(getDTO(1,"Root",0));
tree.add(getDTO(239,"Node-1",1));
tree.add(getDTO(242,"Node-2",239));
tree.add(getDTO(243,"Node-3",239));
tree.add(getDTO(244,"Node-4",242));
tree.add(getDTO(245,"Node-5",243));
I am able to display above data in tree structure like below :

Root
-----Node-1
------------Node-2
------------------Node-4
------------Node-3
------------------Node-5
How can I create a hierarchical bookmark structure (as shown above) in PDF using iText?

Posted on StackOverflow on Feb 26, 2015 by Butani Vijay

In PDF terminology, bookmarks are referred to as outlines. Please take a look at the CreateOutlineTree example to find out how to create an outline tree as shown in this PDF:

Outline tree shown in the bookmarks panel
Outline tree shown in the bookmarks panel

We start with the root of the tree:

PdfOutline root = writer.getRootOutline();

Then we add a branch:

PdfOutline movieBookmark = new PdfOutline(root, 
    new PdfDestination(
        PdfDestination.FITH, writer.getVerticalPosition(true)),
        title, true);

To this branch, we add a leaf:

PdfOutline link = new PdfOutline(movieBookmark,
    new PdfAction(String.format(RESOURCE, movie.getImdb())),
    "link to IMDB");

And so on.

The key is to use PdfOutline and to pass the parent outline as a parameter when constructing a child outline.

Can I do this in an existing pdf? I mean without creating new PDF, I want to add bookmarks to an existing pdf.

There's also an example called BookmarkedTimeTable where we create the outline tree in a completely different way:

ArrayList<HashMap<String, Object>> outlines =
    new ArrayList<HashMap<String, Object>>();
HashMap<String, Object> map = new HashMap<String, Object>();
outlines.add(map);

In this case, map is the root object to which we can add branches and leaves. To create a hierarchy, you just need to add kids like this:

First level:

HashMap<String, Object> calendar = new HashMap<String, Object>();
calendar.put("Title", "Calendar");

Second level:

HashMap<String, Object> day = new HashMap<String, Object>();
day.put("Title", "Monday");
ArrayList<HashMap<String, Object>> days =
    new ArrayList<HashMap<String, Object>>();
days.add(day);
calendar.put("Kids", days);

Once we're finished, we add the outline tree to the PdfStamper like this:

stamper.setOutlines(outlines);

Note that PdfStamper is the class we need when we manipulate an existing PDF (as opposed to PdfWriter which is to be used when we create a PDF from scratch).

I am unable to add third level using this. I mean suppose in your example I want to add child to the date, and have a branch like Calendar -> 2014-03-02 -> Monday

Third level:

HashMap<String, Object> hour = new HashMap<String, Object>();
hour.put("Title", "10 AM");
ArrayList<HashMap<String, Object>> hours = new ArrayList<HashMap<String, Object>>();
hours.add(hour);
day.put("Kids", hours);

And so on...