2

Here is a question: When I use PDF Box, I have no idea how to get height and width of a PDF file. I cannot see any method for get.

PDDocument pdDocument = getPDDocument(fis);
PDPage doc = pdDocument.getPage(0);
Abra
  • 19,142
  • 7
  • 29
  • 41
wpwing
  • 23
  • 1
  • 3

1 Answers1

3

Inside of each PDPage you have a method called getMediaBox() that returns a PDRectangle. You can obtain the height and the width.

Be careful with the UOM that represent the width and height.

https://pdfbox.apache.org/docs/2.0.0/javadocs/org/apache/pdfbox/pdmodel/PDPage.html#getMediaBox()

Abra
  • 19,142
  • 7
  • 29
  • 41
Juan Ramos
  • 557
  • 4
  • 14
  • If you want the *visible* page size, you should use the **CropBox** instead. Read [this answer](https://stackoverflow.com/a/13240546/1729265) for an overview of the boxes defined for a page. And check the page **Rotate** value, if it's 90 or 270, you have to switch width and height. – mkl Apr 18 '20 at 10:43