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

784 views
Skip to first unread message

Support

unread,
Feb 14, 2012, 6:41:38 PM2/14/12
to pdfne...@googlegroups.com
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();) {

        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.

 

 



Niketa Bamta

unread,
Mar 18, 2014, 9:18:28 AM3/18/14
to pdfne...@googlegroups.com
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.

James Borthwick

unread,
Mar 18, 2014, 3:38:19 PM3/18/14
to PDFTron PDFNet SDK on behalf of Niketa Bamta
Hello Niketa,

It seems you are passing a PDFRect object for the parameter box_type, which is expecting a Box 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 received this message because you are subscribed to the "PDFTron PDFNet SDK" group (http://www.pdftron.com/pdfnet/forum.html).
---
You received this message because you are subscribed to the Google Groups "PDFTron PDFNet SDK" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pdfnet-sdk+...@googlegroups.com.
To post to this group, send email to pdfne...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

James Borthwick

unread,
Mar 19, 2014, 11:56:37 AM3/19/14
to PDFTron PDFNet SDK on behalf of Niketa Bamta
You may also want to try the Stamper class which is easy to use.

If you do continue with ElementWriter, make sue you are passing the correct value to the page_coord_sys parameter.

James
Reply all
Reply to author
Forward
0 new messages