Watermarks are commonly used in Word documents to indicate importance or copyright ownership. This guide demonstrates how to use the Free Spire.Doc for Java libray to add both text and image watermarks to Word documents programmatically.
Library Integration
Option 1: Download the Free Spire.Doc for Java package, extract it, and add the Spire.Doc.jar file from the lib folder to your project dependencies.
Option 2: Configure Maven by adding the following repository and dependency to your pom.xml file:
public class InsertTextWatermark { public static void main(String[] args) {
// Load existing Word document
Document wordDoc = new Document();
wordDoc.loadFromFile("InputDocument.docx");
// Initialize text watermark object
TextWatermark watermark = new TextWatermark();
// Configure watermark properties
watermark.setText("Confidential");
watermark.setFontName("Arial");
watermark.setFontSize(55);
watermark.setColor(Color.BLUE);
watermark.setLayout(WatermarkLayout.Diagonal);
// Apply watermark to document
wordDoc.setWatermark(watermark);
// Save modified document
wordDoc.saveToFile("TextWatermarkOutput.docx", FileFormat.Docx_2013);
}
}
</div>### Image Watermark Implementation
<div>```
import com.spire.doc.*;
public class InsertImageWatermark {
public static void main(String[] args) throws Exception {
// Load target document
Document document = new Document();
document.loadFromFile("InputDocument.docx");
// Create image watermark instance
PictureWatermark imgWatermark = new PictureWatermark();
// Configure image properties
imgWatermark.setPicture("watermark_logo.png");
imgWatermark.setScaling(25);
imgWatermark.isWashout(true);
// Add image watermark to document
document.setWatermark(imgWatermark);
// Save result document
document.saveToFile("ImageWatermarkOutput.docx", FileFormat.Docx);
}
}