Watermarks are added to the Word documents for various purposes such as preventing illegal usage, defining the ownership, displaying the document’s status, etc. In this article, you will learn how to add a watermark to Word documents using Java. The article will demonstrate the insertion of text and image watermarks into a Word document separately.
- Java API to Add Watermark in Word Documents
- Add Text Watermark to a Word Document
- Add Image Watermark to a Word Document
Java API to Add Watermark in Word Documents
In order to add watermarks to the Word documents, we will use Aspose.Words for Java. It is a feature-rich API that lets you create word processing documents from scratch. Also, it allows you to manipulate the existing Word documents quite easily. You can either download the API or install it using the following Maven configurations.
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>https://repository.aspose.com/repo/</url>
</repository>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-words</artifactId>
<version>21.7</version>
<type>pom</type>
</dependency>
Add Text Watermark to a Word Document in Java
The following are the steps to insert a text watermark into a Word document using Java.
- First, load the Word document using Document class.
- Create an instance of TextWatermarkOptions class to set watermark options such as font family, font size, color, layout, etc.
- Add text watermark to the document using Document.getWatermark().setText(String, TextWatermarkOptions) method.
- Finally, save the updated Word document using Document.save(String) method.
The following code sample shows how to add a text watermark to a Word document.
The following is the screenshot of a Word document after adding the text watermark.
Add Image Watermark to a Word Document
The following are the steps to add an image watermark to a Word document using Java.
- First, load the Word document using Document class.
- Create an instance of ImageWatermarkOptions class to set watermark options.
- Load the watermark image into a BufferedImage object.
- Add image watermark to the document using Document.getWatermark().setImage(BufferedImage, ImageWatermarkOptions) method.
- Finally, save the updated Word document using Document.save(String) method.
The following code sample shows how to add an image watermark to the Word document.
The following is the screenshot of a Word document after adding the image watermark.
Get a Free API License
You can get a temporary license in order to use the API without evaluation limitations.
Conclusion
In this article, you have learned how to add a watermark to Word documents using Java. The steps and code samples have demonstrated how to insert a text or image watermark into a Word document. You can learn more about the Java word processing API using the documentation. In case you would have any questions, feel free to let us know via our forum.