Link

Convert PDF to BufferedImage

Simple example to convert PDF to BufferedImage

JPedal provides easy conversion of the pages from a PDF file or directory of PDF files into Java BufferedImages. This uses the JPedal ConvertPagesToImages class.

Example code - convert PDF file to Image using File Path

ConvertPagesToImages convert = new ConvertPagesToImages("/path/to/file.pdf");
//convert.setPassword("password"); //if password needed
try {
    if (convert.openPDFFile()) {
        int pageCount = convert.getPageCount();
        for (int page = 1; page <= pageCount; page++) {
            BufferedImage image = convert.getPageAsImage(page);
        }
    }
} catch (Exception e) {
    e.printStackTrace();
}
convert.closePDFfile(); 

Example code - convert PDF file in Memory

// Disable all caching to file to reduce memory usage
PdfFileReader.alwaysCacheInMemory = -1;
// bytes is a byte[] with the PDF file data
ConvertPagesToImages convert = new ConvertPagesToImages(bytes);
// convert.setPassword("password"); // If password needed
try {
    if (convert.openPDFFile()) {
        int pageCount = convert.getPageCount();
        for (int page = 1; page <= pageCount; page++) {
            BufferedImage image = convert.getPageAsImage(page);
        }
    }
} catch (PdfException e) {
    e.printStackTrace();
}
convert.closePDFfile();

Customizable example to convert PDF to BufferdImage

This example provides upscaling or ConvertPagesToHiResImages class has lots of additional options

Example code

ConvertPagesToHiResImages convert = new ConvertPagesToHiResImages("/path/to/file.pdf");
//convert.setPassword("password");
//convert.setPageScaling(1.33f); //which gives same size as Acrobat at 100%
HashMap options = new HashMap(); //see https://javadoc.idrsolutions.com/org/jpedal/constants/JPedalSettings.html
try {
    if (convert.openPDFFile()) {
        int pageCount = convert.getPageCount();
        for (int page = 1; page <= pageCount; page++) {
            BufferedImage image = convert.getPageAsHiResImage(page, isBackgroundTransparent, options);
        }
    }
} catch (Exception e) {
    e.printStackTrace();
}
convert.closePDFfile();

Still need help? Send us your questions.