How do I get annotations coordinates relative to PDF page crop box ?

Q: We are currently have problems with particular PDFs in our system. The problem is when we try and get the x1, y1, x2, y2 coordinates from certain PDFs the values seem off. I’ve attached an example PDF which is causing a problem. Below is the code that we are using to iterate over the annotations/links and the values PDFTron is returning:

for (PageIterator itr = mPDFDocument.getPageIterator(); itr
.hasNext():wink: {

Page page = (Page) (itr.next());

//For each annotation (link)
for (int i = 0; i < page.getNumAnnots(); ++i) {
Annot annot = page.getAnnot(i);

//Check if valid
if (annot.isValid() == false) {
continue;
}
Log.log("x1: " + annot.getRect().getX1());
Log.log("x2: " + annot.getRect().getX2());
Log.log("y1: " + annot.getRect().getY1());
Log.log("y2: " + annot.getRect().getY2());
Log.log("width: " + annot.getRect().getWidth());
Log.log("height: " + annot.getRect().getHeight());
}
}


17:35:21:883 [LOG ] Issue: 3227 x1: 89.90720000000002
17:35:21:884 [LOG ] Issue: 3227 x2: 102.06700000000001
17:35:21:884 [LOG ] Issue: 3227 y1: 668.6030000000001
17:35:21:885 [LOG ] Issue: 3227 y2: 681.23
17:35:21:886 [LOG ] Issue: 3227 width: 12.15979999999999
17:35:21:886 [LOG ] Issue: 3227 height: 12.626999999999953
17:35:21:887 [LOG ] Issue: 3227 x1: 165.309
17:35:21:888 [LOG ] Issue: 3227 x2: 440.122
17:35:21:888 [LOG ] Issue: 3227 y1: 138.98700000000002
17:35:21:889 [LOG ] Issue: 3227 y2: 190.58100000000002
17:35:21:890 [LOG ] Issue: 3227 width: 274.813
17:35:21:891 [LOG ] Issue: 3227 height: 51.593999999999994
17:35:21:892 [LOG ] Issue: 3227 x1: 237.26
17:35:21:893 [LOG ] Issue: 3227 x2: 471.631
17:35:21:894 [LOG ] Issue: 3227 y1: 335.792

As you can see the bolded x2 value has already gone outside the PDF page dimensions 432 x 613. Please help, I’ve been beating my head over this one for a few days now.


A:

The problem is that on some pages in your PDF, the crop box does not match media box. By default, PDFNet (and Acrobat) show the area which is intersection of crop and media box.

If you want that your coordinates are relative to the crop box you would need to subtract crop box origin (page.getCropBox()).

Btw. a page can also have rotation attribute so things can get more tricky.

To deal with all the issues, I suggest using page.GetDefaultMatrix() and rotate all coordinates as described in the following article:

https://groups.google.com/d/topic/pdfnet-sdk/TE3Eoay3J7A/discussion

You can also invert the matrix and move coordinates in the other direction.

Hi,

I have same problem in iOS SDK.
I want to set an Image at starting co ordinates of the link annotation on page.
I am first getting page’s annotation and then getting pdfRect using [annotation GetRect] method.
I have tried with [page GetDefaultMatrix:TRUE box_type:[page GetBox:e_crop] angle:[page GetRotation]].
But not succeed.

Please let me know if any other solution is there or anything wrong using GetDefaultMatrix method.

Thanks,
Niketa.

Hello Niketa,

It seems you are passing a PDFRect object for the parameter box_type, which
is expecting a Box<http://www.pdftron.com/pdfnet/mobile/docs/iOS/src/_p_d_f_net_o_b_j_c_8h.html#a61ea4554dd95aa11cf38bafb86bb977f>enum,
as highlighted in yellow:

I have tried with [page GetDefaultMatrix:TRUE box_type:[page GetBox:e_crop]
angle:[page GetRotation]].

Try passing in e_crop instead.

James

You may also want to try the
Stamper<http://www.pdftron.com/pdfnet/mobile/docs/iOS/src/interface_stamper.html>class
which is easy to use.

If you do continue with
ElementWriter<http://www.pdftron.com/pdfnet/mobile/docs/iOS/src/interface_element_writer.html>,
make sue you are passing the correct value to the page_coord_sys parameter.

James