Accessibility

Perk up PDF documents with JavaScript

 

Created:
01 November 2004
User Level:
Intermediate, Advanced

Excerpted from “Adobe Acrobat 6.0: The Professional User’s Guide” by Donna Baker and Tom Carson.

In this tutorial, we'll show you how to use dialog boxes within Adobe® Acrobat® to add scripting. In other cases, you'll have to build some scripts. To bring some order to the presentation of how to use JavaScript, we've organized it according to where it's being used—that is, whether you're using it inside a document or outside a document.

You can attach JavaScript to a document in seven different ways:

  • With Page Open and Page Close actions

  • With Document actions

  • With bookmarks

  • With links

  • With form fields

  • At the document level (or top level)

  • From the JavaScripts folder on your hard drive

Let's have a look at these JavaScript options. How and where you access them in Acrobat varies. You'll run through the processes, and then you'll look at some in detail in upcoming projects.

Opening and closing pages

First up, using JavaScript with Page Open or Page Closed actions, the script is executed when the page opens or closes. A logical use of this event would be updating a date on the page. If you would like to try this as we explain how it works, open an Acrobat file—any file will do. The change in interface from Acrobat 5.0 to Acrobat 6.0 changed how these are accessed. A page action involves a page. Pages are under the Pages tab.

To add a Page Open action, follow these steps:

  1. On the Pages tab, right-click the page thumbnail you want to apply the action to. Select Page Properties to open the dialog box shown below.

    acr6kbjavascrpt_1_int

    Set page properties from a page thumbnail in the Pages tab, and choose a page action from the Page Properties dialog box.

    Note: The first option, Page Open and Page Close events, has a sample script to give you a sense of how the process works.

  2. Click the Trigger drop-down list and choose an option. You can choose either Page Open or Page Close (Page Open is selected in this example).

  3. Select Run a JavaScript from the Actions drop-down list. Click Add.

  4. The JavaScript Editor dialog box opens; write the script, such as:

    app.alert ("Welcome to my document.");
  5. Click OK to close the JavaScript Editor window.

  6. Click OK to close the dialog box and return to the Page Properties dialog box.

  7. Under Actions a Page Open Run JavaScript now appears.

  8. Click Close to close the dialog box. Save the file.

To test the script, reopen the file. When it opens, you should see the alert message, as shown below.

acr6kbjavascrpt_2_int

The document now displays this alert when it's opened.

Now let's discuss attaching JavaScript to document actions.

Document actions

Document actions relate to different states of a document, and include the following:

  • Document Will Close (when a document closes)

  • Document Will Save (before a document is saved)

  • Document Did Save (after a document is saved)

  • Document Will Print (before a document is printed)

  • Document Did Print (after a document is printed)

The states that include "Did" in their names will run the script immediately after the function—either save or print—is completed.

Add document actions by following these steps:

  1. Select Advanced > JavaScript > Set Document Actions. The dialog box below opens.

    acr6kbjavascrpt_3_int

    Choose a document action from the list in this dialog box.

    Note: You may have noticed that there is no Delete button on this dialog box. You can delete an action, of course. Select the action from the list at the top of the dialog box and click Edit to open the JavaScript Editor dialog box. Delete the code and click OK. The code is now removed.

  2. Select the JavaScript action from the list, and click Edit.

  3. When the JavaScript Editor dialog box opens, add your script. Click OK to close the dialog box and return to the Document Actions dialog box.

  4. The script appears in the bottom window, and a green dot appears to the left of the action's line in the listing, as shown below. Click OK to close the dialog box.

    acr6kbjavascrpt_4_int

    In this example, scripts are attached to the first two options, as indicated by the dots beside their names.

You can attach a number of scripts to the same document at this level, and you can view them all in this dialog box. Simply select the action from the list at the top of the box, and the corresponding script will be displayed in the bottom window.

If you click Edit All, the JavaScript Editor dialog box will open, displaying all the scripts attached to the document.

Excerpted from “Adobe Acrobat 6.0: The Professional User’s Guide” by Donna Baker and Tom Carson.Copyright ©2004. Published by Apress. Used with the permission of Apress. To buy this book, visit www.apress.com.