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
Step 1 - Open the "Edit PDF" Tool
In this example, we will create a link to the 'Summary' bookmark, around the existing text: 'Link to "Summary" bookmark'.
Return to the "Tools" panel by selecting it on the main Acrobat toolbar.
Find and click on the "Edit PDF" tool icon.
Step 2 - Access Link Editing Tools
Press the "Link" icon on the toolbar to access a list of options.
Select the "Add/Edit Web or Document Link" option to open the 'Create Link' tool.
Step 3 - Create a Link
Use the cursor to create a link. Draw a box by clicking and dragging around the desired text.
Step 4 - Configure the Link
In the dialog box that opens, use the 'Link Appearance' section to modify the link's appearance. Then select the "Custom Link" option in the 'Link Action' section before pressing "Next".
In the "Link Properties" dialog, link appearance options can also be modified. Open the "Actions" tab.
Step 5 - Add the Link Action
In the "Add an Action" section in this dialog, use the drop-down list to select the "Run a JavaScript" action.
Now press the "Add..." button.
Step 6 - Create JavaScript Code
This opens the "JavaScript Editor" dialog. Use it to add the necessary line of code used to execute the bookmark action. In this example, 'ExecuteBookmark("Summary");' is entered. Press "OK" once done.
Step 7 - Close "Link Properties"
The new action is now listed under "Actions". Press "OK" to proceed.
Step 8 - Close the "Edit PDF" Tool
Press "Close" to exit the "Edit PDF" tool.
Step 9 - Test the New Link
Test the new link by clicking on it.
The link will direct the user to the appropriate page location according to the relevant bookmark and JavaScripts created earlier.
You can find a list of other step-by-step bookmarking tutorials here: http://www.evermap.com/abm_bm_summary.asp#tutorials.