How to place a stamp on multible opened documents + How to place a stamp no matter if page is rotated or not.

A picture is worth a thousand words. We have created some pdfs tutorials showing common tasks.

Moderators: TrackerSupp-Daniel, Tracker Support, Vasyl-Tracker Dev Team, Chris - Tracker Supp, Sean - Tracker, Tracker Supp-Stefan

KD952
User
Posts: 60
Joined: Mon Feb 13, 2023 6:13 am

How to place a stamp on multible opened documents + How to place a stamp no matter if page is rotated or not.

Post by KD952 »

Disclaimer: I don't know how to code. I am just smashing my head against a brick wall until it works.

The following code places the "Approved" stamp on all opened documents. The stamp is placed in the bottom right-hand corner as seen by the user (Rotated User Space). The stamp is placed on the first Page of the document. It does not matter if the page is rotated or not.
I hope this helps somebody in the future.

Kind regards,
Daniel

Code: Select all

var d = app.activeDocs; 
for (var i = 0; i < d.length; i++)
{
var point = (0.352778);
var nStampWidth = (86.52/point); 
var nStampHeight = (22.72/point);

// Define the stamp location in Rotated Space
var rctCropRot = d[i].getPageBox("Crop",0);

// Define Stamp Location in rotated Space, 
var nStampLeft = (rctCropRot[2] - nStampWidth);
var nStampTop = (rctCropRot[3] + nStampHeight);
var nStampRight = (rctCropRot[2]);
var nStampBottom = (rctCropRot[3]);

var rctAnnotRot = [nStampLeft, nStampTop, nStampRight, nStampBottom]; 

// Convert the annot position into Default space
var mxFromRot = (new Matrix2D).fromRotated(d[i],0);
var rctAnnotDflt = mxFromRot.transform(rctAnnotRot);

d[i].addAnnot({type:"Stamp",page:0,AP:"Approved", rect:rctAnnotDflt, rotate:d[i].getPageRotation(0)});
}
KD952
User
Posts: 60
Joined: Mon Feb 13, 2023 6:13 am

Re: How to place a stamp on multible opened documents + How to place a stamp no matter if page is rotated or not.

Post by KD952 »

Here the code with js-tool Button.

Kind regards,
Daniel

Code: Select all


//set up a trusted function (app.activeDocs can only be run from privileged context)

var ApproveAll = app.trustedFunction( function() { 
	app.beginPriv();

// get an array of all active documents
var d = app.activeDocs; 
for (var i = 0; i < d.length; i++)
{
var point = (0.352778);

// define Stamp Width and Height in mm
var nStampWidth = (86.52/point); 
var nStampHeight = (22.72/point);

// get the sice of the Crop-Box = what the user sees
var rctCropRot = d[i].getPageBox("Crop",0);

// Define Stamp Location in rotated Space (what the user sees), 
var nStampLeft = (rctCropRot[2] - nStampWidth);
var nStampTop = (rctCropRot[3] + nStampHeight);
var nStampRight = (rctCropRot[2]);
var nStampBottom = (rctCropRot[3]);

var rctAnnotRot = [nStampLeft, nStampTop, nStampRight, nStampBottom]; 

// Convert the annot position into Default space
var mxFromRot = (new Matrix2D).fromRotated(d[i],0);
var rctAnnotDflt = mxFromRot.transform(rctAnnotRot);

//place the Approved stamp
d[i].addAnnot({type:"Stamp",page:0,AP:"Approved", rect:rctAnnotDflt, rotate:d[i].getPageRotation(0)});
}

app.endPriv();
});


// add js-Button
app.addToolButton({
    cName: "ApproveAll",
    cLabel: "ApproveAll",
    cExec: "ApproveAll()",
    cTooltext: "Approve_All_Documents",
    cEnable: true,
    nPos: 0
});
User avatar
Tracker Supp-Stefan
Site Admin
Posts: 17960
Joined: Mon Jan 12, 2009 8:07 am
Location: London

Re: How to place a stamp on multible opened documents + How to place a stamp no matter if page is rotated or not.

Post by Tracker Supp-Stefan »

Hello Daniel,

Many thanks for sharing this code with others! I hope it does get some good use! :)

Kind regards,
Stefan
KD952
User
Posts: 60
Joined: Mon Feb 13, 2023 6:13 am

Re: How to place a stamp on multible opened documents + How to place a stamp no matter if page is rotated or not.

Post by KD952 »

Hello Stefan!

Thank you! :)

Question for you:

The code works fine even if you change the CropBox. For example after using the command Crop Pages (cmd.document.cropPages)

The only time the code breaks down is after using the command Resize Pages (cmd.document.resizePages).

Seems to me the command Resize Pages (cmd.document.resizePages) somehow messes up the CropBox of the page?

Kind regards,
Daniel
User avatar
Tracker Supp-Stefan
Site Admin
Posts: 17960
Joined: Mon Jan 12, 2009 8:07 am
Location: London

Re: How to place a stamp on multible opened documents + How to place a stamp no matter if page is rotated or not.

Post by Tracker Supp-Stefan »

Hello KD952,

What parameters do you use for the Resize Pages operation?
Could it that after your resize - the coordinates you are specifying fall outside of the remaining crop box area?

Kind regards,
Stefan
KD952
User
Posts: 60
Joined: Mon Feb 13, 2023 6:13 am

Re: How to place a stamp on multible opened documents + How to place a stamp no matter if page is rotated or not.

Post by KD952 »

Hello Stefan!

Thank you for your help. It's fixed now.

There is literally a check box to solve the issue. He who can read has the advantage...
image.png
Kind regards,
Daniel
You do not have the required permissions to view the files attached to this post.
User avatar
Tracker Supp-Stefan
Site Admin
Posts: 17960
Joined: Mon Jan 12, 2009 8:07 am
Location: London

Re: How to place a stamp on multible opened documents + How to place a stamp no matter if page is rotated or not.

Post by Tracker Supp-Stefan »

Hello Daniel,

Yeah - was about to suggest that after seeing the settings you use! :)
Glad to see you figured it out on your own!

Enjoy using our products and
Kind regards,
Stefan