DEV Community

E-iceblue Product Family
E-iceblue Product Family

Posted on • Edited on

Add, Extract, Replace and Remove Images in PDF in Java

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 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.

<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>
Enter fullscreen mode Exit fullscreen mode

Now let's start coding.

Add image

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);
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:
Alt Text

Extract image

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);
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:
Alt Text

Replace image

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);
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:
Alt Text

Remove image

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);
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:
Alt Text

Top comments (0)