In last week’s article I provided scripts to run in the console that placed the built-in Reviewed dynamic stamp in all four corners of the page.
The scripts used the height and width of the stamp.
Calculating Stamp Height and Width
To calculate the height of a stamp, simply subtract the bottom rect [1] from the top [3].
To calculate the width of a stamp, simply subtract the left rect [0] from the right [2].
First, apply a stamp to a page and then select the stamp. Use the Reviewed dynamic stamp for this example.
Run the following script in the console:
var rc=this.selectedAnnots[0].rect;
var height=rc[3]-rc[1];
var width=rc[2]-rc[0];
console.println("The height of the stamp is "+height+ "points.\rThe width of the stamp is "+width+" points.");
The script above should return the following two lines:
The height of the stamp is 55.1083984375points.
The width of the stamp is 255.63818359375 points.
For simplicity, and without negatively affecting the results, these numbers can be rounded to 55.11 and 255.64.
Calculating Page Height and Width
In the previous post, the page height was 792 points (11 inches) and the width was 612 points (8.5 inches). There was no need to calculate these because the page was created with the app.newDoc( ) script without the optional height and width input parameters. Therefore, a default page size of 8.5" x 11" was produced.
The crop box of the page can be used to determine the height and width of the page when it is unknown. Like the stamp and form field rect, the crop box has an array of four coordinates that start on the left edge, and like the form field rect, the order goes clockwise: left, top, right, bottom. The left and bottom are always zero, therefore the top is the height and the right is the width. You can obtain the crop box array by running the following script in the console:
this.getPageBox("Crop", this.pageNum);
The script above will return the crop box array for the current page. this.pageNum can be replaced by the actual zero-based page number to obtain the crop box for any page number in the document.
Positioning Using The Crop Box of The Page
The following scripts placed the Reviewed stamp in each corner of the page when run from the console. These scripts are the same as the ones from the last post, except the crop box is used to calculate page height and width in case it is unknown.
//Define the crop box
var cb=this.getPageBox("Crop", this.pageNum);
//Place Reviewed stamp in bottom left corner
this.addAnnot({
type:"Stamp",
AP:"#DReviewed",
page:this.pageNum,
rect:[0,0,255.64,55.11]
});
//The 2 zeros in the rect above could also be cb[0] and cb[3]
//Place Reviewed stamp in bottom right corner
this.addAnnot({
type:"Stamp",
AP:"#DReviewed",
page:this.pageNum,
rect:[cb[2]-255.64,0,cb[2],55.11]
});
//Place Reviewed stamp in top right corner
this.addAnnot({
type:"Stamp",
AP:"#DReviewed",
page:this.pageNum,
rect:[cb[2]-255.64,cb[1]-55.11,cb[2],cb[1]]
});
//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]]
});
Video
I use the method above in our customizable dynamic stamp to automatically position stamps.
Resizing Stamps
In Part I of this series I explained how manually resizing stamps does not allow image distortion. When resizing programatically by changing the rect, the proportions need to me maintained.
In the example above, the Reviewed stamp has a height of 55.11 and a width of 255.64. This is a height/width ratio of 0.2156 or a width/height ratio of 4.6387. The stamp should be resized in accordance with these ratios. For example, if you wanted to double the height by multiplying it by 2, you would divide the new height by the height/width ratio to get the correct width:
New Height: 55.11 * 2 = 110.22
New Width: 110.22/0.2156 = 511.22
Notice the new width is double the old width (not exactly, but that is due to rounding of the height, width, and ratio). While it is helpful to understand the math behind this, it is not necessary to perform the math. All that is required is for the increase of height be in the same proportion to the increase in width, and vice versa. You can also let the script do the math for you. If you have been following along with the examples, delete the Reviewed stamp in the bottom left corner of your page. Use the following code to apply the stamp at double the height and width (and let the script do the math for you):
//Place Reviewed stamp in bottom left corner
this.addAnnot({
type:"Stamp",
AP:"#DReviewed",
page:this.pageNum,
rect:[0,0,255.64*2,55.11*2]
});
Now select the stamp and reduce the stamp by 50% of the height and width by changing its rect. Run the following script in the console:
var rc=this.selectedAnnots[0].rect;
this.selectedAnnots[0].rect=[rc[0],rc[1],rc[2]*0.5,rc[3]*0.5];
This only works with bottom left corner stamp because the left and the bottom coordinates are zero, so the width and height can be increased from there. The other three stamps have to be modified slightly differently:
Top Left - The width can be increased accordingly because its rect coordintate is the right side of the stamp. The bottom coordinate should be decreased proportionally.
//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*2,255.64*2,cb[1]] });
Top Right - The left and the bottom should be decreased proportionally.
//Define the crop box var cb=this.getPageBox("Crop", this.pageNum); //Place Reviewed stamp in top right corner this.addAnnot({ type:"Stamp", AP:"#DReviewed", page:this.pageNum, rect:[cb[2]-255.64*2,cb[1]-55.11*2,cb[2],cb[1]] });
Bottom Right - The left should be decreased proportionally. The top should be increased proportionally.
//Define the crop box var cb=this.getPageBox("Crop", this.pageNum); //Place Reviewed stamp in bottom right corner this.addAnnot({ type:"Stamp", AP:"#DReviewed", page:this.pageNum, rect:[cb[2]-255.64*2,0,cb[2],55.11*2] });