Modifying the User Interface
Adding Navigation to PDF Documents
10
The following code creates a bookmark that displays a greeting when clicked. Note that the
omission of the
nIndex
value means that it is placed at position 0 in the
children
array:
myRoot.createChild("myBookmark", "app.alert(‘Hello!’);");
The following code adds a bookmark called
grandChild
as a child of
myBookmark
, :
var current = myRoot.children[0];
current.createChild("grandChild");
Suppose that you would like to move
grandChild
so that it becomes a child of the root.
Invoke the root bookmark’s
insertChild
method, and provide a reference to
grandChild
as a parameter:
var grandChild = myRoot.children[0].children[0];
myRoot.insertChild(grandChild, 1);
Managing Bookmarks
You can use Acrobat JavaScript to change the
name
,
color
, and
style
properties of a
bookmark. Note that the
style
property is an integer:
0
means normal,
1
means italic,
2
means bold, and
3
means bold-italic. The code below changes the name to
New Name
, the
color to red, and the font style to bold:
var myRoot = this.bookmarkRoot;
var myChild = myRoot.children[0];
myChild.name = "New Name";
myChild.color = color.red;
myChild.style = 2;
In addition to adding new or existing bookmarks as you learned in
Creating Bookmarks,
you may also delete a bookmark and its children by invoking its
remove
method. The
following line of code removes all bookmarks from the document:
this.bookmarkRoot.remove();
Creating a Bookmark Hierarchy
Because of the tree structure associated with bookmarks, it is possible to construct a
hierarchy of bookmarks; a child of a bookmark represents a subsection of the section
represented by that bookmark. To create a hierarchy, first add bookmarks to the root, then
to the children of the root, and recursively to their children.
The following code creates bookmarks
A
,
B
,
C
. Each section has 3 children. Child
A
has
children
A0
,
A1
, and
A2
. Child
B
has children
B0
,
B1
, and
B2
. Child
C
has children
C0
,
C1
,
and
C2
:
Acrobat JavaScript Scripting Guide
173