While parsing the PDF files, you may need to extract images along with text from the documents. To perform this operation programmatically, this article covers how to extract images from a PDF in Java. The steps by step guide along with API references and code sample demonstrate the complete image extraction procedure.
Java Library to Extract Images from PDF
To extract images from PDF, we’ll use Aspose.PDF for Java. It is a powerful PDF manipulation library that provides a wide range of features to create and process PDF files. You can either download the library or install it using the following Maven configuration.
<repository> <id>AsposeJavaAPI</id> <name>Aspose Java API</name> <url>https://repository.aspose.com/repo/</url> </repository>
<dependency> <groupId>com.aspose</groupId> <artifactId>aspose-pdf</artifactId> <version>22.12</version> </dependency>
How to Extract Images from a PDF in Java
The following are the steps that we will follow to extract images from a PDF file.
- Load the PDF file.
- Loop through the pages in the PDF.
- Extract images one by one.
- Save the extracted images.
The following section demonstrates how to write code for PDF image extraction in Java.
Java PDF Image Extraction
The following are the steps to extract images from a PDF document using Java.
- Load the PDF document using the Document class.
- Iterate through the page collection of the document returned by Document.getPages() method.
- For each Page, loop through the collection of XImage it has using Page.getResources().getImages() method.
- Create an object of FileOutputStream to save each image.
The following code sample shows image extraction from a PDF in Java.
Free Java PDF Image Extraction
You can get a free temporary license to extract images from PDF without any limitations.
Explore Java PDF Library
In various cases, images are required to be extracted from PDF documents. Therefore, in this article, you have learned how to extract images from PDF files in Java. We have demonstrated how easy it is to process a PDF file and then extract images in a few simple steps. You can easily integrate the provided code sample into your applications and perform PDF image extraction.