Search code examples
javapdfbox

How to get height and width of a pdf while using PDF BOX


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

Solution

  • 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()