Copy link to clipboard
Copied
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.
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 us
Copy link to clipboard
Copied
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().
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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)
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
- Waow! Happy 2018
- Multiple responses as correct: no