In this guide, we explore techniques for removing and duplicating watermarks within Word documents using Java applications. Free Spire.Doc for Java provides functionality to eliminate both text and image watermarks, as well as transfer watermarks between documents.
Jar Package Import
Method 1: Download the Free Spire.Doc for Java package and extract its contents. Add the Spire.Doc.jar file from the lib directory as a dependency in your Java project.
Method 2: Install the JAR package through the Maven repository by adding the following configuration to your pom.xml file:
<repositories>
<repository>
<id>com.e-iceblue</id>
<url>http://repo.e-iceblue.cn/repository/maven-public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.doc.free</artifactId>
<version>2.7.3</version>
</dependency>
</dependencies>Original Word Document:
Removing Text Watermarks:
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
public class TextWatermarkRemover {
public static void main(String[] args){
// Create Document instance
Document document = new Document();
// Load the Word document
document.loadFromFile("original_with_text_watermark.docx");
// Clear the watermark by setting it to null
document.setWatermark(null);
// Save the modified document
document.saveToFile("document_without_watermark.docx", FileFormat.Docx_2013);
}
}Removing Text Watermark Result:
Duplicating Image Watermarks:
import com.spire.doc.*;
public class WatermarkCopier {
public static void main(String[] args) {
// Load source document with watermark
Document sourceDoc = new Document();
sourceDoc.loadFromFile("document_with_image_watermark.docx");
// Load target document
Document targetDoc = new Document();
targetDoc.loadFromFile("destination_document.docx");
// Extract watermark from source and apply to target
targetDoc.setWatermark(sourceDoc.getWatermark());
// Save the modified target document
targetDoc.saveToFile("document_with_copied_watermark.docx", FileFormat.Docx_2013);
// Release resources
targetDoc.dispose();
}
}Duplicating Image Watermark Result: