Site iconJava PDF Blog

How to find PDF page size in Java

jpedal

PDF files are not directly supported by Java. This tutorial shows you how to extract PDF page size (height and width) from a PDF file in simple steps using JPedal Java PDF library. The page size can be defined in Centimetres, Inches and pixels.

Why use a third-party library to handle PDF files?

PDF files are a very complex binary/text hybrid data structure which is a subset of the even more complicated Postscript format. In this example, we will use our JPedal Java PDF library to make this task simple.

How to find PDF page size in Java

  1. Create a File handle, InputStream, or URL pointing to the PDF file
    PdfUtilities extract=new PdfUtilities(path);
  2. Include a password if file password protected
    extract.setPassword("password");
  3. Open the PDF file
    if (extract.openPDFFile()) {
  4. Extract the data for each page
    if (extract.openPDFFile()) {
          float[] pageDimensions = extract.getPageDimensions(pageNum, 
          PageUnits.Inches, PageSizeType.CropBox);
    }
    }
  5. Close the PDF file
     extract.closePDFfile();
    

Further useful links