Thursday, 4 November 2021

Add Image Stamps to Word Documents in Java

Microsoft Word does not have a built-in stamp feature. When you want a Word document to be protected against copying or leakage, or be identified as belonging to a particular company, you can insert an image to a specified position of text to mimic the stamp effect. In this article, you’ll learn how to add an image stamp to a Word document using Java codes.

DEPENDENCY

In order to finish the operation above, you need to use a free third-party library called Free Spire.Doc for Java. First of all, you’re required to add the Spire.Doc.jar file as a dependency in your Java program. The JAR file can be downloaded from this link, or you can also easily import the JAR file 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.doc.free</artifactId>
<version>3.9.0</version>
</dependency>
</dependencies>

Add an Image stamp to a Word document

Free Spire.Doc for Java includes classes and methods for adding images and setting their size, position, and wrapping style. The following are detailed steps for your reference.

l  Create a Document instance and load a Word sample document using Document.loadFromFile() method.

l  Get a specific section and paragraph in Word in order to insert an image using Document.getSections().get() method and Section.getParagraphs().get() method respectively.

l  Add an image using Paragraph.appendPicture() method, and set its position, size and wrapping style using methods provided by DocParagraph class.

l  Save the document using Document.saveToFile() method.

import com.spire.doc.*;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.TextWrappingStyle;
import com.spire.doc.fields.DocPicture;

public class ImageStamp {
public static void main(String[] args) {
//Create a Document instance
Document doc = new Document();

//Load a Word sample document
doc.loadFromFile("C:\\Users\\Test1\\Desktop\\sample.docx");

//Get the specific paragraph
Section section = doc.getSections().get(1);
Paragraph paragraph = section.getParagraphs().get(2);

//Add an image
DocPicture picture = paragraph.appendPicture("C:\\Users\\Test1\\Desktop\\stamp.png");

//Set the position of the image
picture.setHorizontalPosition(240f);
picture.setVerticalPosition(120f);

//Set width and height of the image
picture.setWidth(150);
picture.setHeight(150);

//Set wrapping style of the image to In_Front_Of_Text, so that it looks like a stamp
picture.setTextWrappingStyle(TextWrappingStyle.In_Front_Of_Text);

//Save the document to file
doc.saveToFile("output/AddImageStamp.docx", FileFormat.Docx);
doc.dispose();
}
}




No comments:

Post a Comment

Change PDF Versions in Java

In daily work, you might need to change the version of a PDF document you have in order to ensure compatibility with another version which a...