Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
This document is weird, the page format is not indicated where it should be. Unlike other PDF documents.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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];
Copy link to clipboard
Copied
Thank you. But-
Since 'Media', 'Crop', 'Trim', 'Art'- all pageboxes are of same size, offset values are '0'. What to do now?
Copy link to clipboard
Copied
Can you share the file with us, please?
Copy link to clipboard
Copied
I have already shared the file. Please check.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
nope. not working
Copy link to clipboard
Copied
The structure of this file is completely bonkers... Probably the fault of the application that created it.
Either re-create the file, or use the page width as the y-offset. That seems to produce good results, even though it doesn't make sense.
Like this:
var cropBox = doc.getPageBox("Crop", p);
var xOffset = 0;
var yOffset = cropBox[2];
return [Number(r[4])+xOffset, Number(r[3])-yOffset, Number(r[2])+xOffset, Number(r[5])-yOffset];
Copy link to clipboard
Copied
"Can you explain what you mean by 'the page format' ? I can see page dimensions where you indicated in the picture."
Sorry, in French "page format" means "page dimensions" and so I often make this mistake.