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
- Add JPedal to your class or module path. (download the trial jar).
- Create a File handle, InputStream, or URL pointing to the PDF file
- Include a password if file password protected
- Open the PDF file
- Extract the data for each page
- Close the PDF file
and the Java code to find PDF Page size…
PdfUtilities extract=new PdfUtilities(path);
extract.setPassword("password"); //if needed
int pageCount = extract.getPageCount();
for (int page = 1; page <= pageCount; page++) {
if (extract.openPDFFile()) {
float[] pageDimensions = extract.getPageDimensions(pageNum,
PageUnits.Inches, PageSizeType.CropBox);
}
}
extract.closePDFfile();
Further useful links
- Blog post explaining how MediaBox and CropBox define a PDF page size
- Javadoc link to PageSizeType class which defines whether to use MediaBox or CropBox (if in doubt use CropBox)
- Javadoc link to PageUnits class which defines sizes
- Javadoc link to PageUtilities class which documents this Utility class
The JPedal PDF library allows you to solve these problems in Java
Viewer viewer = new Viewer();
viewer.setupViewer();
viewer.executeCommand(ViewerCommands.OPENFILE, "pdfFile.pdf");
//Convenience static method (see class for additional options)
ExtractClippedImages.writeAllClippedImagesToDir("inputFileOrDirectory", "outputDir", "outputImageFormat", new String[] {"imageHeightAsFloat", "subDirectoryForHeight"});
//Convenience static method (see class for additional options)
ExtractTextAsWordList.writeAllWordlistsToDir("inputFileOrDirectory", "outputDir", -1);
//Convenience static method (see class for additional options)
ArrayList resultsForPages = FindTextInRectangle.findTextOnAllPages("/path/to/file.pdf", "textToFind");
PrintPdfPages print = new PrintPdfPages("C:/pdfs/mypdf.pdf");
if (print.openPDFFile()) {
print.printAllPages("Printer Name");
}
//Convenience static method (see class for additional options)
ExtractClippedImages.writeAllClippedImagesToDir("inputFileOrDirectory", "outputDir", "outputImageFormat", new String[] {"imageHeightAsFloat", "subDirectoryForHeight"});
Why do developers choose JPedal over alternatives?
- Actively developed commercial library with full support and no third party dependencies.
- Simple licensing options and source code access for OEM users.
- Process PDF files up to 3x faster than alternative Java PDF libraries.