Crionics LOGO

TUTORIALS

 

 Using destinations as links to a given PDF page

As easy as 1, 2, 3...

 

In this tutorial, we will see how destinations can be used to create URL links to a specific document page. Destinations may seem like hidden features. Indeed, they are used in many PDF interactive features. Typically, table of contents (aka outlines) are composed of a hierarchy of destinations. Buttons and actions may also link to destinations.

This tutorial shows you how to create a named destination you can use from a URL, such as:

<a href="http://www.mydomain.com/myPDF.pdf#nameddest=Chap2">Link to Chap2</a>

When triggered, the link loads the PDF document and points directly to Chapter 2.


For reference, you can see destinations from Acrobat by using the following menu item:
View->Navigation Panels->Destinations.

note: this menu item is not visible from Acrobat Reader.

Creating destinations

A destination is typically a pointer to a page along with some rendering attributes, such an a (x,y) location and a zoom factor.

There are two kinds of destinations:
* LocalPageDestinations that refer to elements in the same document.
* RemotePageDestinations that refer to elements in a different document.

PDF documents can indeed reference other pages and elements that are part of another document; they use this last type of destination.

The code sample below shows how to create a destination pointing to page 2.

LocalPageDestination dest = LocalPageDestination.createInstance(doc);
dest.setPageIndex(2);

Attaching destinations to documents

As we said in our introduction, destinations can be used in different forms: tables of content, buttons, indexes, etc... In the context of this tutorial we are trying to create what is called a "named destination."

A named destination associates a given string with a destination.

In other words, say we want to attach the above destination dest and use it as a named destination under the name "GoToPageNumber 2". We would use the following code fragment:

CreatorModule creatorModule = doc.getCreatorModule();
creatorModule.setNamedDestination("GoToPageNumber 2", dest);

Quite simple, isn't it?

The creator module actually offers two other methods that won't be detailed in the tutorial. Their use should be straightforward to the average developer.

Putting everything together

The code sample below creates a three-page document with destinations on each page. You can test the resulting PDF by clicking on the links below:

Link to Chap1
Link to Chap2
Link to Chap3

Please note that this is only a code fragment; the complete source code is part of the jPDF distribution.

// Create a document with 3 pages:
doc = new PdfDocument();

Page page1 = Page.createInstance(doc);
drawOnPage(page1, "Check destination from Acrobat by using the menu item View->Navigation Panels->Destinations");

Page page2 = Page.createInstance(doc);
drawOnPage(page2, "Destinations are triggered by using a URL of the form http://www.mydomain.com/myDoc.pdf#nameddest=Chap2");

Page page3 = Page.createInstance(doc);
drawOnPage(page3, "Last test to demonstrate how you can position the destination using a position+zoom factor");

// Get the creator module and generate the destinations:
CreatorModule creatorModule = doc.getCreatorModule();

LocalPageDestination dest1 = LocalPageDestination.createInstance(doc);
dest1.setPageIndex(1);
creatorModule.setNamedDestination("Chap1", dest1);

LocalPageDestination dest2 = LocalPageDestination.createInstance(doc);
dest2.setPageIndex(2);
creatorModule.setNamedDestination("Chap2", dest2);

LocalPageDestination dest3 = LocalPageDestination.createInstance(doc);
dest3.setPageIndex(3);
ZoomPosition zoom = new ZoomPosition(new Integer(15), new Integer(30), new Float(3));
dest3.setZoom(zoom);
creatorModule.setNamedDestination("Chap3", dest3);

// Save the document:
doc.saveAs("MySampleDestinations.pdf");
doc.close();


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