Learning Centers

Example Acrobat JavaScripts

November 04, 2004

Advertisement
Advertisement
 

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
	this.print();
}
else
{
  // v6 or above, can use printParams object
  // to define printer to print to
  var pp = this.getPrintParams();
  pp.printerName = "Auto Canon LBP-800";
  this.print(pp);
}

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";
  }
  else
  {
    // document has already been opened
  }
}

// call the docOpened() function
docOpened();

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.

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.

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

PDF In-Depth Free Product Trials Ubiquitous PDF

Debenu Quick PDF Library

Get products to market faster with this amazing PDF developer SDK. Over 900 functions and an equally...

Download free demo

Two Passwords Are Better Than One: The Low-Down On PDF Security

For people who don't spend their time looking at PDF files in text editors*, PDF security is a sometimes misunderstood beast.

For example, those document restrictions that PDF files sometimes have -- no Printing, Content Copying, Page Extraction, etc -- are essentially useless unless the PDF also has a User Password.

January 09, 2014
Platinum Sponsor



Search Planet PDF
more searching options...
Planet PDF Newsletter
Most Popular Articles
Featured Product

Debenu PDF Aerialist 11

The ultimate plug-in for Adobe Acrobat. Advanced splitting, merging, stamping, bookmarking, and link control. Take Acrobat to the next level.

Features

Adding a PDF Stamp Comment

OK, so you want to stamp your document. Maybe you need to give reviewers some advice about the document's status or sensitivity. This tip from author Ted Padova demonstrates how to add stamps with the Stamp Tool along with related comments.