Developing with Acrobat Javascript

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

What is Acrobat Javascript?

width=’152′ height=’150′ alt=’AcroJS’ align=’right’>
Adobe have integrated a fully fledged scripting language inside Acrobat – Javascript (ECMA Script).

Javascript which has been known in the HTML world for a long time. Javascript which is based on ECMA Script has also been modified to fit inside the confines of the browser. Different Browser vendors (Microsoft, Netscape, Opera) have added to and changed their implementation of Javascript to suit their individual needs – Adobe have done the same thing, created a customised version of ECMA Script that suit’s the needs of Acrobat users – AcroJS.

Although Javascript in the Browser and AcroJS share the same common language foundation they operate on completely different Document Object Models (DOM’s).

Why use Acrobat Javascript?

Repetitive tasks are the most common reason for using AcroJS. AcroJS allows you to automate tasks that you do manually time and time again, for example you could write some AcroJS that is part of a Batch Sequence to add a series of Bookmarks to a folder of PDF documents.

AcroJS could also be used to insert additional interactivity into a PDF, for example Javascript in Acrobat has the ability to create a custom Popup Menu that is accessed when you right mouse click.

If you have created some PDF Forms and want to ensure your users enter only valid data then you could use AcroJS to validate and control the data entered. For example you could make use of Regular Expressions to validate a Social Security Number or an Email Address (link to new article).

If you have a full version of Acrobat (v5, v6 Std, v6 Pro) we can make use of Templates to create a dynamic PDF. Templates (link) provide a way to dynamically add marked elements to a pre-existing PDF, elements such as graphics, form fields and text.

The Acrobat Document Object Model (ADOM)

The first diagram is a rough mockup of the Internet Explorer Document Object Model (as seen by Javascript). The second diagram is the Acrobat Document Object Model (as seen by Javascript).

Each diagram illustrates the hierarchy of objects in each respective environment.

src=’http://www.planetpdf.com/planetpdf/images/2-ITAD_LC_DWJ.gif’ width=’400′ height=’242′
alt=’Web browser’>

src=’http://www.planetpdf.com/planetpdf/images/3-ITAD_LC_DWJ.gif’ width=’400′ height=’227′
alt=’Acrobat (Reader/Standard/Professional)’>

How do I use Acrobat Javascript

width=’219′ height=’216′ alt=’AcroJS levels’ align=’right’>
We can write Javascript for different objects/elements in Acrobat. To access the respective elements or objects we need to place our scripts in the appropriate place. AcroJS can be inserted in the following levels:


  • Folder/Application level
  • Document level
  • Page level
  • Form field level
  • Bookmark/Link level

If you want your AcroJS to be available to all PDF documents that are opened then you can insert them at the Folder level. Basically this means writing code that is placed into a .JS file and placed into the appropriate directory (normally C:Program FilesAdobeAcrobat x.0AcrobatJavascripts)

Document level scripts reside inside the PDF and are available and accessible to all other elements and scripts in the PDF. (links)

Page level scripts aren’t accessible by/from any other elements and can be one or both of the following types: Page Open Action or Page Close Action. (links)

AcroJS placed at the form field level can be placed in various locations:


  • Calculation Script
  • Validation Script
  • Format Script
  • Event triggered Script
  • Keystroke Script

As you can see AcroJS at the Form Field level provides a great number of choices.

When you need to run a script that’s activated when you click a Bookmark or a Link then Acrobat provides this functionality as well.

Limitations and Possibilities

So what can’t we do? The single biggest limitation that Javascript has it that it can’t access to the outside world – the operating system. This is actually a good thing as it provides users with the confidence that virii, trojans and the like wont ever be a problem.

In version 6 of Acrobat the interaction between plugins and Javascript has become much stronger. This connection illustrates almost limitless possibilities for serious application development inside Acrobat.

Summary

As a rough guess I would say about 75% of Acrobat can be scripted by Javascript, this represents the possibility to save a lot of time through automation.

By far the most attractive featue of AcroJS to myself is the opportunity it provides in the way of Form validation and interactivity. Javascript gave the Web browser it’s life and AcroJS does the same for Acrobat.

For the full rundown on Javascript in Acrobat check out the Acrobat Javascript section in the Dev Learning Centre.

More Info

You May Also Like

About the Author: Dave Wraight

Leave a Reply