Entering document scripts

Learn about Document Level Scripts, also called Document Scripts or Document JavaScripts.

By July 20, 2007

 

Scope: All Acrobat versions
Skill Level: Beginner
Prerequisites: Familiarity with JavaScript

Document Level Scripts, also called Document Scripts or Document JavaScripts, reside at the top of the PDF document scripting hierarchy. These scripts are loaded and run when a PDF is opened in Acrobat. For all practical purposes they can be considered Document Open Scripts. Any functions or variables defined in a document script are visible to every other script in the PDF, so they can also be considered the document’s global area. If a document discloses itself to Acrobat, then scripts outside the document can also see and use variable and functions defined here. Disclosure means the document’s disclosed property is set to true.

The Document Script is where initialization code is placed, such as setting the disclosed property. However, at the time the document scripts are run the PDF may not be fully loaded. This can cause problems with code statements that access fields and annotations, or display user interface items like popup menus and dialogs. If you have problems with initialization, move the troublesome parts of the code to the Page Open script for the first page. The Page Open Script is loaded and run after initialization is complete.

Accessing Document Scripts

To edit an existing script or create a new script use the menu item

Advanced>JavaScript>Document JavaScripts …

This menu item displays the Document Scripts Dialog (Figure 1). A PDF may contain several Document Scripts. These scripts are displayed in the dialog in the same order Acrobat loads and runs them. Be careful with this ordering if you create scripts that depend on variables defined in another script. When new scripts are added using this dialog Acrobat tries to place them in alphabetical order, so naming usually controls order. This system breaks down when existing scripts are not already in alphabetical order.

Closing this dialog will cause Acrobat to reload and run the Document Scripts, even if the scripts have not been touched. To be clear, this means that simply opening and then closing this dialog, without performing any other action, forces all the script to be re-run. Depending on what the Document Scripts actually do, this action can be helpful, harmful, or just annoying. If you would prefer to edit these scripts without Acrobat re-running them after every change, then turn off JavaScript in the Preferences Panel.

Figure 1 – Editing and Creating Document Scripts

  • To Edit a script: select it from the list of scripts and
    press the Edit… button.

  • To Create a new script: Enter a new Script Name and

    press the Add… button.


Did you know?

  • You can ask a question and get an answer from one of our experts.
  • You can search our database of over 800 tutorials by product and/or topic.
  • You can leave a comment below for the author of this tutorial.

Related topics:

JavaScript

Top Searches:

Edit PDF, create PDF, Action Wizard

0 comments

Comments for this tutorial are now closed.

Comments for this tutorial are now closed.