pdfbox-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Andrew Munn <and...@nmedia.net>
Subject PDFbox, converting between points and rendered pixels?
Date Tue, 31 Mar 2015 02:47:34 GMT

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 

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);
        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:


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?


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

View raw message