In order to extract Image from 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>
<id>AsposeJavaAPI</id>
<name>Aspose Java AP</name>
<url>https://releases.aspose.com/java/repo/</url>
</repository>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-pdf</artifactId>
<version>version of aspose-pdf API</version>
</dependency>
Extract Image from PDF using Java
You need Aspose.PDF for Java to try the code in your environment.
- Open PDF document.
- Extract a particular image.
- Save output image.
- Save updated PDF file.
Extract Images from PDF File - Java
try {
// Open document
Document pdfDocument = new Document(DATA_DIR.resolve("ExtractImages.pdf").toString());
// Extract a particular image
XImage xImage = pdfDocument.getPages().get_Item(1).getResources().getImages().get_Item(1);
FileOutputStream outputImage = new FileOutputStream(DATA_DIR.resolve("output.jpg").toString());
// Save output image
xImage.save(outputImage, ImageType.getJpeg());
outputImage.close();
// Save updated PDF file
pdfDocument.save(DATA_DIR.resolve("ExtractImages_out.pdf").toString());
} catch (Exception e) {
System.out.println(e.getMessage());
}