Images are widely used for different types of depictions and demonstrations in PDF files. In this article, you will learn how to manipulate images in PDF files programmatically. Particularly, the article will cover how to add, extract, remove or replace images in PDF files using Java.
- Java Library to Add Images in PDF
- Add an Image in a PDF in Java
- Extract an Image from a PDF in Java
- Remove Images from a PDF in Java
- Replace an Image in a PDF in Java
- Get a Free License
Java Library to Add Images in PDF
To add and manipulate images in PDF, we will use Aspose.PDF for Java. It is a powerful library that provides you with a wide range of PDF manipulation features. The library lets you work with text, annotations, or images in PDF files seamlessly.
You can either download the library’s JAR or install it using the following Maven configurations.
<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 Add Image to a PDF in Java
The following are the steps to add an image in the PDF file in Java.
- First, create an instance of the Document class to load the PDF document.
- Get the Page you want to add an image to using Document.getPages().get_Item(int) method.
- Load the image file into a FileInputStream object.
- Add the image into the page’s resources using Page.getResources().getImages().add(FileInputStream) method.
- Use operators to place the image on the page:
- GSave operator to save the current graphical state.
- ConcatenateMatrix operator to specify where the image is to be placed.
- Do operator to draw the image on the page.
- GRestore operator to save the updated graphical state.
- Finally, save the updated PDF file using Document.save(string) method.
The following code sample shows how to add an image to a PDF in Java.
Extract Images from a PDF File using Java
The following are the steps to extract images from a PDF document using Java.
- Create an instance of the Document class to load the PDF document.
- Extract desired image into XImage object using Document.getPages().get_Item(int).getResources().getImages().get_Item(int) method.
- You can also loop through the image collection to extract and save all the images.
- Finally, save the extracted image as a file using OutputStream.
The following code sample shows how to extract images from PDF files using Java.
Remove Images from a PDF File using Java
The following are the steps to remove an image from a PDF file using Java.
- Load the PDF file into a Document object.
- Delete the desired image(s) using one of the following methods.
- delete() to delete images from collection.
- delete(int index) to delete an image from the collection by index.
- delete(String name) to delete an image from the collection by name.
- Finally, save the updated PDF file using the Document.save(string) method.
The following code sample shows how to delete an image in PDF using Java.
Replace an Image in a PDF File using Java
The following are the steps to replace an image in a PDF file using Java.
- Load the PDF file into a Document object.
- Load the new image into FileInputStream object.
- Use Document.getPages().get_Item(int).getResources().getImages().replace(int, FileInputStream) method to replace the image by specifying the index.
- Finally, save the updated PDF file using the Document.save(string) method.
The following code sample shows how to replace an image in PDF using Java.
Free Java PDF Library to Add Images
You can get a free temporary license and work with PDF images without any limitations.
Explore Java PDF Library
You can explore other features of Java PDF library using documentation. In case of any queries, you can contact us via our forum.
Conclusion
In this article, you have learned how to manipulate images in PDF files using Java. The step-by-step guide and code samples have shown how to extract, add, remove, and replace images in a PDF file. You can easily use the library and provided code samples in your Java applications.