Positioning PDF Stamps Programmatically Part I
Some extra thought is required if the stamp will be sized differently than the original or if the page has a rotation other than zero.
A PDF stamp that exists in the stamps folder can be placed with a script using the document addAnnot( ) method. If the stamp is dynamic, the dynamic features including any popup dialog windows, will execute when the stamp is placed. The method’s input parameter is an object literal with several potential stamp properties, but the following properties are mandatory:
type - The annotation type is Stamp.
AP - The name of the stamp. The following video shows how to obtain the stamp’s AP:
page - The zero-based page number on which the stamp will be placed.
rect - The stamp’s location (rectangle) on the page.
Let me teach you JavaScript for Adobe Acrobat:
rect is Tricky
When placing a Square type annotation with a script, the rect property can be set to any size square or rectangle. Because stamps contain images, you can’t do this. The application prevents you from doing something that would distort that image.
Try This
Place one of the built-in dynamic stamps on a PDF.
These are all rectangular in shape. Now try dragging one of the corners inward or out ward to make the shape into a square. Dragging reshapes the stamp proportionally so it does not lose its shape or distort the image. It is not possible to drag a rectangle stamp into a square. Setting the rect property to a square will not work either. While the script won’t throw an error, the image will simply maintain its shape inside new rect coordinates.
Understanding the rect Property
The rect (“rectangle”) property is an array of four numbers, which are points. There are 72 points per inch. The numbers (subract 1 from the bullet number for the array index) in order are:
Left side (points from the left side of the page, which is zero).
Bottom (points from the bottom of the page).
Right side (points from the left side of the page).
Top (points from the bottom of the page).
Technically, 1 and 2 are the x/y coordinates of the lower left corner, and 3 and 4 are the x/y coordinates of the upper right corner. I find it easier to picture it as left, bottom, right, and top. The rect of a form field starts on the left but moves clockwise instead of counter-clockwise: Left, Top, Right, Bottom.
For a stamp (or any other annotation with a rect property), the rect for a one-inch square in the bottom left corner of the page would be [0, 0, 72, 72]. The same position for a form field would have a rect of [0, 72, 72, 0] (left and right are the same, top and bottom are reversed). If the annotation rect is accidentally used to place a form field, it won’t make any difference in the result as the diagonal corners of the rectangle are switched, resulting the exact same position.
Try This
Create and save a blank page PDF by running the following script in the console:
app.newDoc();app.excMenuItem("SaveAs");
Now run the following scripts in the console:
//Place Reviewed stamp in bottom left corner
this.addAnnot({
type:"Stamp",
AP:"#DReviewed",
page:this.pageNum,
rect:[0,0,255.64,55.11]
});
//Place Reviewed stamp in bottom right corner
this.addAnnot({
type:"Stamp",
AP:"#DReviewed",
page:this.pageNum,
rect:[612-255.64,0,612,55.11]
});
//Place Reviewed stamp in top right corner
this.addAnnot({
type:"Stamp",
AP:"#DReviewed",
page:this.pageNum,
rect:[612-255.64,792-55.11,612,792]
});
//Place Reviewed stamp in top left corner
this.addAnnot({
type:"Stamp",
AP:"#DReviewed",
page:this.pageNum,
rect:[0,792-55.11,255.64,792]
});
The following numbers were used in stamp placement positioning:
255.64 - The width of the stamp.
55.11 - The height of the stamp.
612 - The width of the page.
792 - The height of the page.
Next week I will out line where these numbers came from. See you then…