Cómo Añadir Tabla a DOCX C# | IronWord

Java PDF to Image File

This article was translated from English: Does it need improvement?
Translated
View the article in English

1. Introduction

Converting PDF pages/documents to image files such as JPEG, PNG, or TIFF has many applications in the software industry. Sometimes, you need an image of specific pages of a PDF file for use elsewhere, but your only option is an image screenshot of the page. Imagine you are working on a Java project that has a functionality to load and convert a PDF page to image files and use them for other purposes. Using traditional Java code, the conversion is nearly impossible. For that purpose, we will use IronPDF for Java.

2. IronPDF for Java

IronPDF for Java is a package that allows you to create, prepare, and manage PDF files. It is popular among developers because of its PDF document generation component, which allows them to read PDFs, and generate/create and modify PDF files without the need for Adobe Acrobat. IronPDF for Java supports custom headers/footers, signatures, attachments, passwords, and security mechanisms. One of the improved performance features is full multithreading and async support. IronPDF for Java works with Maven-based projects.

Below we will discuss how to convert PDF pages to image formats like JPEG, JPG, or PNG using Java.

3. Prerequisites

Before we get started, there are a few points that must be present to carry out this conversion.

  1. Java should be installed in the system and its path should be set in Environment Variables. Please refer to this Java installation guide to install Java if you haven't already.
  2. A good Java IDE should be installed, like Eclipse or IntelliJ. To download Eclipse, please visit this Eclipse download page and to download IntelliJ, please visit this IntelliJ download page.
  3. Maven should be integrated with the IDE before starting the conversion. For the tutorial on installing Maven and integrating it into the environment, visit this Maven installation tutorial.

4. IronPDF for Java Installation

Once all the prerequisites are fulfilled, installing IronPDF for Java is simple and easy, even for new Java developers.

To use IronPDF for Java, first you need an IDE. For this article, we will use JetBrains IntelliJ IDEA to install the required dependencies and run examples.

First, open JetBrains IntelliJ IDEA and create a new Maven project.

Java PDF to Image - Figure 1: Create new Maven Project

Create a new Maven Project

A new window will appear. Enter the name of the project and click on Finish.

Java PDF to Image - Figure 2: New Project Name

New Project Name

After you click finish, a new project will open and by default, pom.xml is opened in the project which is good because we need to add Maven dependencies of IronPDF for Java.

Java PDF to Image - Figure 3: New Project

New Project

Add the following dependencies in pom.xml file or you can download the API's JAR file from the following Maven Repository page for IronPDF.

<dependencies>
    <dependency>
        <groupId>com.ironsoftware</groupId>
        <artifactId>ironpdf</artifactId>
        <version>2022.11.0</version>
    </dependency>
</dependencies>
<dependencies>
    <dependency>
        <groupId>com.ironsoftware</groupId>
        <artifactId>ironpdf</artifactId>
        <version>2022.11.0</version>
    </dependency>
</dependencies>
XML

Once you place the dependencies in the pom.xml file, a small icon will appear in the right top corner of the window.

Java PDF to Image - Figure 4: Maven Dependencies

Maven Dependencies

Click on this icon to install the Maven dependencies. This will only take a few minutes depending on your internet connection.

5. Convert PDF File to Images using IronPDF for Java

Using IronPDF for Java, converting PDFs to image formats such as JPEG is just a few lines of code. It converts the input PDF document into an output stream of images. The toBufferedImages method returns a list containing a collection of BufferedImage objects, and it is arranged in ascending order corresponding with page numbers.

Using IronPDF for Java, you can not only generate images from PDF documents, but you can also create images from URLs and HTML directly.

5.1. Converting PDF Document to Images

In this following example, we will convert the entire PDF document into images. To get started, just write the following code and run the program.

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));
        }
    }
}
JAVA

The output of the following example will be saved in the assets folder of your project (create this folder before running the program) with numbering starting from 1 and will be incremented for all the PDF pages.

Java PDF to Image - Figure 5: New Project

