通常情况下,Microsoft Word 只支持添加一个文本字样的水印到 Word 文档。为满足更多元化的文档创建需求,本文将介绍一种在 Word 文档中添加多行文字水印的方法,下面,通过使用 Spire.Doc for .NET 来展示具体实现的方法及步骤。
安装 Spire.Doc for .NET
首先,您需要将 Spire.Doc for.NET 包含的 DLL 文件作为引用添加到您的 .NET 项目中。DLL 文件可以从此链接下载,也可以通过 NuGet 安装。
PM> Install-Package Spire.Doc添加多行文本水印
在 Word 中添加多行文本水印,即水印文本以多行多列平铺的方式铺满文档页面。实现的方法是通过在页眉中添加形状艺术字,并通过多次复制形状来模拟实现多行文字水印效果。以下是实现水印添加的主要代码步骤:
- 创建 Document 类的对象,并调用 Document.LoadFromFile(string fileName) 方法加载 Word 文档。
- 创建 ShapeObject 类的实例,并通过 ShapeObject.Width、ShapeObject.Height、ShapeObject.VerticalPosition、ShapeObject.Rotation、ShapeObject.WordArt.Text、ShapeObject.WordArt.FontFamily、ShapeObject.FillColor 等属性设置形状大小、位置、旋转角度、水印文字、字体及颜色等。
- for 循环遍历所有 Section,通过 Section.HeadersFooters.Header 属性获取页眉,并以 HeaderFooter.AddParagraph() 方法添加段落到页眉。
- 通过 for 循环以 ShapeObject.Clone() 方法多次复制形状,并通过 ShapeObject.VerticalPosition 和 ShapeObject.HorizontalPosition 属性设置形状位置排列。
- 调用 Paragraph.ChildObjects.Add(IDocumentObject entity) 方法添加形状到页眉段落。
- 最后,通过 Document.SaveToFile(string fileName, FileFormat fileFormat) 方法保存文档到指定路径。
- C#
- VB.NET
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
namespace MultiLineTextWatermark
{
class Program
{
static void Main(string[] args)
{
//加载Word文档
Document doc = new Document();
doc.LoadFromFile("http://cdn.e-iceblue.cn/Sample.docx");
//创建形状,并设置大小、文本内容、位置及样式
ShapeObject shape = new ShapeObject(doc, ShapeType.TextPlainText);
shape.Width = 60;
shape.Height =15;
shape.VerticalPosition = 25;
shape.HorizontalPosition = 20;
shape.Rotation = 320;
shape.WordArt.Text = "内部使用";
shape.WordArt.FontFamily = "宋体";
shape.FillColor = System.Drawing.Color.Red;
shape.StrokeColor = System.Drawing.Color.Red;
//遍历所有section
for (int n = 0; n < doc.Sections.Count; n++)
{
Section section = doc.Sections[n];
//获取页眉
HeaderFooter header = section.HeadersFooters.Header;
//添加段落到页眉
Paragraph paragraph1 = header.AddParagraph();
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 6; 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);
}
}
}Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields
Namespace MultiLineTextWatermark
Class Program
Private Shared Sub Main(args As String())
'加载Word文档 Dim doc As New Document() doc.LoadFromFile("http://cdn.e-iceblue.cn/Sample.docx") '创建形状,并设置大小、文本内容、位置及样式
Dim shape As New ShapeObject(doc, ShapeType.TextPlainText)
shape.Width = 60
shape.Height = 15
shape.VerticalPosition = 25
shape.HorizontalPosition = 20
shape.Rotation = 320
shape.WordArt.Text = "内部使用"
shape.WordArt.FontFamily = "宋体"
shape.FillColor = System.Drawing.Color.Red
shape.StrokeColor = System.Drawing.Color.Red
'遍历所有section For n As Integer = 0 To doc.Sections.Count - 1 Dim section As Section = doc.Sections(n) '获取页眉
Dim header As HeaderFooter = section.HeadersFooters.Header
'添加段落到页眉 Dim paragraph1 As Paragraph = header.AddParagraph() For i As Integer = 0 To 4 For j As Integer = 0 To 5 '复制形状并设置多行多列位置
shape = DirectCast(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
End Namespace
申请临时 License
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。







