Link
Skip to main content

Convert PDF to BufferedImage in Java

JPedal can convert PDF pages to Java BufferedImage objects for in-memory processing, display, or further manipulation. Two classes are available: ConvertPagesToImages for standard conversions, and ConvertPagesToHiResImages for upscaled output with additional control.

Requirements

Requirement Value
Minimum Java version Java 17
Third-party dependencies None

Convert PDF to BufferedImage from a file path

ConvertPagesToImages convert = new ConvertPagesToImages("inputFile.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();

Convert PDF to BufferedImage from a byte array

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

Convert PDF to hi-res BufferedImage with scaling options

ConvertPagesToHiResImages supports upscaling and additional output control via JPedalSettings.

ConvertPagesToHiResImages convert = new ConvertPagesToHiResImages("inputFile.pdf");
//convert.setPassword("password");
boolean isBackgroundTransparent = false;
HashMap<Integer, Object> options = new HashMap<>();
//options.put(JPedalSettings.EXTRACT_AT_BEST_QUALITY_MAXSCALING, 10); //Do not scale image above the specified factor
//options.put(JPedalSettings.EXTRACT_AT_PAGE_SIZE, new String[]{Integer.toString(2000), Integer.toString(3000)}); //Extract page as the specified dimensions (aspect ratio preserved so will do best fit)
//options.put(JPedalSettings.ALLOW_PAGES_SMALLER_THAN_PAGE_SIZE, Boolean.TRUE); //Extracted pages smaller than original page size is allowed
//The full list of settings can be found here 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();

We also have a demo project on our GitHub page.

Frequently Asked Questions

How do I convert a PDF page to a BufferedImage in Java?

Use the ConvertPagesToImages class. Call openPDFFile(), then call getPageAsImage(pageNumber) for each page. Page numbering starts at 1.

What is the difference between ConvertPagesToImages and ConvertPagesToHiResImages?

ConvertPagesToImages covers standard conversions. ConvertPagesToHiResImages adds upscaling and fine-grained output control via JPedalSettings. Use it when output quality at large dimensions is critical.

Can I load a PDF from a byte array and convert it to a BufferedImage?

Yes. Set PdfFileReader.alwaysCacheInMemory = -1 to disable file caching, then pass a byte[] directly to the ConvertPagesToImages constructor.

How do I set the output resolution or scale?

Use setPageScaling() with a float value. The default of 1.33f matches Acrobat’s 100% view. Pass 2.0f to double the output dimensions. For more control, use ConvertPagesToHiResImages with JPedalSettings.

Can JPedal convert password-protected PDFs to BufferedImage?

Yes. Call convert.setPassword("yourPassword") before openPDFFile().

Does JPedal require any third-party libraries for PDF to BufferedImage conversion?

No. JPedal is a 100% Java solution with no third-party dependencies, no native binaries and no external tools required.

What Java version is required?

JPedal requires Java 17 as a minimum. Details at Which Java versions does JPedal support?


Why JPedal?

  • Actively developed commercial library with full support and no third party dependencies.
  • Process PDF files up to 3x faster than alternative Java PDF libraries.
  • Simple licensing options and source code access for OEM users.

Learn more about JPedal

Start Your Free Trial


Customer Downloads

Select Download