2

I'm looking to get an accurate size of each page in a PDF as part of a Unit test of PDF's I'll be creating. As I'm dealing with PDFs that have many different page sizes in each document the code returns an ArrayList of dimensions.

AFAIK each page can have its own DPI setting too.

I've done quite a bit of Googling but I've only come up with this which only gives me part of the answer, as I still need to work out what DPI each page is.

PDFBox - find page dimensions

public static ArrayList<float[]> getDimentions(PDDocument document) {
    ArrayList<float[]> dimensions = new ArrayList<>();
    float[] dim = new float[2];
    //Loop Round Each Page
    //Get Dimensions of each page and DPI
    for (int i = 0; i < document.getNumberOfPages(); i++) {
        PDPage currentPage = document.getPage(i);
        PDRectangle mediaBox = currentPage.getMediaBox();
        float height = mediaBox.getHeight();
        float width = mediaBox.getWidth();
        // How do I get the DPI now????
    }
    //Calculate Size of Page in mm  (
    //Get Dimensions of each page and DPI ( https://stackoverflow.com/questions/20904191/pdfbox-find-page-dimensions/20905166  )
    //Add size of page to list of page sizes
    //Return list of page sizes.
    return dimensions;
}
OutsideCoder
  • 326
  • 2
  • 16
  • *"AFAIK each page can have its own DPI setting too."* - PDFs don't have a DPI value, not even on a per-page level. – mkl Apr 09 '19 at 16:39
  • How do I calculate their size then if I don't know the DPI? As this [question](https://stackoverflow.com/questions/20904191/pdfbox-find-page-dimensions/20905166) seem to refer this? Is DPI only applicable for the rendered bitmaps inside the PDF? – OutsideCoder Apr 09 '19 at 20:00
  • The page dimensions (media box / crop box) are given in default userspace units which in turn default to 1/72 inch. So simply divide the box width or height by 72 to get the page width or height in inch. But this does not correspond to a DPI value because the latter is a resolution, the former merely a different measurement unit. – mkl Apr 09 '19 at 20:13
  • Thanks @mkl. That seemed to work. Much appreciated. Feel free to add that as an answer and I'll select it accordingly. – OutsideCoder Apr 15 '19 at 15:54

1 Answers1

4

The page dimensions (media box / crop box) are given in default userspace units which in turn default to 1/72 inch. So simply divide the box width or height by 72 to get the page width or height in inch.

This does not correspond to a DPI value of 72, though, because that would be a resolution value and a pdf does not have a resolution, the default userspace units merely are a different measurement unit.

mkl
  • 77,874
  • 12
  • 103
  • 212