Tutorials, tools, scripts and samples for scripting Acrobat and PDFTutorials, tools, scripts and samples for scripting Acrobat and PDF
HomeVideo TutorialsCopy-n-Paste ScriptsDownload LibraryFree ContentSearchMEMBERS AREA


 About this Site
About Us
Contact Us
Privacy Policy
Refund Policy
Site Video Tour
Terms of Use
Testimonials
 DEPARTMENTS
Articles
Video Tutorials
Automating Acrobat
Copy-n-Paste Scripts
Download Library
Free Content
Free Videos
 RESOURCES

Let me know when new content is available.

Home | Free Content | Basic Document Level (Document Open) . . .
 

Basic Document Level (Document Open) Scripts

Open, Event, Global
Printer-Friendly Format

Warning
JavaScript has been turned off for this Browser Window
This page is best viewed with JavaScript turned on

General:

The Document Level scripts are a set of scripts that exist at the top of the Document's JavaScript structure. A PDF can have any number of Document Level scripts. These scripts are the first ones executed when the document is opened in Acrobat or Adobe Reader. In fact, these scripts may actually be run before the document is completely loaded and displayed to the user so care must be taken not to run code in one of these scripts that depends on the document being loaded. For example, any code that affects document pages or creates visual or user interface elements should not be placed in a document script.

Document scripts are used for doing setup operations, performing top level environment testing and defining functions and variables that will be used by other code in the document. It's perfectly acceptable in a Document Level script to define a function that will create a UI element, but it may be risky to run such a function in a Document Level script.

How It Works

The most common way to add a Document Level Script to a PDF is with the
"Advanced > Document Processing > Document JavaScript..." menu item in Acrobat Professional 8.0 and later. In Acrobat 7 the same menu item is at
"Advanced > Document JavaScripts > Document JavaScript..." and in earlier versions there are similar menu item paths. The names of Document Level Scripts are arbitrary and have no effect on scripting so you can call them whatever you want. Descriptive names are useful for later reference. I like to use "Init" as the name of most of my Document Level scripts, because I use them for document initialization. When Acrobat creates the Document level script it automatically adds an empty function to it that has the same name as the script. In most cases this function is useless and should be deleted.

The scripts below are all typical environmental tests and setup activities that are placed in Document Level scripts, such as testing the version and variation of Acrobat.


For more information see these videos


Disclosure: Allowing Access from other Documents/Scripts
Basic Document Level Scripts
Skill Level: Beginner Environment: Acrobat JavaScript
Applies To: PDF Document Script Location: Document Level Script
Acrobat Ver.: 5.05 and later Reader: Yes

If several PDF documents are to be used together then this script must be placed in a Document script on each of the PDFs to disclose them. Disclosure allows scripts on one document to access objects, variables, and functions on another document. Disclosure is necessary for example, on a group of related forms that interact with one another; for a PDF that has a help file that's managed with JavaScript; for documents on CD where search and access functions are handled through JavaScript; or for any other situation where a script in one PDF needs to handle another PDF.

Code:
this.disclosed = true;

Alert User if Acrobat/Adobe Reader Version is too Low
Basic Document Level Scripts
Skill Level: Beginner Environment: Acrobat JavaScript
Applies To: PDF Document Script Location: Document Level Script
Acrobat Ver.: 4.0 and later Reader: Yes

PDF features and Acrobat JavaScript have changed dramatically since Acrobat was first released and continue to change with each new version. Use this script if your PDF requires a viewer of a specific version or later.

How It Works:
The script checks the Acrobat version number and then displays an alert message to the user if the version number is too low.

To Use this Script change the version number that's tested.

Modify this script by changing the message displayed to the user or actions taken by the script for an incorrect version number. The test can also be modified to target a specific version number, or number range.

Code:
if(app.viewerVersion < 7)
{// Acrobat/Reader Version 6 and earlier are unacceptable
   app.alert("This Document Requires Acrobat/Reader 7.0 or later for Proper Operation");
}

Close PDF if Acrobat/Adobe Reader Version is too Low
Basic Document Level Scripts
Skill Level: Beginner Environment: Acrobat JavaScript
Applies To: PDF Document Script Location: Document Level Script
Acrobat Ver.: 5.0 and later Reader: Yes

If the document is opened in a version of Acrobat/Adobe Reader that is too low, a message is displayed to the user and then the PDF is closed. Use this script if your PDF requires a viewer of a specific version or later.

To Use this Script change the version number that's tested.

