Extract Images from PDF in Java

·

2 min read

PDF files are easy to view and read, while the content inside it can be difficult to modify. However, it is inevitable that you may encounter a situation where you need to extract images from PDF files for other purposes. This article will share how to achieve this task programmatically using Free Spire.PDF for Java.

Import Dependency (2 methods)

Method 1: Download the free library and unzip it. Then add the Spire.Pdf.jar file to your project as a dependency.

Method 2: Directly add the jar dependency to maven project by adding the following configurations to the pom.xml.

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.pdf.free</artifactId>
        <version>5.1.0</version>
    </dependency>
</dependencies>

Sample Code

To extract images from a PDF document, you can use the PdfPageBase.extractImages() method offered by Spire.PDF for Java. Below is the complete sample code.

import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class ExtractImage {
    public static void main(String[] args) throws IOException {
        //create a PdfDocument instance
        PdfDocument doc = new PdfDocument();

        //load a PDF sample file
        doc.loadFromFile("template.pdf");

        //declare an int variable
        int index = 0;

        //loop through all pages
        for (PdfPageBase page : (Iterable<PdfPageBase>) doc.getPages()) {

            //extract images  from the given page
            for (BufferedImage image : page.extractImages()) {

                //specify the file path and name
                File output = new File("C:\\Users\\Administrator\\Desktop\\ExtractedImages\\" + String.format("Image_%d.png", index++));

                //save images as .png files
                ImageIO.write(image, "PNG", output);
            }
        }
    }
}

extractImage.jpg