Images play a vital role in various documents. They are helpful in conveying complex information that is difficult to present in plain text and making documents more visually appealing. In this article, we will focus on how to insert, replace or delete images in PDF documents in Java using Spire.PDF for Java.
- Insert an Image into a PDF Document
- Replace an Image with Another Image in a PDF Document
- Delete a Specific Image in a PDF Document
Install Spire.PDF for Java
First of all, you're required to add the Spire.Pdf.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.
<repositories> <repository> <id>com.e-iceblue</id> <name>e-iceblue</name> <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url> </repository> </repositories> <dependencies> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.pdf</artifactId> <version>10.9.0</version> </dependency> </dependencies>
Insert an Image into a PDF Document in Java
The following steps demonstrate how to insert an image into an existing PDF document:
- Initialize an instance of the PdfDocument class.
- Load a PDF document using PdfDocument.loadFromFile() method.
- Get the desired page in the PDF document using PdfDocument.getPages().get() method.
- Load an image using PdfImage.fromFile() method.
- Specify the width and height of the image area on the page.
- Specify the X and Y coordinates to start drawing the image.
- Draw the image on the page using PdfPageBase.getCanvas().drawImage() method.
- Save the result document using PdfDocument.saveToFile() method.
- Java
import com.spire.pdf.FileFormat; import com.spire.pdf.PdfDocument; import com.spire.pdf.PdfPageBase; import com.spire.pdf.graphics.*; public class AddImage { public static void main(String []args){ //Create a PdfDocument instance PdfDocument pdf = new PdfDocument(); pdf.loadFromFile("Input.pdf"); //Get the first page in the PDF document PdfPageBase page = pdf.getPages().get(0); //Load an image PdfImage image = PdfImage.fromFile("image.jpg"); //Specify the width and height of the image area on the page float width = image.getWidth() * 0.50f; float height = image.getHeight() * 0.50f; //Specify the X and Y coordinates to start drawing the image float x = 100f; float y = 60f; //Draw the image at a specified location on the page page.getCanvas().drawImage(image, x, y, width, height); //Save the result document pdf.saveToFile("AddImage.pdf", FileFormat.PDF); } }
Replace an Image with Another Image in a PDF Document in Java
The following steps demonstrate how to replace an image with another image in a PDF document:
- Initialize an instance of the PdfDocument class.
- Load a PDF document using PdfDocument.loadFromFile() method.
- Get the desired page in the PDF document using PdfDocument.getPages().get() method.
- Load an image using PdfImage.fromFile() method.
- Create the PdfImageHelper class.
- Use the PdfImageHelper. getImagesInfo() method to obtain a collection of all images on the page.
- Use the PdfImageHelper. replaceImage() method to replace the specified index of images in the collection.
- Save the result document using PdfDocument.saveToFile() method.
- Java
import com.spire.pdf.*; import com.spire.pdf.graphics.PdfImage; import com.spire.pdf.utilities.PdfImageHelper; import com.spire.pdf.utilities.PdfImageInfo; public class ReplaceImage { public static void main(String []args) { //Create a PdfDocument instance PdfDocument doc = new PdfDocument(); //Load a PDF document doc.loadFromFile("AddImage.pdf"); //Get the first page PdfPageBase page = doc.getPages().get(0); //Load an image PdfImage image = PdfImage.fromFile("image1.jpg"); // Get the image information from the page PdfImageHelper imageHelper = new PdfImageHelper(); PdfImageInfo[] imageInfos = imageHelper.getImagesInfo(page); // Replace Image imageHelper.replaceImage(imageInfos[0], image); //Save the result document doc.saveToFile("ReplaceImage.pdf", FileFormat.PDF); //Dispose the document doc.dispose(); } }
Delete a Specific Image in a PDF Document in Java
The following steps demonstrate how to delete an image from a PDF document:
- Initialize an instance of the PdfDocument class.
- Load a PDF document using PdfDocument.loadFromFile() method.
- Get the desired page in the PDF document using PdfDocument.getPages().get() method.
- Create the PdfImageHelper class.
- Use the PdfImageHelper. getImagesInfo() method to obtain a collection of all images on the page.
- Use the PdfImageHelper. deleteImage() method to delete specific images on the page.
- Save the result document using PdfDocument.saveToFile() method.
- Java
import com.spire.pdf.*; import com.spire.pdf.utilities.PdfImageHelper; import com.spire.pdf.utilities.PdfImageInfo; public class DeleteImage { public static void main(String []args) { //Create a PdfDocument instance PdfDocument pdf = new PdfDocument(); //Load a PDF document pdf.loadFromFile("AddImage.pdf"); //Get the first page PdfPageBase page = pdf.getPages().get(0); // Get the image information from the page PdfImageHelper imageHelper = new PdfImageHelper(); PdfImageInfo[] imageInfos = imageHelper.getImagesInfo(page); //Delete the first image on the page imageHelper.deleteImage(imageInfos[0]); //Save the result document pdf.saveToFile("DeleteImage.pdf", FileFormat.PDF); //Dispose the document pdf.dispose(); } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.