Mark Stephens Mark founded the company and has worked with Java and PDF since 1997. The original creator of the core code, he is also a NetBeans enthusiast who enjoys speaking at conferences and reading. He holds an Athletics Blue and an MA in Mediaeval History from St. Andrews University.

Java PDF to Image conversion (Tutorial)

7 min read

Java PDF to Image conversion

TL;DR:

Java has no built-in PDF support, so converting a page to an image needs a PDF library. With JPedal you open the PDF, render each page to a BufferedImage, then write it out with JDeli.write() as PNG, JPEG, TIFF, GIF, BMP, WebP or HEIC. ConvertPagesToImages is the fast, standard-quality path and ConvertPagesToHiResImages the slower, higher-quality one. Set the resolution with setPageScaling(dpi / 72f) — 150 DPI for screen, 300 DPI for print.

Do you need to convert your PDF files to images in Java? PDF files are not directly supported but you can solve this problem very easily with a third-party PDF library for Java.

This tutorial shows you how to convert PDF to Image using Java with our popular JPedal Java PDF library. JPedal is the best Java PDF library for developers. You can convert a PDF file to a BufferedImage in Java or save it directly to BMP, GIF, HEIC, JPEG, JPEG2000, PNG, TIFF, or WebP.

Add JPedal to your project

JPedal is published to the IDRsolutions Maven repository. Add the repository and the dependency to your build, or download the trial jar and put it on your class or module path.

Maven

<repositories>
    <repository>
        <id>IDRsolutions</id>
        <name>IDR Solutions</name>
        <url>https://maven.idrsolutions.com</url>
    </repository>
</repositories>

<dependency>
    <groupId>com.idrsolutions</groupId>
    <artifactId>jpedal-trial</artifactId>
    <version>YYYY.MM</version>
</dependency>

Gradle

repositories {
    maven { url 'https://maven.idrsolutions.com' }
}

dependencies {
    implementation 'com.idrsolutions:jpedal-trial:YYYY.MM'
}

The repository needs your trial token or customer credentials in ~/.m2/settings.xml — see the Maven setup guide. Paid releases use the jpedal artifact in place of jpedal-trial.

Java PDF to image conversion (Method 1 – fast and standard quality image)

  1. Download JPedal trial jar.
  2. Create a File handle, InputStream or URL pointing to the PDF file
  3. Include a password if file password protected
  4. Open the PDF file
  5. Iterate over the pages
  6. Close the PDF file

and here is the Java code to convert PDF to Image…

Java PDF to image conversion (Method 2 – slower and very high quality image)

  1. Download JPedal trial jar.
  2. Create a File handle, InputStream or URL pointing to the PDF file
  3. Include a password if file password protected
  4. Set conversion options for output defined in JPedalSettings
  5. Open the PDF file
  6. Iterate over the pages
  7. Close the PDF file

and here is the Java code for PDF to Image conversion…

Our PDF to image conversion guide explains the different image formats you can generate and includes Java examples for each.

Controlling the resolution and DPI of the output

This is the setting that matters most, and the one that is easiest to get wrong. Output resolution is controlled with setPageScaling(). The PDF to image documentation gives three reference points:

extract.setPageScaling(1.0f);  // 72 DPI - the default
extract.setPageScaling(1.33f); // which gives same size as Acrobat at 100% (approx 96 DPI)
extract.setPageScaling(2.0f);  // 144 DPI

Scaling and resolution are directly proportional, because a PDF page is defined in points at 72 points per inch. For any other target, the scaling factor is targetDpi / 72:

final int targetDpi = 300;   // 150 DPI -> 2.08f, 300 DPI -> 4.17f, 600 DPI -> 8.33f
extract.setPageScaling(targetDpi / 72f);

150 DPI is a reasonable default for screen and web output, and 300 DPI is the usual minimum for print.

setPageScaling() and setFitToSize() are methods on ConvertPagesToImages (Method 1). ConvertPagesToHiResImages (Method 2) is configured differently: it takes a map of JPedalSettings values, which is where you set the output size for the high quality path.

If you need a fixed output size instead — for thumbnails, for example — use setFitToSize(). JPedal preserves the aspect ratio and fits the page within the given dimensions:

extract.setFitToSize(new int[] {300,400});

You can also restrict the conversion to particular pages rather than rendering the whole document:

extract.setPageRange(new PageRanges("1-5,8:10,15"));

Watch the memory cost. Scaling applies in both directions, so doubling the DPI quadruples the pixel count. An A4 page is about 595 × 842 pixels at 72 DPI, but roughly 2480 × 3508 at 300 DPI — around 35 MB as an uncompressed 32-bit BufferedImage. Render and write one page at a time rather than collecting a whole document in a list.

Which image format should you use?

JPedal renders the page; the output format is chosen when you write the BufferedImage out. The trade-offs are different for each one:

Format Compression Transparency Multi-page Best for
PNG Lossless Yes No Text-heavy pages, thumbnails, screenshots, anything that will be zoomed
JPEG Lossy No No Photographic or scanned pages where file size matters more than sharp text
TIFF Lossless (LZW, Deflate) or JPEG Yes Yes Archival and document workflows, and whole documents in a single file
GIF Lossless, 256 colours only 1-bit only Yes (animation) Legacy pipelines only — the colour limit makes it a poor fit for PDF pages
BMP None (uncompressed) No No Windows interop and legacy tooling that cannot read anything else
WebP Lossy or lossless Yes No Web delivery — usually smaller than both PNG and JPEG
HEIC Lossy or lossless Yes Yes Apple ecosystems and the smallest lossy files

Related tutorials on PDF to Image Conversion

If you want to convert a PDF file directly to any of these image formats in Java, check out our related tutorials. Here we document how to

Frequently asked questions

Which method should I use, ConvertPagesToImages or ConvertPagesToHiResImages?

Start with ConvertPagesToImages. It is the faster path and is good enough for thumbnails, previews and most screen output. Move to ConvertPagesToHiResImages when you need the highest possible fidelity, for example for print output or archival capture, and can accept that it is slower and uses more memory.

Which image format should I convert a PDF page to?

PNG for anything text-heavy or that will be zoomed, because it is lossless and supports transparency. JPEG for scanned or photographic pages where size matters more than sharp text. TIFF when you need a whole document in one file. WebP for web delivery. GIF and BMP only when something downstream requires them.

What DPI should I use when converting a PDF to an image?

150 DPI is a good default for screen and web use, and 300 DPI is the usual minimum for print. Set it with convert.setPageScaling(targetDpi / 72f), because PDF pages are defined at 72 points per inch. Remember that the pixel count grows with the square of the scaling factor.

Can I convert only some of the pages?

Yes. Call convert.setPageRange(new PageRanges("1-5,8:10,15")) before you open the file. Without it, every page in the document is rendered.

How do I convert a password-protected PDF?

Call convert.setPassword("password") before openPDFFile(). The same applies to both the owner and the user password.

Can Java convert a PDF to an image without a third-party library?

No. The JDK has no PDF support at all — ImageIO can write image files, but nothing in the standard library can parse or render a PDF page. You need a PDF library such as JPedal, Apache PDFBox or a Ghostscript wrapper to produce the BufferedImage in the first place.

Other useful PDF to image resources

We bring over two decades of hands-on experience to helping you work with PDF files in Java.



The JPedal PDF library allows you to solve these problems in Java


Mark Stephens Mark founded the company and has worked with Java and PDF since 1997. The original creator of the core code, he is also a NetBeans enthusiast who enjoys speaking at conferences and reading. He holds an Athletics Blue and an MA in Mediaeval History from St. Andrews University.