Positioning PDF Stamps Programmatically Part III
How to change the stamp size relative to where is lands when applied from the stamps menu.
Last week I explained how to resize stamps in four corners of the page using JavaScript.
All five of our number-incrementing stamps have five different size settings. They are actually one stamp that is resized when applied, no matter where the stamp lands on on the page. This done by using the heigh/width ratio of the stamp to increase or decrease all sides proportionally.
Increase the Height by 25% and Increase the Width Proportionally
In this example, the top and bottom of the stamp with increase by 12.5% of the original height from the middle and the left and right sides will increase by 12.5% from the middle: Here are the steps:
Apply the built-in Reviewed stamp from the Dynamic category of the stamps menu, anywhere on the page.
Obtain the rect of the stamp.
Use the rect to obtain the height and width.
Decrease the left rect by 1/2 of 25% of the width.
Decrease the bottom rect by by 1/2 of 25% of the height.
Increase the right rect by 1/2 of 25% of the width.
Increase the top rect by 1/2 of 25% of the height.
Use the results of points 3 through 6 to set the rect of the stamp.
Here’s the script you can run in the console for points 2 through 8 after you have selected the stamp:
var anot=this.selectedAnnots[0];
var rc=anot.rect;
var height=rc[3]-rc[1];
var width=rc[2]-rc[0];
anot.rect=[rc[0]-width*0.25*.5,rc[1]-height*0.25*.5,rc[2]+width*0.25*.5,rc[3]+height*0.25*.5];
Tip
Instead of selecting the stamp, the first line can be replaced with the following lines:
if(this.getAnnots()!=null)
{var anot=this.getAnnots()[this.getAnnots().length-1]}
The first line above test for the existence of annotations in order to avoid an error if none exist. The second line uses the last annotation applied to define the anot variable. This is the one we use when programming a size change into a stamp script because the user can’t select the stamp being applied, but the script can identify the stamp being applied.