Add, Extract, Replace and Remove Images in PDF in Java
Tags: java, pdf, image
Published: 2021-06-02 07:57:57

---
title: Add, Extract, Replace and Remove Images in PDF in Java
published: true
description: This article illustrates how to add, extract, replace and remove images in PDF in Java.
tags: Java, PDF, image
//cover_image: https://direct_url_to_image.jpg
---
Images make your document more readable and eye-catching. In this article, I am going to show you how to work with images in PDF, particularly, add, extract, replace and remove images in PDF in Java using [Free Spire.PDF for Java](https://www.e-iceblue.com/Introduce/free-pdf-for-java.html) API.
##Maven dependencies##
To begin with, you need to specify the dependencies for Free Spire.PDF for Java API in your maven project’s pom.xml file.
```java
<repositories>
        <repository>
            <id>com.e-iceblue</id>
            <name>e-iceblue</name>
            <url> http://repo.e-iceblue.com/nexus/content/groups/public/</url>
        </repository>
    </repositories>
    <dependencies>
        <dependency>
            <groupId> e-iceblue </groupId>
            <artifactId>spire.pdf.free</artifactId>
            <version>4.4.1</version>
        </dependency>        
    </dependencies>
```
Now let's start coding.

##Add image##
```java
import com.spire.pdf.FileFormat;
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.graphics.PdfImage;

public class AddImage {
    public static void main(String []args){
        //Create a PdfDocument instance
        PdfDocument pdf = new PdfDocument();
        //Add one page to the pdf file
        PdfPageBase page = pdf.getPages().add();

        //Load an image
        PdfImage image = PdfImage.fromFile("image.jpg");

        //Set the width and height of image
        float width = image.getWidth() * 0.50f;
        float height = image.getHeight() * 0.50f;

        //Define a position to draw image
        double x = (page.getCanvas().getClientSize().getWidth() - width) / 2;
        float y = 60f;

        //Draw image on page canvas
        page.getCanvas().drawImage(image, x, y, width, height);

        //Save the result file
        pdf.saveToFile("output/addImage.pdf", FileFormat.PDF);
    }
}
``` 
Output:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i2mq1mg4buae2snt9etk.png)
##Extract image##
```java
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class ExtractImage {
    public static void main(String []args) throws IOException {
        //Create a PdfDocument instance
        PdfDocument doc = new PdfDocument();
        //Load a pdf file
        doc.loadFromFile("output/addImage.pdf");

        //Declare an int variable
        int index = 0;
        //Loop through the pages
        for (PdfPageBase page : (Iterable<PdfPageBase>) doc.getPages()) {
            //Extract images from a particular page
            for (BufferedImage image : page.extractImages()) {
                //Specify the file path and name
                File output = new File("images/" + String.format("image_%d.jpg", index++));
                //Save image as .jpg file
                ImageIO.write(image, "JPG", output);
            }
        }
    }
}
```
Output:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ikfjckhayt6o1wsuq10a.png)
 
##Replace image##
```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 file
        doc.loadFromFile("output/addImage.pdf");

        //Get the first page
        PdfPageBase page = doc.getPages().get(0);

        //Load an image
        PdfImage image = PdfImage.fromFile("newImage.jpg");

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

        //Save the result file
        doc.saveToFile("output/replaceImage.pdf", FileFormat.PDF);
    }
}
```
Output:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zakvm0315phctz5mxmtu.png)
 
##Remove image##
```java
import com.spire.pdf.FileFormat;
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;

public class RemoveImage {
    public static void main(String []args){
        //Create a PdfDocument instance
        PdfDocument pdf = new PdfDocument();
        //Load a PDF file
        pdf.loadFromFile("output/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 file
        pdf.saveToFile("output/removeImage.pdf", FileFormat.PDF);
    }
}
```
Output:
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3s7nji0q6s0ficf3wxc0.png)