Java PDF to Image File
Convert PDF documents to image formats like JPEG, PNG, or TIFF in Java using IronPDF's toBufferedImages method. Simply load a PDF file, call toBufferedImages to get a list of BufferedImage objects, then write each image to disk using ImageIO.
Quickstart: Convert PDF to Images in Java
Add IronPDF dependency to your Maven project: ```xml :title=pom.xml
com.ironsoftware ironpdf 2022.11.0 Load your PDF document:
PdfDocument pdf = PdfDocument.fromFile(Paths.get("document.pdf"));PdfDocument pdf = PdfDocument.fromFile(Paths.get("document.pdf"));JAVA- Convert to images and save:
List<BufferedImage> images = pdf.toBufferedImages(); for (int i = 0; i < images.size(); i++) { ImageIO.write(images.get(i), "PNG", new File("page_" + i + ".png")); }List<BufferedImage> images = pdf.toBufferedImages(); for (int i = 0; i < images.size(); i++) { ImageIO.write(images.get(i), "PNG", new File("page_" + i + ".png")); }JAVA
What Is PDF to Image Conversion and Why Is It Needed?
Converting PDF pages to image files like JPEG, PNG, or TIFF has many applications in software development. You may need images of specific PDF pages for use elsewhere, but your only option is taking screenshots. Traditional Java code makes this conversion nearly impossible. IronPDF solves this challenge with a straightforward approach.
PDF to image conversion is essential for many business scenarios: creating thumbnails for document management systems, generating preview images for web applications, extracting visual content for presentations, or converting documents for display on devices that don't support PDF rendering. IronPDF simplifies this complex task by providing a robust API that handles all rendering complexities internally.
How to Convert PDF to Image in Java
- Install Java library to convert PDF to various image formats
- Utilize toBufferedImages method to convert PDF to image
- Convert URL to image by first getting a PDF with renderUrlAsPdf method
- Use step 2 to convert PDF to the desired image format
- Use
writemethod to export each image
What Is IronPDF for Java and How Does It Help?
IronPDF for Java is a library that enables you to create, prepare, and manage PDF files. Developers use it to read, generate, and modify PDF files without Adobe Acrobat. IronPDF supports custom headers/footers, signatures, attachments, passwords, and security mechanisms. It offers full multithreading and async support for improved performance. IronPDF works with Maven-based projects.
The library excels at HTML to PDF conversion, allowing developers to leverage their existing HTML/CSS knowledge when creating PDFs. Beyond basic conversion, IronPDF offers advanced features like adding backgrounds and foregrounds, creating and filling forms, and applying custom watermarks. For PDF to image conversion, IronPDF provides a seamless API that maintains quality while offering flexibility in output formats.
Below we discuss how to convert PDF pages to image formats like JPEG, JPG, or PNG using Java.
What Prerequisites Do I Need Before Starting?
Before starting, ensure these prerequisites are met:
- Java installed with path set in Environment Variables. See this Java installation guide.
- Java IDE installed (Eclipse or IntelliJ). Download Eclipse or IntelliJ.
- Maven integrated with your IDE. See this Maven installation tutorial.
- License keys configured for commercial use.
How Do I Install IronPDF for Java?
Once all prerequisites are met, installing IronPDF is simple. For detailed setup instructions, consult the getting started documentation.
To use IronPDF for Java, you need an IDE. This article uses JetBrains IntelliJ IDEA to install dependencies and run examples.
First, open JetBrains IntelliJ IDEA and create a new Maven project.
A new window appears. Enter the project name and click Finish.
After clicking Finish, the new project opens with pom.xml displayed by default. We need this file to add Maven dependencies for IronPDF.
Add the following dependencies to your pom.xml file. You can also download the API JAR file from the Maven Repository page for IronPDF. For cloud deployments, check our guides for AWS, Azure, or Google Cloud.
<dependencies>
<dependency>
<groupId>com.ironsoftware</groupId>
<artifactId>ironpdf</artifactId>
<version>2022.11.0</version>
</dependency>
<!-- Optional: SLF4J logging implementation -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.36</version>
</dependency>
</dependencies><dependencies>
<dependency>
<groupId>com.ironsoftware</groupId>
<artifactId>ironpdf</artifactId>
<version>2022.11.0</version>
</dependency>
<!-- Optional: SLF4J logging implementation -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.36</version>
</dependency>
</dependencies>Once you add the dependencies to pom.xml, a small icon appears in the top right corner.
Click this icon to install the Maven dependencies. Installation takes a few minutes depending on your internet connection. After installation, explore the complete API documentation to understand all available features.
How Do I Convert PDF Files to Images Using IronPDF?
Using IronPDF for Java, converting PDFs to image formats like JPEG requires just a few lines of code. It converts the input PDF document into an output stream of images. The toBufferedImages method returns a list of BufferedImage objects arranged in ascending order by page number.
IronPDF converts PDF documents to images and can also create images directly from URLs and HTML. This flexibility makes it ideal for various use cases, from creating document previews to generating thumbnails for content management systems.
How Do I Convert an Existing PDF Document to Images?
This example converts an entire PDF document into images. Write the following code and run the program. For more PDF to image conversion examples, visit our rasterization code examples.
import com.ironsoftware.ironpdf.PdfDocument;
import com.ironsoftware.ironpdf.edit.PageSelection;
import com.ironsoftware.ironpdf.image.ToImageOptions;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.List;
public class Main {
public static void main(String [] args) throws IOException {
// Load the PDF document from a file
PdfDocument instance = PdfDocument.fromFile(Paths.get("business_plan.pdf"));
// Convert the PDF pages into a list of BufferedImage objects
List<BufferedImage> extractedImages = instance.toBufferedImages();
// Set image conversion options
ToImageOptions rasterOptions = new ToImageOptions();
rasterOptions.setImageMaxHeight(800);
rasterOptions.setImageMaxWidth(500);
// Convert the pages using the specified options
List<BufferedImage> sizedExtractedImages = instance.toBufferedImages(rasterOptions, PageSelection.allPages());
int pageIndex = 1;
// Loop through each image and write to the file system
for (BufferedImage extractedImage : sizedExtractedImages) {
String fileName = "assets/images/" + pageIndex++ + ".png";
ImageIO.write(extractedImage, "PNG", new File(fileName));
}
}
}import com.ironsoftware.ironpdf.PdfDocument;
import com.ironsoftware.ironpdf.edit.PageSelection;
import com.ironsoftware.ironpdf.image.ToImageOptions;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.List;
public class Main {
public static void main(String [] args) throws IOException {
// Load the PDF document from a file
PdfDocument instance = PdfDocument.fromFile(Paths.get("business_plan.pdf"));
// Convert the PDF pages into a list of BufferedImage objects
List<BufferedImage> extractedImages = instance.toBufferedImages();
// Set image conversion options
ToImageOptions rasterOptions = new ToImageOptions();
rasterOptions.setImageMaxHeight(800);
rasterOptions.setImageMaxWidth(500);
// Convert the pages using the specified options
List<BufferedImage> sizedExtractedImages = instance.toBufferedImages(rasterOptions, PageSelection.allPages());
int pageIndex = 1;
// Loop through each image and write to the file system
for (BufferedImage extractedImage : sizedExtractedImages) {
String fileName = "assets/images/" + pageIndex++ + ".png";
ImageIO.write(extractedImage, "PNG", new File(fileName));
}
}
}The output saves to your project's assets folder (create this folder before running the program) with numbering starting from 1 and incrementing for each PDF page. Apply advanced features like compressing images or adding watermarks to output files.
How Do I Convert a URL to PDF and Then to Images?
IronPDF converts HTML to PDF directly, then converts each page of the generated PDF to images. This feature allows you to capture web content as images, useful for archiving web pages, creating screenshots for documentation, or generating visual reports from web-based dashboards.
This example uses the Amazon website. The program renders an Amazon.com page into a PDF, then outputs each PDF page as images stored in an assets folder. For secure pages requiring authentication, see our guide on website and system logins.
import com.ironsoftware.ironpdf.PdfDocument;
import com.ironsoftware.ironpdf.edit.PageSelection;
import com.ironsoftware.ironpdf.image.ToImageOptions;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.List;
public class Main {
public static void main(String [] args) throws IOException {
// Generate a PDF from a URL
PdfDocument pdf = PdfDocument.renderUrlAsPdf("https://www.amazon.com/?tag=hp2-brobookmark-us-20");
// Convert the PDF pages into a list of BufferedImage objects
List<BufferedImage> extractedImages = pdf.toBufferedImages();
// Set image conversion options
ToImageOptions rasterOptions = new ToImageOptions();
rasterOptions.setImageMaxHeight(800);
rasterOptions.setImageMaxWidth(500);
// Convert the pages using the specified options
List<BufferedImage> sizedExtractedImages = pdf.toBufferedImages(rasterOptions, PageSelection.allPages());
int pageIndex = 1;
// Loop through each image and write to the file system
for (BufferedImage extractedImage : sizedExtractedImages) {
String fileName = "assets/images/" + pageIndex++ + ".png";
ImageIO.write(extractedImage, "PNG", new File(fileName));
}
}
}import com.ironsoftware.ironpdf.PdfDocument;
import com.ironsoftware.ironpdf.edit.PageSelection;
import com.ironsoftware.ironpdf.image.ToImageOptions;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.List;
public class Main {
public static void main(String [] args) throws IOException {
// Generate a PDF from a URL
PdfDocument pdf = PdfDocument.renderUrlAsPdf("https://www.amazon.com/?tag=hp2-brobookmark-us-20");
// Convert the PDF pages into a list of BufferedImage objects
List<BufferedImage> extractedImages = pdf.toBufferedImages();
// Set image conversion options
ToImageOptions rasterOptions = new ToImageOptions();
rasterOptions.setImageMaxHeight(800);
rasterOptions.setImageMaxWidth(500);
// Convert the pages using the specified options
List<BufferedImage> sizedExtractedImages = pdf.toBufferedImages(rasterOptions, PageSelection.allPages());
int pageIndex = 1;
// Loop through each image and write to the file system
for (BufferedImage extractedImage : sizedExtractedImages) {
String fileName = "assets/images/" + pageIndex++ + ".png";
ImageIO.write(extractedImage, "PNG", new File(fileName));
}
}
}Adjust image resolution by modifying these calls on the ToImageOptions instance:
rasterOptions.setImageMaxHeight(800);
rasterOptions.setImageMaxWidth(500);rasterOptions.setImageMaxHeight(800);
rasterOptions.setImageMaxWidth(500);These lines adjust the width and height of generated images when toBufferedImage is invoked. For advanced rendering options and settings, consult our guide on PDF generation settings.
Converting Specific Pages to Images
Sometimes you only need specific pages rather than the entire document. IronPDF provides flexible page selection options:
// Convert only page 3 to an image
List<BufferedImage> singlePage = instance.toBufferedImages(rasterOptions, PageSelection.singlePage(3));
// Convert pages 2 through 5
List<BufferedImage> pageRange = instance.toBufferedImages(rasterOptions, PageSelection.pageRange(2, 5));
// Convert first and last pages only
PageSelection customPages = new PageSelection();
customPages.add(0); // First page (0-indexed)
customPages.add(instance.getPageCount() - 1); // Last page
List<BufferedImage> selectedPages = instance.toBufferedImages(rasterOptions, customPages);// Convert only page 3 to an image
List<BufferedImage> singlePage = instance.toBufferedImages(rasterOptions, PageSelection.singlePage(3));
// Convert pages 2 through 5
List<BufferedImage> pageRange = instance.toBufferedImages(rasterOptions, PageSelection.pageRange(2, 5));
// Convert first and last pages only
PageSelection customPages = new PageSelection();
customPages.add(0); // First page (0-indexed)
customPages.add(instance.getPageCount() - 1); // Last page
List<BufferedImage> selectedPages = instance.toBufferedImages(rasterOptions, customPages);What Are the Key Takeaways from This Guide?
This guide demonstrates how to convert PDFs into images using IronPDF for Java. The generated images preserve page numbers and document names from the original PDF. IronPDF supports multiple image formats including JPEG, JPG, TIFF, and more.
IronPDF gives you full control over output image resolution. For more information on PDF manipulation using Java, refer to the IronPDF Documentation for Java. For additional PDF to image conversion details, see this IronPDF PDF to Images Example.
IronPDF for Java is free for development but requires a license for commercial use. Learn more about licensing on the IronPDF Java Licensing page.
Frequently Asked Questions
How do I convert a PDF file to PNG images in Java?
You can convert PDF files to PNG images using IronPDF's toBufferedImages method. First, load your PDF document using PdfDocument.fromFile(), then call toBufferedImages() to get a list of BufferedImage objects representing each page. Finally, use ImageIO.write() to save each BufferedImage as a PNG file.
What image formats are supported for PDF conversion?
IronPDF supports converting PDF documents to various image formats including JPEG, PNG, and TIFF. The toBufferedImages method returns BufferedImage objects that can be saved in any format supported by Java's ImageIO class.
Can I convert specific pages of a PDF to images instead of the entire document?
Yes, IronPDF allows you to convert specific pages by accessing individual BufferedImage objects from the list returned by toBufferedImages(). You can iterate through only the pages you need and save them as separate image files.
What are the common use cases for PDF to image conversion?
IronPDF's PDF to image conversion is commonly used for creating thumbnails in document management systems, generating preview images for web applications, extracting visual content for presentations, and converting documents for display on devices that don't support PDF rendering.
How do I add IronPDF to my Maven project?
To add IronPDF to your Maven project, include the following dependency in your pom.xml file:
Can I convert a URL directly to an image?
Yes, you can convert a URL to an image using IronPDF by first rendering the URL as a PDF using the renderUrlAsPdf method, then converting that PDF to images using the toBufferedImages method.
Does the PDF to image conversion maintain quality?
IronPDF maintains high quality during PDF to image conversion by handling all rendering complexities internally. The library provides a robust API that ensures the converted images retain the visual fidelity of the original PDF pages.













