前面我们介绍了如何使用 。该文将详细介绍在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.shapelinestyle;
import com.spire.doc.documents.shapetype;
import com.spire.doc.fields.shapeobject;
import java.awt.*;
public class wordwatermark {
public static void main(string[] args) {
//加载示例文档
document doc = new document();
doc.loadfromfile("sample.docx");
//添加艺术字并设置大小
shapeobject shape = new shapeobject(doc, shapetype.text_plain_text);
shape.setwidth(60);
shape.setheight(20);
//设置艺术字文本内容、位置及样式
shape.setverticalposition(30);
shape.sethorizontalposition(20);
shape.setrotation(315);
shape.getwordart().settext("内部使用");
shape.setfillcolor(color.red);
shape.setlinestyle(shapelinestyle.single);
shape.setstrokecolor(new color(192, 192, 192, 255));
shape.setstrokeweight(1);
section section;
headerfooter header;
for (int n = 0; n < doc.getsections().getcount(); n ) {
section = doc.getsections().get(n);
//获取section的页眉
header = section.getheadersfooters().getheader();
paragraph paragraph1;
for (int i = 0; i < 4; i ) {
//添加段落到页眉
paragraph1 = header.addparagraph();
for (int j = 0; j < 3; j ) {
//复制艺术字并设置多行多列位置
shape = (shapeobject) shape.deepclone();
shape.setverticalposition(50 150 * i);
shape.sethorizontalposition(20 160 * j);
paragraph1.getchildobjects().add(shape);
}
}
}
//保存文档
doc.savetofile("result.docx", fileformat.docx_2013);
}
}
效果图: