Watermarks are commonly used in Word documents to indicate document status, ownership, or confidentiality. For example, a report may need a “DRAFT” watermark during review, internal documents may require a “CONFIDENTIAL” label, and business templates may include a company logo watermark.
Adding or removing watermarks manually is simple for individual documents. However, it becomes inefficient when documents are generated automatically, processed in batches, or managed by enterprise document systems. In these scenarios, programmatic watermark management allows developers to apply consistent formatting across multiple Word files.
In this guide, we will show how to use Java to:
- Add text watermarks to Word documents.
- Add image watermarks to Word documents.
- Remove existing watermarks from Word documents.
Why Manage Word Watermarks Programmatically?
Automated watermark processing is useful in many scenarios:
- Document review workflows: Add a “DRAFT” watermark during review and remove it after approval.
- Confidential document management: Mark sensitive files with labels such as “CONFIDENTIAL” or “INTERNAL USE ONLY”.
- Automated report generation: Apply consistent watermarks when generating reports, contracts, or business documents.
- Batch document processing: Update multiple Word files without manually opening each document.
Prerequisites
Before getting started, you need:
- Java Development Kit (JDK): A Java development environment for building and running Java applications.
- Spire.Doc for Java: A Java library for creating, reading, and modifying Word documents programmatically.
You can add the library to your project using Maven.
Maven Dependency
Add the following configuration to your 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</artifactId>
<version>14.7.4</version>
</dependency>
</dependencies>
Part 1: Add a Text Watermark to a Word Document
Text watermarks are the most common type of watermark. They are usually used to display document status, such as “DRAFT”, “CONFIDENTIAL”, or “APPROVED”.
Step 1: Load a Word Document
First, create a Document object and load the Word file that you want to modify.
Document document = new Document();
document.loadFromFile("input.docx");
The loadFromFile() method loads the Word document into memory, allowing you to modify its content.
Step 2: Create a Text Watermark
Use the TextWatermark class to create a text watermark and configure its content, font, and color.
import java.awt.*;
TextWatermark watermark = new TextWatermark(
"CONFIDENTIAL",
new Font("Arial", Font.PLAIN, 45),
Color.GRAY
);
The parameters control the appearance of the watermark:
- The first parameter defines the watermark text.
- The second parameter specifies the font style and size.
- The third parameter sets the watermark color.
You can replace "CONFIDENTIAL" with other labels depending on your requirements:
DRAFT
INTERNAL USE ONLY
APPROVED
Step 3: Configure the Text Watermark Layout
You can control how the watermark appears by setting its layout.
For example, create a diagonal watermark:
watermark.setLayout(WatermarkLayout.Diagonal);
A diagonal watermark is commonly used for document status labels because it is noticeable without covering the main content.
For a horizontal watermark, use:
watermark.setLayout(WatermarkLayout.Horizontal);
Step 4: Apply and Save the Text Watermark
After configuring the watermark, apply it to the document and save the result.
document.setWatermark(watermark);
document.saveToFile(
"text-watermarked.docx",
FileFormat.Docx
);
Full Code Example: Add a Text Watermark
The following example adds a diagonal “CONFIDENTIAL” watermark to a Word document.
import com.spire.doc.*;
import com.spire.doc.documents.*;
import java.awt.*;
public class AddTextWatermark {
public static void main(String[] args) {
Document document = new Document();
// Load a Word document
document.loadFromFile("input.docx");
// Create a text watermark
TextWatermark watermark = new TextWatermark(
"CONFIDENTIAL",
new Font("Arial", Font.PLAIN, 45),
Color.GRAY
);
// Set watermark layout
watermark.setLayout(WatermarkLayout.Diagonal);
// Add watermark
document.setWatermark(watermark);
// Save the document
document.saveToFile(
"text-watermarked.docx",
FileFormat.Docx
);
document.dispose();
System.out.println("Text watermark added successfully!");
}
}
Part 2: Add an Image Watermark to a Word Document
Besides text watermarks, Word documents often use image watermarks such as company logos, brand marks, or copyright images.
Image watermarks are useful when a visual identifier is needed, such as in company templates or official documents.
Step 1: Load a Word Document
First, load the Word document where you want to add the image watermark.
Document document = new Document();
document.loadFromFile("input.docx");
Step 2: Create an Image Watermark
Use the PictureWatermark class to create an image watermark.
PictureWatermark watermark = new PictureWatermark();
watermark.setPicture("logo.png");
The logo.png file is the image that will be displayed as the document watermark.
Step 3: Adjust the Image Watermark Appearance
You can customize the image watermark according to your needs.
For example, reduce the image intensity:
watermark.setWashout(true);
Adjust the image scale:
watermark.setScaling(100);
These settings help the image work better as a background element without affecting document readability.
Step 4: Apply and Save the Image Watermark
After configuring the image watermark, apply it to the document and save the result.
document.setWatermark(watermark);
document.saveToFile(
"image-watermarked.docx",
FileFormat.Docx
);
Full Code Example: Add an Image Watermark
The following example adds a logo image as a watermark to a Word document.
import com.spire.doc.*;
public class AddImageWatermark {
public static void main(String[] args) {
Document document = new Document();
// Load a Word document
document.loadFromFile("input.docx");
// Create an image watermark
PictureWatermark watermark = new PictureWatermark();
watermark.setPicture("logo.png");
// Configure watermark appearance
watermark.setWashout(true);
watermark.setScaling(100);
// Add image watermark
document.setWatermark(watermark);
// Save the document
document.saveToFile(
"image-watermarked.docx",
FileFormat.Docx
);
document.dispose();
System.out.println("Image watermark added successfully!");
}
}
Part 3: Remove a Watermark from a Word Document
After a document has been reviewed or approved, you may need to remove the watermark before publishing or sharing it.
Step 1: Load the Watermarked Document
First, load the Word document that contains the watermark.
Document document = new Document();
document.loadFromFile("watermarked.docx");
Step 2: Remove the Watermark
Use the removeWatermark() method to remove the existing watermark:
document.removeWatermark();
This removes the watermark previously applied to the document.
Step 3: Save the Document Without the Watermark
Finally, save the updated document:
document.saveToFile(
"without-watermark.docx",
FileFormat.Docx
);
Full Code Example: Remove a Watermark
The following example removes a watermark from an existing Word document.
import com.spire.doc.*;
public class RemoveWatermark {
public static void main(String[] args) {
Document document = new Document();
// Load a watermarked document
document.loadFromFile("watermarked.docx");
// Remove watermark
document.removeWatermark();
// Save the document
document.saveToFile(
"without-watermark.docx",
FileFormat.Docx
);
document.dispose();
System.out.println("Watermark removed successfully!");
}
}
Run the Program
Compile and run the Java programs:
- The text watermark example generates a Word document with a text label.
- The image watermark example adds a logo or image background.
- The removal example creates a new document without the watermark.
You can open the generated .docx files in Microsoft Word to verify the results.
Additional Tips
- Choose appropriate watermark settings: Use lighter colors and suitable font sizes so the watermark does not affect document readability.
- Keep the original document: Adding or removing watermarks modifies the document, so keep a backup copy when processing important files.
- Process multiple documents: For batch operations, place the document loading, modification, and saving logic inside a loop.
- Test complex documents: Documents with multiple sections, images, or advanced formatting should be tested before large-scale processing.
Conclusion
Watermarks are widely used in Word document management to indicate document status, protect sensitive information, and maintain consistent branding.
While manual editing is sufficient for individual files, automated watermark management is more efficient for document generation systems and batch processing workflows.
With Spire.Doc for Java, developers can add text watermarks, add image watermarks, and remove existing watermarks from Word documents through simple API calls. This makes watermark handling easier to integrate into reporting systems, document management platforms, and enterprise applications.
Comments
Post a Comment