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...
ConvertPagesToImages convert = new ConvertPagesToImages(new File("/path/to/document.pdf"));
//convert.setPassword("password");
try {
if (convert.openPDFFile()) {
int pageCount = convert.getPageCount();
for (int page = 1; page <= pageCount; page++) {
BufferedImage img = convert.getPageAsImage(page);
JDeli.write(img, OutputFormat.PNG, new File("outputFolder/page" + page + ".png"));
}
}
} catch (Exception e) {
e.printStackTrace();
}
convert.closePDFfile();
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...
ConvertPagesToHiResImages convert = new ConvertPagesToHiResImages(new File("/path/to/document.pdf"));
//convert.setPassword("password");
boolean isBackgroundTransparent = false;
HashMap<Integer, Object> options = new HashMap<>();
//options.put(JPedalSettings.EXTRACT_AT_PAGE_SIZE, new String[] {"2000", "3000"}); //best fit, aspect ratio preserved
try {
if (convert.openPDFFile()) {
int pageCount = convert.getPageCount();
for (int page = 1; page <= pageCount; page++) {
BufferedImage img = convert.getPageAsHiResImage(page, isBackgroundTransparent, options);
JDeli.write(img, OutputFormat.PNG, new File("outputFolder/page" + page + ".png"));
}
}
} catch (Exception e) {
e.printStackTrace();
}
convert.closePDFfile();
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:
convert.setPageScaling(1.0f); // 72 DPI
convert.setPageScaling(1.33f); // 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.
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:
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, 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:
| 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 one of these image formats in Java, check out our related tutorials:
- Java PDF to BMP conversion (API docs)
- Java PDF to GIF conversion (API docs)
- Java PDF to HEIC conversion (API docs)
- Java PDF to JPEG conversion (API docs)
- Java PDF to PNG conversion (API docs)
- Java PDF to TIFF conversion (API docs)
- Java PDF to WebP conversion (API docs)
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
- How to rasterize PDF files in Java
- How to calculate PDF image DPI
- JPedal PDF to image conversion documentation
We bring over two decades of hands-on experience to helping you work with PDF files in Java.
