Add hyperlinks through Javascript in a large pdf document
Copy link to clipboard
Copied
Hello everyone,
I am working on adding hyperlinks in a large .pdf document (over 4000 pages).
JS Script is doing the job, except the execution time is around 4h. I imagine there is a faster technical solution to do the same thing. I guess just the double for loop is ruining it.
Looking at the messages in otherr discussions, it seems the document is a bit large to use JS from Adobe
Is there any proper solution to make the script faster? Any other way? Anything obviously stupid in the code below?
Thank you so much,
Xavier
//go through document page by page
for (var p = 0; p < this.numPages; p++)
{
//get number of words in the page
var numWords = this.getPageNumWords(p);
for (var i = 0; i<numWords; i++)
{
var Word = this.getPageNthWord(p, i, true);
if (this.getPageNthWord( p, (i+1), true) != null)
{
var NextWord = this.getPageNthWord(p, (i+1), true);
//Detect Word "Model" and number of characters after this model
if (Word == "Model")
{
if ( (NextWord.length == 12) &e
& (NextWord.indexOf(".") > -1) )
{
//add link on the detected zonvar q = this.getPageNthWordQuads(p, (i+1));
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]]);
var HTMLFile = NextWord.replace(".","_") + "/" + NextWord.replace(".","_") + "_view.html";
l.setAction("Myscript(this.path.substr(0,this.path.lastIndexOf(\"/\")) + \"/2 - Database files/HTML Files/" + HTMLFile + "\");");
}
}
}
}
}
Copy link to clipboard
Copied
A script in Acrobat has a lot of problems processing all the words in such large files. It's really not suited for the job, unfortunately. However, there are alternatives. If you could use the Search & Remove Text command to identify the texts you're interested in a script could then use those Redaction annotations to create the links (and remove the annotations without applying them, of course). That would be a two part process, but it will work much faster as the searching will be done by Acrobat itself, not with a script.
Another option is to use a stand-alone tool for doing it. I've developed such a tool, so if you're interested in purchasing it feel free to contact me privately to discuss it further.

