关于word水印
第一,官网下载所需要的jar包 :https://www.e-iceblue.cn/Downloads/Spire-Doc-NET.html
解压压缩包,获取Spire.Doc.jar
第二,将Spire.Doc.jar添加
或者应用maven <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>
文字水印测试
import com.spire.doc.*;
import com.spire.doc.documents.WatermarkLayout;
import java.awt.*;
public class Main {
public static void main(String[] args) {
//加载测试文档
Document document = new Document();
document.loadFromFile("sample.docx");
//插入文本水印
InsertTextWatermark(document.getSections().get(0));
//保存文档
document.saveToFile("textwatermark.docx",FileFormat.Docx );
}
//自定义方法指定文本水印字样,并设置成水印
private static void InsertTextWatermark(Section section){
TextWatermark txtWatermark = new TextWatermark();
txtWatermark.setText("内部使用");
txtWatermark.setFontSize(40);
txtWatermark.setColor(Color.red);
txtWatermark.setLayout(WatermarkLayout.Diagonal);
section.getDocument().setWatermark(txtWatermark);
}
}
图片水印 import com.spire.doc.*; public class Main { public static void main(String[] args) { //加载测试文档 Document document = new Document(); document.loadFromFile("sample.docx"); //加载需要设置成水印的图片 PictureWatermark picture = new PictureWatermark(); picture.setPicture("wx.png"); picture.setScaling(5); picture.isWashout(false); //将图片设置成水印 document.setWatermark(picture); //保存文档 document.saveToFile("imagewatermark.docx",FileFormat.Docx ); } }