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.

Convert PDF to Image in Java (PNG, JPEG, TIFF, WebP and More)

7 min read

Java PDF to Image conversion

To convert PDF to image in Java you need a PDF library, because the JDK cannot read PDF files. With JPedal, the pure Java PDF library from IDRsolutions, you open the PDF, render each page to a BufferedImage, then save it as an image file. ConvertPagesToImages is the fast, standard quality path and ConvertPagesToHiResImages the slower, higher quality one.

TL;DR

Set the resolution with setPageScaling(dpi / 72f): 150 DPI for screen, 300 DPI for print. Write the BufferedImage with JDeli.write().

This tutorial shows you how to convert PDF to image using Java with our JPedal Java PDF library. 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. For a product overview, see JPedal's PDF to image renderer for Java.

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.

Convert PDF to image in Java: code for both methods

Method 1: fast and standard quality image

  • Download the JPedal trial jar
  • Create a File handle, byte array or file path pointing to the PDF file
  • Include a password if the file is password protected
  • Open the PDF file
  • Iterate over the pages and write each one out
  • Close the PDF file

and here is the Java code to convert PDF to image...

The conversion code is the same for every output format. Only the OutputFormat constant and the file extension change.

Method 2: slower and very high quality image

  • Download the JPedal trial jar
  • Create a File handle, byte array or file path pointing to the PDF file
  • Include a password if the file is password protected
  • Set conversion options for output defined in JPedalSettings
  • Open the PDF file
  • Iterate over the pages
  • 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:

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:

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

setPageScaling(), setFitToSize() and setPageRange() 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:

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

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, which is 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:

FormatCompressionTransparencyMulti-pageBest for
PNGLosslessYesNoText-heavy pages, thumbnails, screenshots, anything that will be zoomed
JPEGLossyNoNoPhotographic or scanned pages where file size matters more than sharp text
TIFFLossless (LZW, Deflate) or JPEGYesYesArchival and document workflows, and whole documents in a single file
GIFLossless, 256 colours only1-bit onlyYes (animation)Legacy pipelines only. The colour limit makes it a poor fit for PDF pages
BMPNone (uncompressed)NoNoWindows interop and legacy tooling that cannot read anything else
WebPLossy or losslessYesNoWeb delivery. Usually smaller than both PNG and JPEG
HEICLossy or losslessYesYesApple ecosystems and the smallest lossy files

Related tutorials on PDF to image conversion

If you want to convert a PDF file directly to one of these image formats in Java, check out our related tutorials:

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. 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.