News Category

Java: Insert, Replace or Delete Images in PDF

2022-10-25 07:17:00 Written by  support iceblue
Rate this item
(0 votes)

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.

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.3.4</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);
    }
}

Java: Insert, Replace or Delete Images in 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.
  • Replace a specific image on the page with the loaded image using PdfPageBase.replaceImage() 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.PdfImage;

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");

        //Replace the first image on the page with the loaded image
        page.replaceImage(0, image);

        //Save the result document
        doc.saveToFile("ReplaceImage.pdf", FileFormat.PDF);
    }
}

Java: Insert, Replace or Delete Images in PDF

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.
  • Delete a specific image on the page using PdfPageBase.deleteImage() 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;

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);

        //Delete the first image on the page
        page.deleteImage(0);

        //Save the result document
        pdf.saveToFile("DeleteImage.pdf", FileFormat.PDF);
    }
}

Java: Insert, Replace or Delete Images in PDF

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.

Additional Info

  • tutorial_title:
Last modified on Thursday, 27 July 2023 07:16