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.
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:
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.

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.
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).
Select Run a JavaScript from the Actions drop-down list. Click Add.
The JavaScript Editor dialog box opens; write the script, such as:
app.alert ("Welcome to my document.");Click OK to close the JavaScript Editor window.
Click OK to close the dialog box and return to the Page Properties dialog box.
Under Actions a Page Open Run JavaScript now appears.
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.

The document now displays this alert when it's opened.
Now let's discuss attaching JavaScript to 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:
Select Advanced > JavaScript > Set Document Actions. The dialog box below opens.

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.
Select the JavaScript action from the list, and click Edit.
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.
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.

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.