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 API to Manipulate Images in PDF
- Add an Image in a PDF using Java
- Extract an Image from a PDF using Java
- Remove Images from a PDF using Java
- Replace an Image in a PDF using Java
- Get a Free License
Java API to Manipulate Images in PDF Files
Aspose.PDF for Java is a powerful API that provides you with a wide range of PDF manipulation features. The API lets you work with text, annotations, or images in the PDF files seamlessly. You can either download the API’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>21.2</version>
</dependency>
Add an Image in a PDF File using Java
The following are the steps to add an image in the PDF file using 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 image to a PDF file using 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.
Get a Free API License
You can try Aspose.PDF for Java for free by getting a temporary license.
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 explore other features of Aspose.PDF for Java using documentation. In case of any queries, you can contact us via our forum.