Example Acrobat JavaScripts

Editor’s Note: This article is part of the Introduction to Acrobat JavaScript Learning Center

In previous articles in this Learning Center we have discussed what exactly Acrobat JavaScript is, where you can use it and how to debug it. In this article we’re going to jump right into the deep-end with some useful examples of using Acrobat JavaScript. Check back soon for more tips and examples.

1. Check Viewer Version and Type

There are certain situations where you may need to check which version of Acrobat/Reader a user is running to see if you can use a particular object/method/property that may only be available in recent versions of Acrobat.

// full version, or just reader
var viewerType = app.viewerVariation;

// get version number
var viewerVersion = app.viewerVersion;

if(viewerVersion < 6)
// cannot use printParams object
// v6 or above, can use printParams object
// to define printer to print to
var pp = this.getPrintParams();
pp.printerName = 'Auto Canon LBP-800';

In the above example we are looking to define the printer to print the PDF to. This became possible in Acrobat/Reader with v6.0, so if we tried to use the printParams object in earlier versions an error would occur. Thus, we use the app.viewerVersion property to first check which version the user is running, and if they are running v6.0 or above, then use the printParams object to define the printer, otherwise to just make the print dialog appear.

See the example PDF file here

2. Insert Today’s Date into Form Field

There is a method in Acrobat JavaScript that makes formatting dates quick and easy. The method is util.printd(), and there an example of this below where today’s date is inserted into a form field named ‘todaysDate’.

// get new Date object
var d = new Date();

// format to mm/dd/yyyy
var sDate = util.printd('mm/dd/yyyy', d);

// insert date into form field
this.getField('todaysDate').value = sDate;

See the example PDF file here

3. Run JavaScript on Document Open

There are many situations where running a JavaScript when the document is opened proves useful. The current date could be set in a form field, each of the form fields could be reset (in the case that someone was previously using the form and saved values in the fields).

// variable to store whether document has been opened already or not
var bAlreadyOpened;

function docOpened()

if(bAlreadyOpened != 'true')
// document has just been opened
var d = new Date();
var sDate = util.printd('mm/dd/yyyy', d);
this.getField('todaysDate').value = sDate;

// now set bAlreadyOpened to true so it doesn't
// run again
bAlreadyOpened = 'true';
// document has already been opened

// call the docOpened() function

To add a document level JavaScript in Acrobat, go to ‘Advanced > JavaScript > Document JavaScripts’, enter a name for the script and click ‘Add’.

The PDF that accompanies example #2 shows a JavaScript running on the document open action. See the example PDF file here

4. Check Dropdown List Value On Selection

In many instances where you provide a user with a dropdown list allowing them to make a selection, you need to perform some other operations based on their selection. By placing JavaScript code under the ‘Validate’ section of the dropdown list, we ensure that the value will be checked as soon as it has been committed.

src=’http://www.planetpdf.com/images/1-CD-LC_JS_-_Example_JS.jpg&#8217; width=’439′ height=’329′
alt=’Form field’s validate tab’>

Figure 1. Form field’s validate tab

Additionally, if we want to ensure that any processing/calculations are triggered as soon as the user makes their selection in the dropdown list (and not when the tab or click the mouse outside of the form field) there is an option (new in v6 of Acrobat) that allows you to specify this, named ‘Commit selected value immediately’ in the fields ‘Options’ tab. This can be seen below.

src=’http://www.planetpdf.com/images/2-CD-LC_JS_-_Example_JS.jpg&#8217; width=’329′ height=’388′
alt=’Combo Box Properties Dialog Box’>

Figure 2. Commit value immediately option

See the example PDF file here

Associated Files

  • Example 1. Check the Viewer Version and Type — Download PDF
  • Example 2. Insert Today’s Date into Form Field — Download PDF
  • Example 3. Run JavaScript on Document Open — Download PDF
  • Example 4. Check Dropdown List Value On Selection — Download PDF

More JavaScript Tips

You can find many more JavaScript articles and tips at Planet PDF. Click here to locate them.

More Info

You May Also Like

About the Author: Chris Dahl

Leave a Reply