Executing code in PDF files on opening
assembly date
2007, November 27.
author(s)
Balla Marcell
keywords
- software
- programming
related links
Wiki related to Adobe Acrobat
Acrobat Developer Center website
Acrobat PDF JavaScript Scripting Reference
This article discusses how to insert script code into a PDF file (using Adobe Acrobat Professional) that is executed when the file gets opened.
It is possible to insert programming code into a PDF file to cause some calculations or evaluations on specified events. For example one could create a PDF form that reacts when a textbox loses its focus to refresh other fields that are based on the value of the original textbox.
Programming code in PDF files is done using JavaScript syntax. Because the code is related to a PDF file rather than a website there is no DOM but specific PDF objects and functions to use.
Implementing actions based on specific events are rather easy considering that there are free Adobe scripting guides that contain all document related objects, events and functions (check the related links sections for the official guide).
But how do you go about the task of executing JavaScript when the PDF file is being opened? First guess would be to look for OnStartup or OnOpen events. But where could you find these? These events have to be related to the whole document and not to single controls (like a textbox). How is it possible to react on these events?
The answer is easier than you might think. In Adobe Acrobat Professional it is possible to specify global functions that are available to every control in a document. When you open a PDF file these functions are read and “executed” so other controls can reference them. Executing here means: defining the function itself. They don’t really get executed. They are stored so you can use them in the document.
The way to execute code when a PDF file is opened is to misuse this behavior. Let’s get down to it.
You can access the document related JavaScript functions through the Adobe Acrobat Professional menu. Unfortunately it can be found in different locations in the versions 7 and 8. Figure 1 show the location of the menu item in Adobe Acrobat 8.
Figure 1: the "Document JavaScripts..." menu item in Adobe Acrobat 8
Once you click this menu item a dialog shows up which enables you to create "JavaScript Functions". These functions can be imaged as document global functions.
What we need to do here is to create such a function and strip off the function block. To do this, enter any name into the "Script Name:" field and click the "Add..." button.
Now a new window pops up and lets you enter the function’s code. Since we want the code to be executed when the file is opened we delete the function block (basically the whole text that is inside the textbox) and insert the code we want. Figure 2 shows an example of stripping of the function block and inserting a simple JavaScript statement.
Figure 2: stripping of the function block
When we click the “OK” button and save the file this code is stored inside the PDF document.
When we open the file now the code gets executed just like if it was a function definition. But since it has no circumfluent function block it gets executed immediately instead of being defined as a function.
Yes, it is that easy. Now you are free to enter and execute whatever PDF JavaScript code you would like. What is often done and pretty useful is to retrieve the screen resolution and change the zoom factor to a reasonable value based on the resolution. Here is how you can do that:
// get copy of monitor array after applying the primary() filter method
var monitors = app.monitors.primary();
// get an array specifying the boundaries of the first suitable monitor
var resolution = monitors[0].rect;
// the array contains left, top, right, bottom as elements (index starts at zero!)
// check if resolution is higher than 1024x768
if(resolution[2] >= 1024 && resolution[3] >= 768)
{
this.zoom = 100; // set zoom factor to 100% (resolution is high enough)
}
else
{
this.zoom = 60; // set zoom factor to 60%
}
Happy coding!
