通常情况下,microsoft word只支持添加一个文本水印到word文档。该文将介绍如何使用spire.doc for .net 在word文档中页眉中添加艺术字模拟实现word文档中多行文字水印效果。
c#
using spire.doc;
using spire.doc.documents;
using spire.doc.fields;
namespace wordwatermark
{
class program
{
static void main(string[] args)
{
//加载示例文档
document doc = new document();
doc.loadfromfile("sample.docx");
//添加艺术字并设置大小
shapeobject shape = new shapeobject(doc,shapetype.textplaintext);
shape.width = 60;
shape.height = 20;
//设置艺术字文本内容、位置及样式
shape.verticalposition = 30;
shape.horizontalposition = 20;
shape.rotation = 315;
shape.wordart.text = "内部使用";
shape.fillcolor = system.drawing.color.red;
shape.strokecolor = system.drawing.color.gray;
section section;
headerfooter header;
for (int n = 0; n < doc.sections.count; n )
{
section = doc.sections[n];
//获取section的页眉
header = section.headersfooters.header;
paragraph paragraph1;
for (int i = 0; i < 3; i )
{
//添加段落到页眉
paragraph1 = header.addparagraph();
for (int j = 0; j < 4; j )
{
//复制艺术字并设置多行多列位置
shape = (shapeobject)shape.clone();
shape.verticalposition = 50 150 * i;
shape.horizontalposition=20 160 * j;
paragraph1.childobjects.add(shape);
}
}
}
//保存文档
doc.savetofile("result.docx", fileformat.docx2013);
system.diagnostics.process.start("result.docx");
}
}
}
vb.net
class program
private shared sub main(byval args as string())
dim doc as document = new document()
doc.loadfromfile("sample.docx")
dim shape as shapeobject = new shapeobject(doc, shapetype.textplaintext)
shape.width = 60
shape.height = 20
shape.verticalposition = 30
shape.horizontalposition = 20
shape.rotation = 315
shape.wordart.text = "内部使用"
shape.fillcolor = system.drawing.color.red
shape.strokecolor = system.drawing.color.gray
dim section as section
dim header as headerfooter
for n as integer = 0 to doc.sections.count - 1
section = doc.sections(n)
header = section.headersfooters.header
dim paragraph1 as paragraph
for i as integer = 0 to 3 - 1
paragraph1 = header.addparagraph()
for j as integer = 0 to 4 - 1
shape = ctype(shape.clone(), shapeobject)
shape.verticalposition = 50 150 * i
shape.horizontalposition = 20 160 * j
paragraph1.childobjects.add(shape)
next
next
next
doc.savetofile("result.docx", fileformat.docx2013)
end sub
end class
多行多列文本水印效果图: