|
||||||
TUTORIALSCreator Tutorial:Adding a table of contentsAs easy as 1, 2, 3...
|
||||||
| Basic Item | These items are only meant to add some visual separation in the outline tree as no specific task is executed when there are selected. |
| Destination Item | These items contain page index information that needs to be shown and, optionally, any zoom effect to be executed when showing it. |
| Action Item | These items contain the action to execute upon selection (usually a GotoAction that contains the same information as the Destination Item). |
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();v
// 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");
item0.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_y / 2), 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");
item2.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();
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.
| © 2000-2007 Crionics Inc. |