Q: How can I list and export the resource images contained in a PDF document?

A: Here is a sample Java code to list all images embedded in a PDF document and save / export them using Qoppa’s jPDFProcess library. This example shows how to save the images in png format but it can simply be changed to export to a different format such as jpeg / jpg, etc..

In version v2016R1, a new package was made available to Qoppa’s jPDFProcess public API called com.qoppa.pdf.resources. This package contains interfaces allowing to get information about resources contained in a PDF, including IResourceManager, IFontResource, IImageResource.

package sample;
 
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.List;
 
import javax.imageio.ImageIO;
 
import com.qoppa.pdf.PDFException;
import com.qoppa.pdf.resources.IFontResource;
import com.qoppa.pdf.resources.IImageResource;
import com.qoppa.pdf.resources.IResourceManager;
import com.qoppa.pdfProcess.PDFDocument;
 
public class AccessDocumentResources
{
	public static void main(String[] args)
	{
		try
		{
			PDFDocument pdfDoc = new PDFDocument("C:/sample/in.pdf", null);
			IResourceManager resourceManager = pdfDoc.getResourceManager();
 
			// List the images from the document
			System.out.println("List images:");
			List<? extends IImageResource> imgList = resourceManager.listImages();
 
			File saveDir = new File("C:/sample/out");
			int count = 0;
			for (IImageResource imgRes : imgList)
			{
				String imgName = "image_" + (++count);
				System.out.println("\t" + imgName + ": width: " + imgRes.getWidth() + " height: " + imgRes.getHeight());
 
				// Save the image to a file
				BufferedImage bufferedImg = imgRes.getImage();
				ImageIO.write(bufferedImg, "png", new File(saveDir, imgName + ".png"));
			}
 
		}
		catch (PDFException ex)
		{
			ex.printStackTrace();
		}
		catch (IOException ex)
		{
			ex.printStackTrace();
		}
	}
}