Extract Images from PDF in Java

This tutorial shows how to extract images from a PDF document using Spire.PDF for Java.

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class ExtractImages {

	public static void main(String[] args) throws IOException {

		//create a PdfDocment object
		PdfDocument doc = new PdfDocument();

		//load a PDF file
		doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\Images.pdf");

		//declare an int variable
		int index = 0;

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


			//extract images from a particular page
			for (BufferedImage image : page.extractImages()) {

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

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

Output:

Extract Images from PDF in Java