Page coordinate arrays (page box). as well the rectangle (rect property) of form fields are in the following order (clockwise starting on the left):
Left
Top
Right
Bottom
Markup annotations like stamps also start on the left, but move counterclockwise:
Left
Bottom
Right
Top
The fact that the bottom and the top are reversed with annotations and the fact that the rect property of form fields is in rotatated user space, which the rect property of stamps is in default user space makes placing stamps programatically relative to the page box extremely complicated.
I trust the way I outline this will simplify the process for you. If you want learn JavaScript for Adobe Acrobat from the ground up, please consider supporting my work by enrolling in my online course.
Stamp Rotation Property
Just like PDF pages, PDF stamps have a rotation property that can be 0, 90, 180, or 270 degrees (clockwise in 90 degree increments). For stamps and other annotations, the property is called rotate. The stamp’s rotate property should be set to match the page’s rotation. This is the easies part of the process. The optional rotate property can simply be added to the addAnnot( ) method when placing the stamp. The following script will place the Received stamp in the top left corner of a page with zero rotation.
//define the variable pgRot for the rotation of the current page
var pgRot=this.getPageRotation(this.pageNum);
//Define the crop box
var cb=this.getPageBox("Crop", this.pageNum);
//Place Reviewed stamp in top left corner
this.addAnnot({
type:"Stamp",
AP:"#DReviewed",
page:this.pageNum,
rect:[0,cb[1]-55.11,255.64,cb[1]],
rotate:pgRot
});
The only difference in the script from last week’s article that placed the Received stamp in the top left corner is the addition of the rotate property (in bold). If you rotate the stamp without modifying its rectangle, the image will rotate inside the rectangle first, distorting its size, then adjust the rectangle to the newly size image.
Try It
Select the stamp created with the script above then run the following scripts, one at a time, in the console:
this.selectedAnnots[0].rotate=90;
this.selectedAnnots[0].rotate=180;
this.selectedAnnots[0].rotate=270;
this.selectedAnnots[0].rotate=0;
Rect
This is the tricky part. Here are the steps:
Rotate the page 90 degrees.
Manually place the stamp on the page and positioned it to the desired location (top left corner for all of these examples).
Rotate the page back to zero degrees. If rotating manually use counterclockwise 90 degrees, if using a script use this.setPageRotations(this.pageNum, this.pageNum, 0).
Note the position and rotation of the stamp. It has moved from the top left to the bottom left and it is now vertical (rotation of 90 degrees).
Use the stamp’s position to calculate the rect in it’s current location. Because of the 90 degree rotation width becomes height and height becomes width:
var height=55.11; var width=255.64; var cb=this.getPageBox("Crop",0); /* Left is cb[0], cb[3], right is height, top width */ /*NOTE: The bottom of the stamp is cb[3] because the stamp rect array moves counterclockwise */ //Define the rect variable rc=[cb[0], cb[3],height, width];
REMINDER: 0° and 180° have the same crop box, as do 90° and 270°. The left and bottom (first and last in the crop box array) are the same (zero) with any rotation, while the top and right (second and third in the crop box array) are reversed with 90 or 270 degrees compared to 0 or 180 degrees. An 8.5" x 11" page have the following crop boxes:
[0, 792, 612, 0]// 0 or 180 rotation [0, 612, 792, 0]// 90 or 270 rotation
Define a variable for page rotation and set conditions to reverse the top and right of the crop box if page rotation is 90 or 270 degrees.
var pgRot=this.getPageRotation(this.pageNum);
if(pgRot==90 || pgRot==270)
{cb=[cb[0],cb[2],cb[1],cb[3]]}
Set page rotation back to zero and repeat steps 1 through 5 for rotations of 180 and 270 degrees.
Set conditions to change the rect based on the page rotation.
Apply the stamp, which will be placed in the top left corner and rotated properly no matter what the page rotation is.
Here’s the full code:
var height=55.11;
var width=255.64;
var cb=this.getPageBox("Crop",this.pageNum);
var pgRot=this.getPageRotation(this.pageNum);
if(pgRot==90 || pgRot==270)
{cb=[cb[0],cb[2],cb[1],cb[3]]}
var rc;
if(pgRot==0)
{rc=[0,cb[1]-height,width,cb[1]]}
if(pgRot==90)
{rc=[cb[0], cb[3],height, width]}
if(pgRot==180)
{rc=[cb[2]-width,cb[3],cb[2],height]}
if(pgRot==270)
{rc=[cb[2]-height,cb[1]-width,cb[2],cb[1]]}
this.addAnnot({
type:"Stamp",
AP:"#DReviewed",
page:this.pageNum,
rect:rc,
rotate:pgRot
});