Creating PDF table of contents
What are PDF Outlines?
Outlines, also known as bookmarks or tables of content, are visual elements that allow users to navigate from one part of a document to another.
Outlines are presented to users as a tree-structured hierarchy of elements, each defining which page (or part of a page) to jump to - while also displaying that they have been selected.

There are currently three types of items defined in the PDF Spec:
- BasicItem
These items are only meant to add some visual separation in the outline tree as no specific task is executed when there are selected.
- DestinationItem
These items contain page index information that needs to be shown and, optionally, any zoom effect to be executed when showing it.
- ActionItem
These items contain the action to execute upon selection (usually a GotoAction that contains the same information as the Destination Item).
Programming outlines
In the following example, we will construct an outline similar to the image above.
// Create a new PDF document:
PdfDocument doc = new PdfDocument();
CreatorModule creatorModule = doc.getCreatorModule();
// Create a new page:
Page page = Page.createInstance(doc);
// Recover the page dimensions:
PageFormat pf = page.getPageFormat();
int page_y = (int) pf.getHeight();
// Recover the object that allows the drawing of text on the page:
JPdfGraphics2D g2d = page.getGraphics();
/******************************************************
/ Add the First chapter close to the top of the document.
/******************************************************
// Some text element in the page:
g2d.drawString("Chapter 1 Title", 15, 30 + g2d.getFontMetrics().getHeight());
// Create the destination and zoom data:
LocalPageDestination destination=LocalPageDestination.createInstance(doc);
destination.setPageIndex(1);
destination.setZoom(new ZoomPosition(new Integer(15), new Integer(30), null));
// Create the item:
OutlineItemDestination item0 = OutlineItemDestination.createInstance(doc);
// Set the title of the item and the destination upon activation:
item0.setTitle("Outline Chapter 1"); <britem0.setDestination(destination);
//******************************************************
/ Add the subchapter in the middle of the document.
/******************************************************
// Some text element in the page:
g2d.drawString("Sub Chapter 1.1 Title", 15, page_y 2 +
g2d.getFontMetrics().getHeight());
// Create the destination and zoom data:
destination=LocalPageDestination.createInstance(doc);
destination.setPageIndex(1);
destination.setZoom(new ZoomPosition(new Integer(15), new Integer(page_y2), null));
// Create the item:
OutlineItemDestination item1 = OutlineItemDestination.createInstance(doc);
// Set the title of the item and the destination upon activation:
item1.setTitle("Outline SubChapter 1.1");
item1.setDestination(destination);
// Register it as a child of the first item:
item1.addBelow(item0);
/******************************************************
/ Add the Second chapter close to the bottom of the document.
/******************************************************
// Some text element in the page:
g2d.drawString("Chapter 2 Title", 15, page_y - 30 +
g2d.getFontMetrics().getHeight());
//Create the destination and zoom data:
destination=LocalPageDestination.createInstance(doc);
destination.setPageIndex(1);
destination.setZoom(new ZoomPosition(new Integer(15), new Integer(page_y - 30), null));
// Create the item:
OutlineItemDestination item2 = OutlineItemDestination.createInstance(doc);
// Set the title of the item and the destination upon activation:
item2.setTitle("Outline Chapter 2"); <britem2.setDestination(destination);
// Register it as a next item of the first item:
item2.addAfter(items0);
//Now register the outline in the document:
Outline outline = creatorModule.createOutline();
outline.addItem(items0);
// Important: DO NOT forget to dispose():
g2d.dispose();
Save the document and run the application
Final step: Save the document to a file and close it in order to release memory.
doc.saveAs("SimpleOutlineDemo.pdf");
doc.close();
The full source code of this tutorial is part of the distribution.
Please click on this link to display the generated PDF.
Copyright © Crionics Inc. 2012 All rights reserved. Terms of Use | Privacy Policy
