Extract Images from PDF via Java

Extract images from PDF document. Use Aspose.PDF for Java to modify PDF files programmatically

Extract Images from PDF Document Using Java Library

In order to add Image in PDF, we’ll use Aspose.PDF for Java API which is a feature-rich, powerful, and easy-to-use conversion API for the Java platform. You can download its latest version directly from Maven and install it within your Maven-based project by adding the following configurations to the pom.xml.

Repository

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java AP</name>
    <url>https://releases.aspose.com/java/repo/</url>
</repository>

Dependency

<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-pdf</artifactId>
<version>version of aspose-pdf API</version>
</dependency>

Extract Image from PDF via Java


You need Aspose.PDF for Java to try the code in your environment.

  1. Open PDF document.
  2. Extract a particular image.
  3. Save output image.
  4. Save updated PDF file.

Extract Images from PDF File - Java

This sample code shows how to extract Images from PDF - Java


    // 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);
    outputImage.close();

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