Skip to main content
Dmitriy K.
Known Participant
April 24, 2021
Answered

Finding text and determining a coordinate

  • April 24, 2021
  • 1 reply
  • 2164 views

Hello everyone! I am trying to solve a problem: automatic insertion of a signature into a stamp on each sheet of a document.
At the moment, I managed to create a dynamic stamp with a signature, refer to it from the code and insert the signature at the specified coordinate:

app.addMenuItem({ cName: "Little_stamp", cUser: "Little_stamp", cParent: "Help", nPos: -1,
cExec: "myTrustedFunction()", cEnable: "event.rc = (event.target != null);"});

function myTrustedFunction() {
for (var p = 0; p < this.numPages; p++) {
var annot = this.addAnnot({
page: p,
type: "Stamp",
author: "Acrobat",
name: "myStamp",
rect: [175, 40, 205, 60],
AP: "#mySig" });
app.trustedFunction(myTrustedFunction);

... but unfortunately this method sometimes fails because the coordinate is changing. Therefore, I decided to implement another way: find a specific word, find out its coordinates and step back 20 points from it. Tell me how to do this?

This topic has been closed for replies.
Correct answer try67

Use getPageNthWord and getPageNumWords to search each word in the page.

When you found a match you can use getPageNthWordQuads to get that word's location on the page.

Then you'll need to use a mathematical operation to convert that quads array to a rect array (this is complicated, but was discussed on the forums many times and there are code examples on how to do it), modify the rect array to the desired location and then add the stamp using it.

1 reply

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
April 24, 2021

Use getPageNthWord and getPageNumWords to search each word in the page.

When you found a match you can use getPageNthWordQuads to get that word's location on the page.

Then you'll need to use a mathematical operation to convert that quads array to a rect array (this is complicated, but was discussed on the forums many times and there are code examples on how to do it), modify the rect array to the desired location and then add the stamp using it.

Dmitriy K.
Known Participant
May 3, 2021

Thanks for the tip, I was able to find the coordinates using the getPageNthWordQuads method! But, unfortunately, it didn’t work out what I wanted, and I went the other way. I find out the page size using getPageBox ("Crop", 0) and subtract the desired number from the obtained coordinates, getting the insertion coordinates.

var array = this.getPageBox("Crop",0);
r=array.toString()
r = r.split(",");
var rectX = [Math.round(r[2])-420, Math.round(r[1])-1060, Math.round(r[2])-390, Math.round(r[1])-1080];
this.addAnnot({
               page: 0,
               type: "Stamp",
               rect: rectX,
               author: "A. C. Acrobat",
              AP: "NotApproved"
            });

This works everywhere except for certain pages, on which the annotation is inserted vertically and outside the page! I am attaching an example of such a sheet.

try67
Community Expert
Community Expert
May 3, 2021

Even without opening the file I can tell you that it's happening because those pages are rotated (even if they don't look like they are), and that there's no simple solution to this issue. My advice would be to re-create those pages by exporting them as an image and then creating a new PDF file from that image. That will create a new, non-rotated page. Then you have to run Text Recognition on it, and then (if that process was successful) you could use your script on it.

 

NB. I opened the file and indeed, the page is rotated by 270 degrees.