6
Using Acrobat JavaScript in Forms
Forms Essentials
If you are accustomed to calculating the positions of form fields from the top left corner of a
page, the following example will serve as a template for obtaining the correct position. In
this example, we will position a 1 inch by 2 inch form field 0.5 inches from the top of the
page and 1 inch from the left side:
// 1 inch = 72 points
var inch = 72;
// obtain the page coordinates in Rotated User Space
var aRect = this.getPageBox({nPage: 2});
// position the top left corner 1 inch from the left side
aRect[0] += 1 * inch;
// make the rectangle 1 inch wide
aRect[2] = aRect[0] + 1*inch;
// top left corner is 0.5 inch down from the top of the page
aRect[1] -= 0.5*inch;
// make the rectangle 2 inches tall
aRect[3] = aRect[1] - 2*inch;
// draw the button
var f = this.addField("myButton", "button", 2, aRect);
Duplicating Form Fields
It may sometimes be useful to duplicate information typed in by the user in other pages of
the document. For example, you might wish to display the user’s name on every page of
the document.
To automate this, give all such form fields the same name and actions. Then whenever the
user triggers a related action, the same information appears in all form fields containing
that name.
To duplicate form fields in general, assign the same name and actions to each of them. In
the example below, we will create duplicate text fields, each named
myField
, on page 2 of
the document, and we will set the background color of every instance to yellow:
for (var i = 0; i < 5; p++)
{
var aRect = [36, 36+100*i, 72, 144+100*i];
var f = this.addField("myField", "text", 2, aRect);
f.fillColor = yellow;
}
104
Acrobat JavaScript Scripting Guide