by Thom Parker, Software Developer/Adventurer, WindJack Solutions, Inc.
Scope: All Acrobat versions
Skill Level: Beginner
Prerequisites: Familiarity with Acrobat Professional
To script activities such as creating form fields and resizing pages it is necessary to know the dimensions of a document page. In PDF this can be a bit confusing because pages are bounded by several different Page Boxes (Figure 1). Each Page Box has a special purpose, so which one is used depends on the purpose of the script.
Page Size Example
Download [PDF: 69 KB]
Getting the Size of a Page
The Crop Box is the most useful for scripting purposes. It is the bounding box the user sees on the screen so it is also the bounding box used to position visible elements like form fields, annotations, and watermarks. To get the Crop Box for the current document page use the following code (see PageSizeExample.pdf):
var rCrop = this.getPageBox("Crop",this.pageNum);
The return value is an array of four numbers representing the edges of a rectangle in the order Left, Top, Right, Bottom. The values are in points and there are 72 points/inch. The rCrop for an 8.5 x 11 inch page would be:
The Crop Box has the special property of being the reference for a page’s coordinate system so it’s bottom left corner is always at the coordinates (0, 0). To acquire any of the other bounding boxes just substitute the word “Crop” in the code above with Art, Bleed, Trim, Media, or BBox.
Resizing a Page
var rCrop = this.getPageBox("Crop",this.pageNum); rCrop += 36; // Adjust Left Side rCrop -= 36; // Adjust Top Side rCrop -= 36; // Adjust Right Side rCrop += 36; // Adjust Bottom Side this.setPageBoxes("Crop",this.pageNum,this.pageNum,rCrop);
There are a couple things to notice here. First, it was stated previously the Crop Box is always at (0, 0), but in this code we are changing the value of the bottom left corner. Not to worry, Acrobat readjusts all coordinate values based on the Crop Box so if you re-acquire the Crop Box its value will be:
The second thing to note is the Doc.setPageBoxes() function uses two page number parameters. It has both start and stop page number arguments for setting a range of pages at the same time.
Placing a Field
var rFld = this.getPageBox("Crop",this.pageNum); // Button is 1/2 x 2 inches // Button is anchored on the top right corner rFld -= 36; // 1/2 inch from top rFld -= 36; // 1/2 from right Side rFld = rFld - 36; // Adjust Bottom Side rFld = rFld - 144; // Adjust Left Side this.addField("MyPrint", "button", this.pageNum, rFld);
|Edit PDF create PDF Action Wizard|