I have data in ArrayList like below :
ArrayList tree = new ArrayList(); 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));
tree structurelike below :
Root -----Node-1 ------------Node-2 ------------------Node-4 ------------Node-3 ------------------Node-5
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:
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
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> outlines = new ArrayList>(); HashMap map = new HashMap(); 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:
HashMap calendar = new HashMap(); calendar.put("Title", "Calendar");
HashMap day = new HashMap(); day.put("Title", "Monday"); ArrayList> days = new ArrayList>(); days.add(day); calendar.put("Kids", days);
Once we're finished, we add the outline tree to the
PdfStamper like this:
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
HashMap hour = new HashMap(); hour.put("Title", "10 AM"); ArrayList> hours = new ArrayList>(); hours.add(hour); day.put("Kids", hours);
And so on...