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

Related Products at PDF Store

Nitro PDF Professional OCR

Nitro PDF Professional OCR gives you all the features of Nitro Pro, plus the added power of Optical ... View full product details
Download free demo

Nitro PDF Professional

Nitro PDF Professional, your PDF creation and editing product. Priced at $99.99, Nitro PDF Pro is th... View full product details
Download free demo

XpdfViewer

This ActiveX control (OCX) provides a PDF file viewer component, enabling developers to add PDF view... View full product details
Download free demo

PDF In-Depth Free Product Trials Ubiquitous PDF

Nitro PDF Professional

the perfect PDF product for business and enterprise, combining an extremely competitive price with a...

Download free demo

XpdfViewer

This ActiveX control (OCX) provides a PDF file viewer component, enabling developers to add PDF viewing...

Download free demo

Ubiquitous PDF: Planning for unexpected cash

It's the end of the financial year and some lucky souls are expecting a tax return. Whether or not the dollars are stacking up for you, it's worth keeping in mind this new PDF tool from Squawkfox.

July 29, 2010
Search Planet PDF
more searching options...







Download PDF Creator

Download The Best of Planet PDF volume 2
Planet PDF Newsletter
Most Popluar Articles
Features

Collating PDFs using JavaScript

Despite the numerous benefits, there can be potential issues with the conversion of paper documents into electronic archives. When scanning paper pages into PDF, it's possible to end up with the odd- and even-numbered pages in separate PDF files. It can be very time-consuming to collate them manually, but there is an easier way. Sean Stewart explains.

Featured Product

BCL easyPDF SDK

BCL easyPDF SDK is a set of PDF Programming Libraries designed specifically to help Software Developers / Programmers build and deploy enterprise class PDF applications for corporate wide PDF...

Platinum Sponsor
Create & Edit PDF - Nitro PDF Software

ARTS PDF

Silver Sponsors

PDF-Tools QuickPDF: The Unrivaled PDF Developer Toolkit