10
Modifying the User Interface
Adding Navigation to PDF Documents
Creating Links
If a PDF document contains text beginning with
http://
such as
http://myURL.com,
you
may convert all such instances to links with URL actions by invoking the
doc
object’s
addWeblinks
method. The method returns an integer representing the number of text
instances converted, as shown in the code below:
var numberOfLinks = this.addWeblinks();
console.println("Converted " + numberOfLinks + " links.");
To add a single link to a PDF document, first invoke the
doc
object’s
addLink
method,
and then customize the returned
link
object’s properties. The
addLink
method requires
two parameters: the page number and the coordinates, in rotated user space, of the
bounding rectangle. In the following example, navigational links are added to the lower left
and right corners of each page in the document. The left link opens the previous page, and
the right link opens the next page:
var linkWidth = 36, linkHeight = 18;
for (var i = 0; i < this.numPages; i++)
{
// Create the coordinates for the left link:
var lRect = [0, linkHeight, linkWidth, 0];
// Create the coordinates for the right link:
var cropBox = this.getPageBox("Crop", i);
var offset = cropBox[2] - cropBox[0] - linkWidth;
var rRect = [offset, linkHeight, linkWidth + offset, 0];
// Create the Link objects:
var leftLink = this.addLink(i, lRect);
var rightLink = this.addLink(i, rRect);
// Calculate
var nextPage
var prevPage
if (prevPage
the previous and next page numbers:
= (i + 1) % this.numPages;
= i - 1;
< 0) prevPage = this.numPages - 1;
// Set the link actions to go to the pages:
leftLink.setAction("this.pageNum = " + prevPage);
rightLink.setAction("this.pageNum = " + nextPage);
// Customize the link appearance:
leftLink.borderColor = color.red;
leftLink.borderWidth = 1;
rightLink.borderColor = color.red;
rightLink.borderWidth = 1;
}
176
Acrobat JavaScript Scripting Guide