You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by Andrew Munn <an...@nmedia.net> on 2015/03/31 04:47:34 UTC

PDFbox, converting between points and rendered pixels?

I render docs like this at 720dpi:

BufferedImage bim = pdfRenderer.renderImageWithDPI(pageNum, 720, ImageType.GRAY);

This way I can measure out fields' locations and sizes in forms using an 
image editor in pixels and divide by 10 to get points (72 points per 
inch).

I can pull out text like this:

 String textFromBox(PDDocument doc, int x, int y, int w, int h, int page){ 
        PDFTextStripperByArea stripper = new PDFTextStripperByArea();
        Rectangle rect = new Rectangle(x/10, y/10, w/10, h/10);
        stripper.addRegion("region", rect);
        System.out.println("getting fom page "+page);
        PDPage pp = (PDPage) doc.getDocumentCatalog().getPages().get(page);
        stripper.extractRegions(pp);
        String text = stripper.getTextForRegion("region");
        System.out.println("text=" + text);
        return text;
    }

Seems to work ok, but I want to overlay some drawn boxes on the form so 
I can see easily what in in the fields created.  Like this:

	http://www.topazdevelopment.com/tmp/annotated.pdf

When I plot the boxes it starts from the bottom-left, not top-left so I 
have to flip the y-axis approx like this:

 int flip(int x){
	// 11" x 72ppi = 792 but 772 works better
	return 772-x;
 }

cos.addRect(xposition / 10, flip(yposition / 10), width / 10, height / 10);

This method is working somewhat but not exactly.  The drawn boxes are 
ending up in the wrong place even though I've measured their pixel 
position correctly in the rendered image.  Is there a better way to 
accomplish this?  Can I interogate a doc for its height in points?

Thanks!



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


Re: PDFbox, converting between points and rendered pixels?

Posted by Tilman Hausherr <TH...@t-online.de>.
Am 01.04.2015 um 06:43 schrieb Andrew Munn:
>>> This method is working somewhat but not exactly.  The drawn boxes are
>>> ending up in the wrong place even though I've measured their pixel
>>> position correctly in the rendered image.  Is there a better way to
>>> accomplish this?
>> Yes, play with the CTM (current transformation matrix)... use transform() and
>> set a ctm that flips... btw, flipping is multiplying with -1 then adding.
>>
> So to convert from pixels in a 720dpi rendered PDF to points in the API do
> I need to do something like this?
>
>   Matrix m = new Matrix();
>   m.scale(0.10f, 0.10f);
>   cos.transform(m);
>
> or do I need to pass in some AffineTransform to the Matrix constructor?

Both is possible.

> Does this transform mean I no longer need the flip?

The flip was for a different reason, which you explained in a previous post.

What I mean is that it is easier to use a transform, than to do 
calculations yourself.

Tilman

>
> Thanks!
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail: users-help@pdfbox.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


Re: PDFbox, converting between points and rendered pixels?

Posted by Andrew Munn <an...@nmedia.net>.
> > This method is working somewhat but not exactly.  The drawn boxes are
> > ending up in the wrong place even though I've measured their pixel
> > position correctly in the rendered image.  Is there a better way to
> > accomplish this?
> 
> Yes, play with the CTM (current transformation matrix)... use transform() and
> set a ctm that flips... btw, flipping is multiplying with -1 then adding.
> 

So to convert from pixels in a 720dpi rendered PDF to points in the API do 
I need to do something like this?  

 Matrix m = new Matrix();
 m.scale(0.10f, 0.10f);
 cos.transform(m);

or do I need to pass in some AffineTransform to the Matrix constructor?  
Does this transform mean I no longer need the flip?

Thanks!

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


Re: PDFbox, converting between points and rendered pixels?

Posted by Tilman Hausherr <TH...@t-online.de>.
Am 31.03.2015 um 04:47 schrieb Andrew Munn:
> I render docs like this at 720dpi:
>
> BufferedImage bim = pdfRenderer.renderImageWithDPI(pageNum, 720, ImageType.GRAY);
>
> This way I can measure out fields' locations and sizes in forms using an
> image editor in pixels and divide by 10 to get points (72 points per
> inch).
>
> I can pull out text like this:
>
>   String textFromBox(PDDocument doc, int x, int y, int w, int h, int page){
>          PDFTextStripperByArea stripper = new PDFTextStripperByArea();
>          Rectangle rect = new Rectangle(x/10, y/10, w/10, h/10);
>          stripper.addRegion("region", rect);
>          System.out.println("getting fom page "+page);
>          PDPage pp = (PDPage) doc.getDocumentCatalog().getPages().get(page);
>          stripper.extractRegions(pp);
>          String text = stripper.getTextForRegion("region");
>          System.out.println("text=" + text);
>          return text;
>      }
>
> Seems to work ok, but I want to overlay some drawn boxes on the form so
> I can see easily what in in the fields created.  Like this:
>
> 	http://www.topazdevelopment.com/tmp/annotated.pdf
>
> When I plot the boxes it starts from the bottom-left, not top-left so I
> have to flip the y-axis approx like this:
>
>   int flip(int x){
> 	// 11" x 72ppi = 792 but 772 works better
> 	return 772-x;
>   }
>
> cos.addRect(xposition / 10, flip(yposition / 10), width / 10, height / 10);
>
> This method is working somewhat but not exactly.  The drawn boxes are
> ending up in the wrong place even though I've measured their pixel
> position correctly in the rendered image.  Is there a better way to
> accomplish this?

Yes, play with the CTM (current transformation matrix)... use 
transform() and set a ctm that flips... btw, flipping is multiplying 
with -1 then adding.

> Can I interogate a doc for its height in points?
PDPage.getMediaBox()

Tilman

> Thanks!
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail: users-help@pdfbox.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org