Adding Links to Bookmark Actions Using JavaScript in Adobe Acrobat
- Introduction
- This tutorial demonstrates how to have PDF links execute an action that corresponds to a specific bookmark’s action. We are going to achieve this by creating custom “Run a JavaScript” actions in Adobe Acrobat. Using this method, we create links that can execute a bookmark action just by referencing the bookmark’s name.
- The first step involves writing two document-level JavaScript functions: ‘ExecuteBookmark()’ and ‘FindBookmarks()’. These document-level scripts are useful as they store all the code in a single place. This makes it easily accessible from any action within the document. All the code can be edited in one place to simplify code maintenance. If changes to the code need to be made, it can be done at "document-level" instead of configuring individual links/actions separately. Now it is possible to assign a "Run a JavaScript" action to any link to call ExecuteBookmark() function in order to "execute" a specific bookmark's action.
- Input Document Description
- To demonstrate this linking exercise, we use a PDF document with multiple bookmarks that perform an action when opened. This could be anything such as: opening a web page, or opening an external file or displaying a page view etc. Using this method, we create links that can execute an action just by using a bookmark’s name.
- Click here to download a sample PDF document that is used in the tutorial.
- Prerequisites
- You need a full copy or a free trial version of Adobe® Acrobat® installed on your computer in order to use this tutorial.
Creating Document-Level JavaScripts
- Step 1 - Open the 'JavaScript' Tool
- With the suitable document open in Adobe® Acrobat®, open the "Tools" panel located on the main Acrobat toolbar.
- Scroll down and click on the "JavaScript" tool icon.
- Step 2 - Open 'Document JavaScripts'
- Click on the "Document JavaScripts" icon on the toolbar to open the "Document JavaScripts" dialog used to create JavaScripts.
- Step 3 - Add a Script Name
- This dialog lists all available document-level JavaScripts. To add a new one, type a desired script name in the entry box provided and press the "Add..." button to open the "JavaScript Editor". In this example, we begin by creating the "ExecuteBookmark" function.
- Step 4 - Create the ExecuteBookmark() JavaScript
- Create and edit the JavaScript within this dialog. Once done, press "OK".
- Here is the code for the ExecuteBookmark() function. This function will be used in the link actions to "execute" a bookmark's action by refering to it "by name". For example, in order to execute a bookmark with "Introduction" title use ExecuteBookmark("Introduction");. Note that if multiple bookmarks with the same title are present, the all bookmarks will be executed one after another (except "child" bookmarks).
-
function ExecuteBookmark(bookmarkName)
{
/* Execute a bookmark with a specific title and expand it */
var root = this.bookmarkRoot;
FindBookmark(bookmarkName, root);
} - Step 5 - Add More JavaScripts
- The script has now been added to the list on the left of the dialog. When selected, it is displayed in the preview window below.
- Next, we will add the "FindBookmark" script. Enter the new script title and press "Add...".
- Step 6 - Configure the FindBookmark() JavaScript
- Create the desired script. It may be necessary to re-size the window and use the scroll bar to view all of the script's content clearly. Press "OK" once done.
- Here is the code for the FindBookmark() function. This function searches all bookmarks under a specific bookmark "parent" for the bookmark with the given title. If the bookmark is located, its associated action is executed:
-
function FindBookmark(bookmarkName, Bm)
{
var result = bookmarkName.localeCompare(Bm.name);
if (result == 0)
{
Bm.open = true;
Bm.execute();
return;
}
else
{
Bm.open = false;
}
// process children
if (Bm.children != null)
{
for (var i = 0; i < Bm.children.length; i++)
{
FindBookmark(bookmarkName, Bm.children[i]);
}
}
}
- Step 7 - Modify/Confirm Scripts
- To make changes to an existing script or to view it in detail, simply select it in the list and press "Edit...".
- Press "Close" once all required scripts have been added.
- Now all the preparation work is completed. We have created a document-level JavaScript that can be called by the link actions to "execute" a desired bookmark.
Adding Links to Bookmarks