Skip to main content
Participating Frequently
December 28, 2017
Answered

Placing Form Button over each Bullet in a Document (or finding string's coordinates on page)?

  • December 28, 2017
  • 3 replies
  • 1588 views

Hello! ! I'm coming from different languages, just getting back into JavaScript, and struggling on this one...

I see how to JavaScript the placing of a Form Button on a page, including how to set its location...

https://acrobatusers.com/tutorials/js_add_buttons_to_pdf

...but I'm trying to figure out how to automatically place the button over each bullet in a document, for example.

How can I find the coordinates (so to speak) of specific text or character strings found within a document, such as using a method like this?
https://acrobatusers.com/tutorials/text-matching-regular-expressions

I'm using primarily Adobe Acrobat X Pro, but also Adobe Acrobat DC Pro.

This topic has been closed for replies.
Correct answer JR Boulay

You should read the addLink chapter in the JavaScript™ for Acrobat® API Reference, it gives a nice sample that you can easily hack to fit your needs:

// Example 2

// Search through the document for the word “Acrobat” and create a link around that word.

for (var p = 0; p < this.numPages; p++)

{

var numWords = this.getPageNumWords(p);

for (var i=0; i<numWords; i++)

{

var ckWord = this.getPageNthWord(p, i, true);

if ( ckWord == "Acrobat")

{

var q = this.getPageNthWordQuads(p, i);

// Convert quads in default user space to rotated

// User space used by Links.

m = (new Matrix2D).fromRotated(this,p);

mInv = m.invert()

r = mInv.transform(q)

r=r.toString()

r = r.split(",");

l = addLink(p, [r[4], r[5], r[2], r[3]]);

l.borderColor = color.red;

l.borderWidth = 1;

l.setAction("this.getURL('http://www.example.com/')");

}

}

}

I started from this script it to create this free tool: https://www.abracadabrapdf.net/?p=7437

(With buttons form fields instead of Link objects since a Link cannot have an "underlined" border)

3 replies

JR Boulay
Community Expert
Community Expert
January 9, 2018

- Waow! Happy 2018

- Multiple responses as correct: no

Acrobate du PDF, InDesigner et Photoshopographe
JR Boulay
Community Expert
JR BoulayCommunity ExpertCorrect answer
Community Expert
December 30, 2017

You should read the addLink chapter in the JavaScript™ for Acrobat® API Reference, it gives a nice sample that you can easily hack to fit your needs:

// Example 2

// Search through the document for the word “Acrobat” and create a link around that word.

for (var p = 0; p < this.numPages; p++)

{

var numWords = this.getPageNumWords(p);

for (var i=0; i<numWords; i++)

{

var ckWord = this.getPageNthWord(p, i, true);

if ( ckWord == "Acrobat")

{

var q = this.getPageNthWordQuads(p, i);

// Convert quads in default user space to rotated

// User space used by Links.

m = (new Matrix2D).fromRotated(this,p);

mInv = m.invert()

r = mInv.transform(q)

r=r.toString()

r = r.split(",");

l = addLink(p, [r[4], r[5], r[2], r[3]]);

l.borderColor = color.red;

l.borderWidth = 1;

l.setAction("this.getURL('http://www.example.com/')");

}

}

}

I started from this script it to create this free tool: https://www.abracadabrapdf.net/?p=7437

(With buttons form fields instead of Link objects since a Link cannot have an "underlined" border)

Acrobate du PDF, InDesigner et Photoshopographe
Participating Frequently
January 9, 2018

Wow... what a New Year's! I'm sorry for disappearing for a while... we just had our first child! Both mom and baby are doing great.

And you guys have been super helpful... can I mark multiple responses as correct?

Thom Parker
Community Expert
Community Expert
December 28, 2017

Unfortunately I never wrote a tutorial on this topic. But, you'll find everything you'll need in the Acrobat JavaScript Reference.

Here is the page with the relavant function, doc.getPageNthWordQuads(). 

Acrobat DC SDK Documentation

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Participating Frequently
December 29, 2017

Awesome tutorials that you did write though, thank you so much!

This is EXACTLY what I was looking for.

But one more Q...

To get the word number N, I would have to iterate through the words with something like getPageNthWord() ?
Or is there a search function that can return the number N for a result?

try67
Community Expert
Community Expert
December 29, 2017

You would need to iterate over all the words in the page, unless of course you know in advance what N is, and then you can specify it directly.