Skip to main content
Known Participant
February 7, 2023
Question

acrobat javascript 'addLink' not working properly in cropped pages.

  • February 7, 2023
  • 3 replies
  • 2440 views

I have a 3 page file with pages of differently cropped sizes. Now I want to search through the document for the word “purchase” and create a link around that word. I am using this code below:

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 == "purchase")
{
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/')");
}
}
}

The problem is: the links are not created properly. When I zoom out the pages, I see the links are created outside the cropped pages. How to fix this?
I have another issue:
If I click "Edit PDF", I see empty textboxes outside the cropped areas. These are probably from the original pages. I cropped these pages using "this.setPageBoxes: 'Media'" in javascript. Is there any way to remove these textboxes using javascript? 

I am attaching a sample file for reference.

 

 

This topic has been closed for replies.

3 replies

JR Boulay
Community Expert
February 8, 2023

"Can you explain what you mean by 'the page format' ? I can see page dimensions where you indicated in the picture."

Imran25844166y4hb

Sorry, in French "page format" means "page dimensions" and so I often make this mistake.

Acrobate du PDF, InDesigner et Photoshopographe
try67
Community Expert
February 7, 2023

Yes, you need to add to the rect an offset. I use the difference between the Crop and Trip boxes of the page for that, like this:

 

var cropBox = doc.getPageBox("Crop", p);
var trimBox = doc.getPageBox("Trim", p);
var xOffset = cropBox[0]-trimBox[0];
var yOffset = cropBox[1]-trimBox[1];
var r = [Number(r[4])+xOffset, Number(r[3])-yOffset, Number(r[2])+xOffset, Number(r[5])-yOffset];
Known Participant
February 7, 2023

Thank you. But-
Since 'Media', 'Crop', 'Trim', 'Art'- all pageboxes are of same size, offset values are '0'. What to do now?

Known Participant
February 8, 2023

I see it now. In this case the issue is with BBox. Calculate the offset between the Crop box and it, and see if that works.


nope. not working

JR Boulay
Community Expert
February 7, 2023

This document is weird, the page format is not indicated where it should be. Unlike other PDF documents.

 

Acrobate du PDF, InDesigner et Photoshopographe
Known Participant
February 7, 2023

Can you explain what you mean by 'the page format' ? I can see page dimensions where you indicated in the picture. I am working on ADOBE Acrobat Pro DC.