PDF to Images Output

5.2. Converting URL to PDF and PDF to Images

Using IronPDF for Java, you can convert HTML to PDF directly, and then convert each page of the generated PDF to images.

For the next example, we will use the Amazon website. The program below will render a page on Amazon.com into a PDF and then output each page of the PDF as images stored in an assets folder.

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));
        }
    }
}
JAVA
Java PDF to Image - Figure 6: PDF to Images Output

PDF to Images Output

Adjust the resolution of the images created from the example above by modifying these calls on the ToImageOptions instance:

rasterOptions.setImageMaxHeight(800);
rasterOptions.setImageMaxWidth(500);
rasterOptions.setImageMaxHeight(800);
rasterOptions.setImageMaxWidth(500);
JAVA

The two lines of code above adjust the width and height that each of the generated images will have when toBufferedImage is invoked.

6. Conclusion

This How-To Guide shows how to convert PDFs into images using IronPDF for Java. The PDF page images produced from IronPDF contain both the page number and the name of the document, as shown in the previous examples. IronPDF can generate images in different formats: JPEG, JPG, TIFF, and many more.

IronPDF also gives full control over the output image resolution to its users. To know more about IronPDF for Java and to access additional How-To Guides on how to manipulate PDFs using Java, please refer to the IronPDF Documentation for Java. For more information about how to convert a PDF to images, go to this IronPDF PDF to Images Example.

IronPDF for Java is free for development purposes but requires a license for commercial use. Get more additional information about the license on the IronPDF Java Licensing page.

Preguntas Frecuentes

¿Cómo puedo convertir un PDF a una imagen en Java?

Puedes usar IronPDF para Java para convertir un PDF a una imagen. Utilizando el método toBufferedImages, las páginas de PDF pueden transformarse en formatos de imagen como JPEG o PNG.

¿Cuáles son los pasos para configurar IronPDF para Java en un proyecto Maven?

Para configurar IronPDF para Java en un proyecto Maven, crea un nuevo proyecto Maven en tu IDE, agrega las dependencias de IronPDF a tu archivo pom.xml y luego usa Maven para instalar estas dependencias.

¿Puedo convertir una URL directamente a una imagen usando Java?

Sí, IronPDF para Java te permite convertir una URL a una imagen. Primero, la URL se renderiza como un PDF usando el método renderUrlAsPdf, y luego las páginas de PDF pueden convertirse a imágenes.

¿Qué formatos de imagen están disponibles al convertir páginas de PDF usando Java?

Al convertir páginas de PDF usando IronPDF para Java, puedes elegir entre varios formatos de imagen, incluyendo JPEG, PNG, JPG y TIFF.

¿Cómo cambio la resolución de las imágenes generadas a partir de PDFs en Java?

Para ajustar la resolución de las imágenes generadas a partir de PDFs en Java, usa la instancia ToImageOptions en IronPDF para establecer propiedades como ImageMaxHeight y ImageMaxWidth.

¿Es IronPDF para Java gratuito para todo tipo de uso?

IronPDF para Java es gratuito para propósitos de desarrollo. Sin embargo, se requiere una licencia comercial para uso en producción, y más detalles se pueden encontrar en el sitio web oficial de IronPDF.

¿Cuáles son los requisitos previos para usar IronPDF en un proyecto Java?

Antes de utilizar IronPDF en un proyecto Java, asegúrate de que Java esté instalado y configurado en tu entorno, y que tengas un IDE de Java como IntelliJ IDEA con integración de Maven.

Darrius Serrant
Ingeniero de Software Full Stack (WebOps)

Darrius Serrant tiene una licenciatura en Ciencias de la Computación de la Universidad de Miami y trabaja como Ingeniero de Marketing WebOps Full Stack en Iron Software. Atraído por la programación desde joven, vio la computación como algo misterioso y accesible, convirtiéndolo en el ...

Leer más
¿Listo para empezar?
Versión: 2025.11 recién lanzado