通常情况下,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.WordArt.FontFamily="宋体";
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.WordArt.FontFamily="宋体"
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
多行多列文本水印效果图: