• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers

Finding text and determining a coordinate

Participant ,
Apr 24, 2021 Apr 24, 2021

Copy link to clipboard

Copied

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?

TOPICS
Acrobat SDK and JavaScript

Views

243

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Apr 24, 2021 Apr 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.

Likes

Translate

Translate
Community Expert ,
Apr 24, 2021 Apr 24, 2021

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
May 03, 2021 May 03, 2021

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 03, 2021 May 03, 2021

Copy link to clipboard

Copied

LATEST

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.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines