前面我们介绍了如何使用 spire.doc for java 添加多行文本水印到 word 文档。该文将详细介绍在word页眉中添加多行图片水印的效果。
import com.spire.doc.document;
import com.spire.doc.fileformat;
import com.spire.doc.headerfooter;
import com.spire.doc.section;
import com.spire.doc.documents.paragraph;
import com.spire.doc.documents.textwrappingstyle;
import com.spire.doc.fields.docpicture;
public class wordimagewatermark {
public static void main(string[] args) throws exception {
//加载示例文档
document doc=new document();
doc.loadfromfile("sample.docx");
//加载图片
docpicture picture = new docpicture(doc);
picture.loadimage("logo.png");
//设置图片环绕方式
picture.settextwrappingstyle(textwrappingstyle.behind);
for (int n = 0; n < doc.getsections().getcount(); n ) {
section section = doc.getsections().get(n);
//获取section的页眉
headerfooter header = section.getheadersfooters().getheader();
paragraph paragrapg1;
if(header.getparagraphs().getcount()>0){
paragrapg1=header.getparagraphs().get(0);
}else {
//添加段落到页眉
paragrapg1 = header.addparagraph();
}
for (int p = 0; p < 3; p ) {
for (int q = 0; q < 2; q ) {
//复制图片并设置多行多列位置
picture = (docpicture)picture.deepclone();
picture.setverticalposition(100 200 * p);
picture.sethorizontalposition(50 210 * q);
paragrapg1.getchildobjects().add(picture);
}
}
}
//保存文档
doc.savetofile("多行图片水印.docx", fileformat.docx_2013);
}
}
效果图: