31

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
divanov
  • 5,663
  • 3
  • 27
  • 48
John Smith
  • 1,146
  • 4
  • 15
  • 35

3 Answers3

41

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.

roded
  • 446
  • 6
  • 17
Jongware
  • 21,058
  • 8
  • 43
  • 86
  • It's true that those values are approximations. The correct values (rounded to eight decimals) for A4 derived from its definition (a document with a surface of 1/16th of a m²) are 210,22410381mm for width and 297,30177875mm for height. Corresponding values in inches and points are 8,27653952";11,70479444" and 595,91084546pt;842,74519961pt respectively, for the curious. – klaar Aug 17 '17 at 14:40
  • 1
    PDRectangle source has constants for this calculation: POINTS_PER_INCH = 72; POINTS_PER_MM = 1 / (10 * 2.54f) * POINTS_PER_INCH, also paper sizes like A4,A5, etc – Daniel Hári Nov 10 '17 at 11:29
6

Coordinates in DTP points are defined as: 1 pt = 1/72 inch = 25.4/72 mm

You could write a method like this:

public float pt2mm(float pt) {
   return pt * 25.4f / 72;
}
nillas
  • 421
  • 3
  • 4
1

If the document is created using a different DPI than 72 then use the more general formula:

public float pt2mmForWeb72dpi(float pt) {
   return pt2mm(pt,72);
}
public float pt2mmForPrint300dpi(float pt) {
   return pt2mm(pt,300);
}
public float pt2mmForPrint600dpi(float pt) {
   return pt2mm(pt,600);
}
public float pt2mm(float pt, float dpi) {
   return pt * 25.4f / dpi;
}

You can find more at https://forums.indigorose.com/forum/indigo-rose-software/developer-s-den/13282-what-is-the-size-of-a4-in-px

A4 is a document format, as a screen image that's going to depend on the image resolution, for example an A4 document resized to:

  • 72 dpi (web) = 595 X 842 pixels
  • 300 dpi (print) = 2480 X 3508 pixels (This is "A4" as I know it, i.e. "210mm X 297mm @ 300 dpi")
  • 600 dpi (print) = 4960 X 7016 pixels

And so forth. FWIW document formats like A4 are described by their print dimensions (millimeters), which is a whole different thing than screen images (pixels) so that's why you don't see anyone using pixels to describe A4. :yes

raisercostin
  • 7,909
  • 3
  • 58
  • 68
  • 1
    The PDF default user space units (which this question is about and which default to a pt = 1/72") are completely unrelated to DPI bitmap image resolutions (which your answer is about). – mkl Nov 10 '17 at 13:57
  • 1
    I added this answer because I had the following problem. I have a pdf produced by a scan in which I found a image of size 2480x3508 pixels and one of 3508x4963. I didn't know how that image size is related to the A4 or A3 page size. After some struggle I realized that both where scanned at 300 dpi and one was an A4 page and the second an A3 page. – raisercostin Nov 10 '17 at 14:51
  • From what I can see of this post (https://stackoverflow.com/a/55700705/6120066). This answer seems incorrect. Would be interested to hear what @raisercostin thinks. – OutsideCoder Apr 17 '19 at 05:22