How to Extract Image From PDF in Java

1. Introduction

Are you looking for a way to extract images from PDF files? Extracting images from PDFs has many applications in every industry, such as image archiving, printing, publication, and more. The PDF format is widely used to store and share documents and may contain images that you need to extract for further use.

In this article, we will learn how to extract images from an existing PDF document and save them in a single folder using the Java programming language. For this purpose, we will use the IronPDF Java library to extract images.

2. IronPDF Java PDF Library

IronPDF is a Java library designed to help developers generate, modify, and extract data from PDF files within their Java applications. With IronPDF, you can create PDF documents from a range of sources, such as HTML, ASPX, images, and more. Additionally, you have the ability to merge, split, and manipulate existing PDFs. IronPDF also includes security features, such as password protection and digital signatures.

Developed and maintained by IronSoftware, IronPDF is known for its ability to extract text from PDFs, HTML, and URLs. This makes it a versatile and powerful tool for a variety of applications, whether you're creating PDFs from scratch or working with existing ones.

3. Prerequisites

Before using IronPDF to extract data from a PDF file, there are a few prerequisites that must be met:

  1. Java installation: Ensure that Java is installed on your system and that its path has been set in the environment variables. If you haven't installed Java yet, follow the instructions at the following link.
  2. Java IDE: Have either Eclipse or IntelliJ installed as your Java IDE. You can download Eclipse from this link and IntelliJ from this link.
  3. IronPDF library: Download and add the IronPDF library to your project as a dependency. For setup instructions, visit the IronPDF website.
  4. Maven installation: Make sure Maven is installed and integrated with your IDE before starting the PDF conversion process. Follow the tutorial at the following link for assistance with installing and integrating Maven.

4. IronPDF for Java Installation

Installing IronPDF for Java is a straightforward process, provided all the requirements are met. In this guide, we will use the JetBrains IntelliJ IDEA to demonstrate the installation and run some sample code.

Here's what to do:

Launch IntelliJ IDEA: Open JetBrains IntelliJ IDEA on your system.

Create a Maven Project: In IntelliJ IDEA, create a new Maven project. This will provide a suitable environment for the installation of IronPDF for Java.

How to Extract Images from PDFs in Java - Figure 1

Create an New IntelliJ Maven Project

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

How to Extract Images from PDFs in Java - Figure 2

Choose a suitable name for the new Maven Project and Press Next to continue

After you click Finish, a new project will open to a pom.xml. We will use this to add the Maven dependencies of IronPDF for Java.

Next, add the following dependencies in the pom.xml file or you can download the JAR file from the following link.

<dependency>
   <groupId>com.ironsoftware</groupId>
   <artifactId>com.ironsoftware</artifactId>
   <version>2024.3.1</version>
</dependency>
XML

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

How to Extract Images from PDFs in Java - Figure 3

Click on the floating Maven icon to install all defined dependencies in the Maven project's classpath

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

5. Extract Images

You can extract images from a PDF document using IronPDF with a single method called extractAllImages. This method returns all the images available in a PDF file. After that, you can save all the extracted images to the file path of your choice using the ImageIO.write method by providing the path and format of the output image.

5.1. Extract Images from PDF document

In the example below, we will be images from a PDF document and saving it to the file system as a PNG image.

import com.ironsoftware.ironpdf.License;
import com.ironsoftware.ironpdf.PdfDocument;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

public class main {
    public static void main(String[] args) throws Exception {
        PdfDocument pdf = PdfDocument.fromFile(Paths.get("Final Project Report Craft Arena.pdf"));

        List<BufferedImage> images = pdf.extractAllImages();
        int i = 0;
        for (BufferedImage image : images) {
            ImageIO.write(image, "PNG", Files.newOutputStream(Paths.get("image" + ++i + ".png")));
        }
    }
}
JAVA

The program above opens the "Final Project Report Craft Arena.pdf" file and uses the extractAllImages() method to extract all images in the file into a list of BufferedImage objects. It then saves each new file image to separate PNG files with a unique name.

How to Extract Images from PDFs in Java - Figure 4: Image Extraction from PDF Output

The images extracted from the "Final Project Report Craft Arena.pdf" file

5.2. Extract Images from URL

In this section, we will discuss how to extract images directly from URLs. In the below code, we will first convert the URL to a PDF page and then toggle navigation to extract images from the PDF.

import com.ironsoftware.ironpdf.License;
import com.ironsoftware.ironpdf.PdfDocument;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

public class main {
    public static void main(String[] args) throws IOException {
        PdfDocument pdf = PdfDocument.renderUrlAsPdf("https://www.amazon.com/?tag=hp2-brobookmark-us-20");

        List<BufferedImage> images = pdf.extractAllImages();
        int i = 0;
        for (BufferedImage image : images) {
            ImageIO.write(image, "PNG", Files.newOutputStream(Paths.get("image" + ++i + ".png")));
        }
    }
}
JAVA

In the above code, we provided the Amazon homepage URL as an input, and it returns 74 images.

How to Extract Images from PDFs in Java - Figure 5: Image Extraction from PDF Output

The images extracted from the Amazon.com web page.

6. Conclusion

Extracting images from a PDF document can be done in Java using the IronPDF library. To install IronPDF, you need to have Java, a Java IDE (Eclipse or IntelliJ), Maven, and the IronPDF library installed and integrated with your project. The process of extracting images from a PDF document using IronPDF is simple and it requires just a single method call to extractAllImages. You can then save the images to a file path of your choice using the ImageIO.write method.

This article provides a step-by-step guide on how to extract images from a PDF document using Java and the IronPDF library. More details, including information about how to extract text from PDFs, can be found in the Extract Text Code Example.

IronPDF is a library with a commercial license, starting at $749. However, you can evaluate it in production with a free trial.