Crionics LOGO

TUTORIALS

 

 Add a bookmark to an existing document

As easy as 1, 2, 3...

 

Introduction

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

 

Adding the new bookmarks

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

 

Finally, save

All the changes we made were kept in memory. Now it's time to save the updated document to disk.

// Save and close the document:
doc.save();
doc.close();


© 2000-2007 Crionics Inc. Report issues with this page