Extract Images from PDF File

[ ]

Each page holds a Resources collection, and this, in turn, holds the Images collection, were all images in a page are kept. The XImage object gets a given image in the Images collection.

To extract and image from a page:

Get the image from the Images collection using the image index. Use the XImage object’s save(..) method to save the extracted image.

The following code snippet shows you how to extract images from the PDF file.

package com.aspose.pdf.examples;

import java.io.FileOutputStream;
import java.io.IOException;

import com.aspose.pdf.*;
import com.aspose.pdf.internal.html.rendering.image.ImageFormat;

public class ExampleExtractImages {

    private static String _dataDir = "/home/admin1/pdf-examples/Samples/";

    public static void ExtractImages() throws IOException {

        // Open document
        Document pdfDocument = new Document(_dataDir + "ExtractImages.pdf");

        // Extract a particular image
        XImage xImage = pdfDocument.getPages().get_Item(1).getResources().getImages().get_Item(1);

        FileOutputStream outputImage = new FileOutputStream(_dataDir + "output.jpg");

        // Save output image
        xImage.save(outputImage, ImageFormat.Jpeg);

        // Save updated PDF file
        pdfDocument.save(_dataDir + "ExtractImages_out.pdf");