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
if (convert.openPDFFile()) {
    int pageCount = convert.getPageCount();
    for (int page = 1; page <= pageCount; page++) {
        BufferedImage image = convert.getPageAsImage(page);
    }
}
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
if (convert.openPDFFile()) {
    int pageCount = convert.getPageCount();
    for (int page = 1; page <= pageCount; page++) {
        BufferedImage image = convert.getPageAsImage(page);
    }
}
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");
HashMap options=new HashMap(); //see https://javadoc.idrsolutions.com/org/jpedal/constants/JPedalSettings.html
if (convert.openPDFFile()) {
    int pageCount=convert.getPageCount();
    for (int page=1; page<=pageCount; page++) {
        BufferedImage image=convert.getPageAsHiResImage(page, isBackgroundTransparent, options);
    }
}
convert.closePDFfile();