java怎么给word文档加水印
2022-03-07
可以使用Free Spire.Doc for Java在word文档中添加文本水印或图片水印。Free Spire.Doc for Java下载链接:网页链接
1.添加文本水印——代码如下:
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));
}
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);
}
}
2.添加图片水印——代码如下:
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 )
}
}
2024-07-20 广告