Code:
if(app.viewerVersion < 7)
{// Acrobat/Reader Version 6 and earlier are unacceptable
   app.alert("This Document Requires Acrobat/Reader 7.0 or later");
   this.closeDoc(true);
}

User Version to Modify Scripting Parameters
Basic Document Level Scripts
Skill Level: Beginner Environment: Acrobat JavaScript
Applies To: PDF Document Script Location: Document Level Script
Acrobat Ver.: 4.0 and later Reader: Yes

In Acrobat 8 Adobe changed the names of many of the common menu items. This is not unusual. In other versions Adobe has also changed names and sometimes removed items altogether. They have also made many changes in how some JavaScript functions and properties behave. If your scripts use things that have been changed between versions, then use this code so your PDF will work in multiple versions of Acrobat/Adobe Reader

How It Works:
This script defines a function that when called zooms the document using the "ZoomIn" menu item. This function will be called by a button script on the document. Unfortunately, the ZoomIn menu item has a different name in Acrobat 8 than it does in Acrobat 7 and earlier, so the function uses the version number to determine which menu item name will be run.

To Use this Script change the menu item name to match a menu item you want to use on your document.

Modify this script by changing the operation controlled by the version "if" statement. Not only menu items names have changed with various versions of Acrobat. For example, the "buttonImportIcon()" function has gone through many variations. You'll have to make the determination of when you need this based on testing your own documents and scripts.

Code:
function DoMyZoom()
{
  if(app.viewerVersion < 8)
    app.execMenuItem("ZoomIn");
  else
    app.execMenuItem("ZoomInMenuItem");
}

Alert User if PDF Opened in Adobe Reader
Basic Document Level Scripts
Skill Level: Beginner Environment: Acrobat JavaScript
Applies To: PDF Document Script Location: Document Level Script
Acrobat Ver.: 3.01 and later Reader: Yes

There are many JavaScript operations that work in Acrobat Professional and Standard, but either require special enabling to work in Adobe Reader or will not work in Adobe Reader at all. These operations are suitable for inter-office documents where the user is expected to have Acrobat, but not for public distribution. Use this script on any PDF that fits these parameters.

How It Works:
The script checks the Acrobat viewer type and then displays an alert message to the user if the type is "Reader".

Modify this script by changing the message displayed to the user or actions taken by the script. The test can also be modified to test for Acrobat Standard or for the many odd variations that Adobe has created over the years. This script can also be modified to test for any of the old Adobe Servers, or LiveCycle servers. These servers can open a PDF and run JavaScript, which may be a big problem for some scripts on the document.

Code:
if(app.viewerType == "Reader")
{
   app.alert("This Document Requires Acrobat Professional or Standard.  It cannot be used in Adobe Reader");
}

Close PDF is User is on a Macintosh
Basic Document Level Scripts
Skill Level: Beginner Environment: Acrobat JavaScript
Applies To: PDF Document Script Location: Document Level Script
Acrobat Ver.: 4.0 and later Reader: Yes

Some Acrobat functionality is only available on Microsoft Windows, for example ODBC database access. This type of functionality also tends to be only suitable for use with Acrobat Professional in an inter-office environment. Some examples are expense reports, HR forms, or internal billing forms. Use this script to warn any errant Mac users that the PDF won't work on thier system.

How It Works:
The script checks the Acrobat Platform property and then displays an alert message to the user if the type is "Mac", and then closes the PDF.

Modify this script by changing the message displayed to the user or actions taken by the script. The test can also be modified to test for Windows or Unix users.

Code:
// To test for a Macintosh use: "MAC"
// To test for Unix use: "UNIX"
if(app.platform != "WIN")
{
   app.alert("This Document Requires an MS Windows OS to Operate.");
   this.closeDoc(true);
}

Expire PDF on a Specific Date
Document Control
Skill Level: Intermediate Environment: Acrobat JavaScript
Applies To: PDF Document Script Location: Document Level Script
Acrobat Ver.: 4.0 and later Reader: Yes

A Document Level script can be used to close a document when a certain date has been reached. This is a simple expiration method with minimal security and can be beaten by anyone who knows PDF and Acrobat JavaScript well enough to understand what's going on. But for most users it's sufficient.

How It Works:
The first part of the script acquires the current and final dates and compares them using Core JavaScript methods. The final, or expiration date, is hard coded into the script. If the final date is earlier than the current date the user is given a popup message and the document is closed. It's important to apply security to the PDF so a user can't see the scripts. However, this part can be beaten by simply turning off JavaScript. The second optional part of the "if" statement, i.e. the "else" part, is designed to make beating the method more difficult.

