Jacob Collins Jacob is the JPedal Product Manager and specialises in PDF creation and manipulation. He also develops Salesforce backend systems and contributes to marketing and support. Outside of work, he’s a 1900‑rated chess player, guitarist, and French learner.

Java PDF to PNG conversion (Tutorial)

8 min read

Convert PDF file to PNG in Java

pdf to png java

TL;DR:

Java cannot read PDF files on its own. With JPedal you open the PDF, render each page to a BufferedImage with getPageAsImage(), then write it out with JDeli.write(). Set the resolution with setPageScaling(dpi / 72f) — 150 DPI for screen, 300 DPI for print — and pass true as the second argument to getPageAsImage() when you want a transparent background rather than white.

Why convert PDF files to PNG images?

A PDF is a vector image (so it is rendered when you display it at whatever size you specify). A PNG is a bit-mapped file of a set size with fixed values. So to convert a PDF to a PNG file we need to create a blank image and then draw the PDF onto this. Then will have the PNG file (or a TIF or JPEG – the process is the same).

This process is usually done with a PDF tool such as Acrobat however, our Java PDF library includes added functionality. There are lots of Open Source and commercial tools in most major languages. If you are using Java, it is not a function that is built into Java – you will need an external application to do this such as our JPedal PDF library. JPedal is the best Java PDF library for developers.

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.

How to convert PDF to PNG in Java

  1. Download a trial copy of JPedal and add it to your IDE.
  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

and the Java code to convert PDF to PNG…

You can read our PDF to PNG conversion guide for more information on the available conversion options and Java code examples.

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 PNG documentation gives three reference points:

convert.setPageScaling(1.0f);  // 72 DPI - the default
convert.setPageScaling(1.33f); // which gives same size as Acrobat at 100% (approx 96 DPI)
convert.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
convert.setPageScaling(targetDpi / 72f);

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

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:

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

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

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

PNG is lossless, so file size tracks the pixel count almost directly and a 300 DPI page can easily run to several megabytes. If the output is too large, reduce the DPI first, then look at PngEncoderOptions:

final PngEncoderOptions options = new PngEncoderOptions();
options.setCompressionFormat(PngCompressionFormat.NONE);
JDeli.write(bi, options, out);

PNG also has a real alpha channel, so you can render the page over transparency instead of white by passing true as the second argument:

final BufferedImage bi = convert.getPageAsImage(page, true);

Which image format should you use?

The conversion code is the same for every output format — only the OutputFormat constant and the file extension change. The trade-offs are not the same, so it is worth checking that PNG is the right choice:

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

Key points to remember

The usual reason for doing this conversion is to display the content as an image (e.g. a thumbnail on a website). However, there are things so remember:

1. Bitmapped images do not scale very well (unlike Vector formats like PDF). So you need to get the size correct. If you make it too small you will not be able to zoom in without pixelation. If you make it too big you will make the download slower and need more memory.

2. Bitmapped images do not have some of the other advantages of PDF files (like text search).  You may need to add manual functionality to your application if you wish to have things like text highlighting.

3. If you are doing it to print the PDF you will need a very large image to get a quality print at 300 or 600 dpi (what looks good on screen at 72dpi will not appear as crisp on a printout).

So long as you remember these, Java PDF to PNG conversion is a straightforward process with a tool such as JPedal.

 

Frequently asked questions

How do I get a transparent background instead of a white one?

Pass true as the second argument: convert.getPageAsImage(page, true). PNG supports a full alpha channel, so the areas of the page with no content stay transparent rather than being filled with white.

Why is my PNG file so large?

PNG is lossless, so nothing is thrown away and the file size follows the pixel count. A 300 DPI A4 page is roughly 2480 × 3508 pixels. Lower the DPI if you can, tune PngEncoderOptions, or switch to WebP or JPEG if some quality loss is acceptable.

What DPI should I use when converting a PDF to PNG?

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 PNG 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 PNG resources

Converting to a different format, or not sure which one you need? Our Java PDF to Image conversion guide covers every output format JPedal supports, and 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


Jacob Collins Jacob is the JPedal Product Manager and specialises in PDF creation and manipulation. He also develops Salesforce backend systems and contributes to marketing and support. Outside of work, he’s a 1900‑rated chess player, guitarist, and French learner.