Word watermarks can be divided into two types: text watermarks and picture watermarks. Text watermarks indicate the current state of the document, drafts, confidentiality, certified, etc. Photo watermarks can be used to specify the logo of a company. This article will show you how to use Java and Free Spire.Doc for Java How to add document watermarks and photo watermarks to Word documents and remove watermarks from Word documents.
Add Text Watermark: TextWatermark class Manipulate text watermarks. When creating a text watermark, you can customize the text watermark attributes such as font, size, font color, and watermark layout style.
import com.spire.doc.*;
import com.spire.doc.documents.WatermarkLayout;
import java.awt.*;
public class WordTextWatermark {
public static void main(String[] args) {
Document document = new Document();
document.loadFromFile("Sample.docx");
insertTextWatermark(document.getSections().get(0));
document.saveToFile("out/result.docx",FileFormat.Docx );
}
private static void insertTextWatermark(Section section) {
TextWatermark txtWatermark = new TextWatermark();
txtWatermark.setText("Internal use");
txtWatermark.setFontSize(40);
txtWatermark.setColor(Color.red);
txtWatermark.setLayout(WatermarkLayout.Diagonal);
section.getDocument().setWatermark(txtWatermark);
}
}
Output:
Add Image Watermark: PictureWatermark class For manipulating image watermarks. The watermark photo can be from the local area. It also comes from streams created in other projects.
import com.spire.doc.*;
public class WordImageWatermark {
public static void main(String[] args) throws Exception{
Document document = new Document();
document.loadFromFile("Sample.docx");
PictureWatermark picture = new PictureWatermark();
picture.setPicture("logo.png ");
picture.setScaling(5);
picture.isWashout(false);
document.setWatermark(picture);
document.saveToFile("out/result2.docx",FileFormat.Docx );
}
}
Output:
You can easily remove watermarks in Word documents using the method Remove Watermark, doc.setWatermark (null).
import com.spire.doc.*;
public class RemoveWatermark {
public static void main(String[] args){
Document doc = new Document();
doc.loadFromFile("ImageWatermark.docx");
doc.setWatermark(null);
doc.saveToFile("RemoveWatermark.docx", FileFormat.Docx);
}
}
Recommended Posts