· 

Java – Add, Replace or Remove Images in PDF

Images play an important role in an electronic document. Developers may have the requirement to add an image (for example, a company logo) to a PDF document, or replace the specific image in a document with a new image, or even delete the specified image permanently from a document. This article will introduce how to achieve these tasks by using Spire.PDF for Java.

Installing Spire.Pdf.jar

If you create a Maven project, you can easily import the jar in your application using the following configurations. For non-Maven projects, download the jar file from this link and add it as a dependency in your application.

<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</artifactId>
        <
verson>4.1.2</version>
    </
dependency>
</
dependencies>

Example 1. Add an Image to PDF

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 object
       
PdfDocument doc = new PdfDocument();

       
//Add a page
       
PdfPageBase page = doc.getPages().add();

       
//Load an image from path
       
PdfImage image = PdfImage.fromFile("C:\\Users\\Administrator\\Desktop\\web-design.jpg");

       
//Set the image width and height in the PDF
       
float width = image.getWidth() * 0.4f;
       
float height = image.getHeight() * 0.4f;

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

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

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

Example 2. Replace an Image in PDF

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

       
//Load the PDF file
       
PdfDocument doc = new PdfDocument();
        doc.loadFromFile(
"C:\\Users\\Administrator\\Desktop\\Sample.pdf");

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

       
//Load an image
       
PdfImage image = PdfImage.fromFile("C:\\Users\\Administrator\\Desktop\\c-sharp.png");

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

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

Example 3. Remove an Image from PDF

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 object
       
PdfDocument doc = new PdfDocument();

       
//Load the sample PDF file
       
doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\Sample.pdf");

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

       
//Delete a particular image by its index
       
page.deleteImage(0);

       
//Save the file
       
doc.saveToFile("output/DeleteImage.pdf", FileFormat.PDF
);
    }
}

Write a comment

Comments: 0