As easy as 1, 2, 3...
In this tutorial we will see how jPDF can be used to update
an existing table of contents. (The creation of a
new bookmark, though very similar, is covered in a different tutorial.)
First, we need to open a document and get hold of its table
of contents. This can be achieved easily by using the following code:
// Open the document:
PdfDocument doc = new PdfDocument("my10PagesDocumentWithAnExistingOutline.pdf");
CreatorModule creator = doc.getCreatorModule();
// Get hold of the current document outline:
Outline outline = doc.getCreatorModule().getOutline();
Next, we want to add some new entries at the end of the tree to show page 5 of the document.
// Create a destination object to show the complete page:
LocalPageDestination destination=LocalPageDestination.createInstance(doc);
destination.setPageIndex(5);
destination.setZoom(new ZoomPage());
// Create a new item:
OutlineItemDestination item = OutlineItemDestination.createInstance(doc);
// Set the title of the item and its destination:
item.setTitle("Page 5 of the Document " );
item.setDestination(destination);
// Now add this new item at the end of the tree:
AbstractOutlineItem rootItem=(AbstractOutlineItem) outline.getRootItem();
rootItem.getLast().addNext(item);