java如何给word文档加水印
1.添加一个文本水印——代码如下:
导入com . spire . doc . *;
导入com . spire . doc . documents . water mark layout;
导入Java . awt . *;
公共类WordTextWatermark {
公共静态void main(String[] args) {
Document = new Document();
document . loadfromfile(" sample . docx ");
insertTextWatermark(document . get sections()。get(0));
}
私有静态void insertTextWatermark(Section Section){
text watermark txtWatermark = new text watermark();
TxtWatermark.setText("内部使用");
txtwatermark . set fontsize(40);
txtwatermark . set color(color . red);
txtwatermark . set layout(watermark layout。对角线);
section.getDocument()。set watermark(txtWatermark);
}
}
2.添加图片水印——代码如下:
导入com . spire . doc . *;
公共类WordImageWatermark {
public static void main(String[]args)?引发异常{
Document = new Document();
document . loadfromfile(" sample . docx ");
PictureWatermark图片= new picture watermark();
picture . set picture(" logo . png ");
picture . set scaling(5);
picture . iswashout(false);
document.setWatermark(图片);
document . savetofile(" out/result 2 . docx ",文件格式。Docx)
}
}