Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PDFBox - find page dimensions

Tags:

java

pdf

pdfbox

How can I find(in mm) the width and the height of a pdf page using PDFBox? Currently, I'm using this:

System.out.println(page.getMediaBox().getHeight()); System.out.println(page.getMediaBox().getWidth()); 

but the result is(not in mm):

842.0 595.22 
like image 246
John Smith Avatar asked Jan 03 '14 12:01

John Smith


1 Answers

Measurement units inside a PDF are in points, a traditional graphic industry measurement unit. Adobe uses the following definition:

1 pt = 1/72 inch 

and since one inch is defined to be exactly 25.4 mm (really!), you can convert from points to mm using the formula

mm = pt*25.4 / 72 

Your values, by the way, translate (loosely) to the A4 paper dimensions 210 x 297 mm. ("Loosely", for 2 reasons. First: Ax dimensions are derived from 1 square meter, in the metric system. Points are based (according to Adobe's usage) in the imperial system; therefore, all conversions between points and millimeters are approximations. Second: the given value in mm for A4 is rounded as well. Ax relative and absolute sizes are based on an irrational number.)

Footnote

Inside an object stream, units of measurement can be scaled to something else. The above is only true for top level base objects.

like image 95
Jongware Avatar answered Oct 02 '22 10:10

Jongware