To use the second part of the script a page size watermark must be applied to the entire document so that all the pages are completely covered. This second part finds the Watermark (OCG) Layer and hides it so the document can be viewed. But before this will work properly, some work needs to be done on the Watermark Layer.
  1. Use the first part of the "Design" code given below to make the Watermark Layer visible in the "Layers" tab.
  2. In the Layers tab right click on the Watermark layer and select Properties to display the Layers properties dialog
  3. On the Layer's properties dialog select "Visible When On" for the "Visibility" drop-down list.
  4. Use the second part of the "Design" code given below to remove the Watermark Layer from the "Layers" tab.
  5. Add the Document Level Script to the PDF
  6. Apply security to the document (after adding the script) and save it.
That's it.

Modify this script by changing the message displayed on expiration.

Code: Design Code
// Copy this script to the Acrobat JavaScript Console 

// Part 1: make Watermark visible in the Layers Tab
this.setOCGOrder(this.getOCGs());

// Part 2: Remove all Layers from the Layers Tab
this.setOCGOrder([]);

Code:Document Level Script
// Copy this script to a document level script
// Get Current Date
var curDate = new Date();
// Get Final date
var finalDate = new Date("3/20/2009");
// Compare Dates
if(finalDate.getTime() < curDate.getTime())
{ // Past closing Date, expire document
   app.alert("This document is no longer valid.  Please contact the author");
   this.closeDoc(true);
}
// Only use code below this point if you have followed the instructions 
// in the How To section above for adding a watermark to the PDF
else
{ // allow document to be shown
  // This part is optional, and just adds better security
  // Document is obscured by a opague watermark that covers all the pages
  var ocgs = this.getOCGs();
  for(var i=0;i<ocgs.length;i++)
  {
     if(ocgs[i].name == "Watermark")
        break;
  }
  // Hide watermark so document can be seen
  ocgs[i].state= false;
}

Display Current Date/Time on Document
Basic Document Level Scripts
Skill Level: Beginner Environment: Acrobat JavaScript
Applies To: PDF Document Script Location: Document Level Script
Acrobat Ver.: 4.0 and later Reader: Yes

When the Document is opened, the current date and/or time is displayed in the header/footer.

How It Works:
The date/time text is dislayed in a Text Field on one or more pages in the PDF. The first part of the script acquires the current date/time and formats it for display. The second part of the script applies the date/time text to a text field.

To Use this Script, add a Text Field to the PDF and name it "CurrentDocDate". Make this field ReadOnly and position it in a convenient location (top or bottom of page). Then duplicate the field to all pages where the date/time will be displayed. It's a good idea to make sure the field looks good (font size, color, border, etc.) and is large enough before duplicating it.

Modify this script by changing the date/time format.

Code:
// Get and Format Date
var currDate = new Date();
var strDate = util.printd("h:MMtt, ddd mmm d, yyyy", currDate);

// Display date on pages
this.getField("CurrentDocDate").value = strDate;

Auto-Increment Visible Form ID Each Time PDF is Opened
Intermediate Document Level Scripts
Skill Level: Intermediate Environment: Acrobat JavaScript
Applies To: PDF Document Script Location: Document Level Script
Acrobat Ver.: 4.0 and later Reader: Yes

When the PDF is opened, a sequential Form ID is displayed in the header/footer. This ID is incremented each time the document is saved and reopened. This mechanism is useful for forms that require a unique ID and are used repeatedly from the same location.

How It Works:
The sequential ID is stored in a form field on the form so the PDF must be saved back to the same location for this technique to work. If more than one person uses the form then it should be opened and saved to a network folder accessible to everyone. This technique will not work for generally distributed forms. But, it will work for forms displyed in Adobe Reader if the form is Reader Rights Enabled with Acrobat 8 Professional

To Use this Script, add a Text Field to the PDF and name it "SeqFormID". If desired, place a seed value in the field. This example uses a pure number, so place any number in the field. Make this field ReadOnly and position it in a convenient location (top or bottom of page). Then duplicate the field to all pages where the Form ID will be displayed. It's a good idea to make sure the field looks good (font size, color, border, etc.) and is large enough before duplicating it.

Modify this script by changing format and the method for incrementing the Form ID. This technique can be generalized for use with distributed forms by acquiring the ID from an internet source or by using a random number technique to generate the number.

Code:
// Get and increment Form ID
this.getField("SeqFormID").value++;