C#/VB.NET 添加多行文本水印到Word文档

  • C#/VB.NET 添加多行文本水印到Word文档已关闭评论
  • 131 次浏览
  • A+
所属分类:.NET技术
摘要

一般情况下,在Word中添加文字水印仅支持添加一个文本字样的水印,但在复杂的办公环境中,由于对不同文档的设计要求,需要在Word文档中添加平铺水印效果,即文档中的水印文字以多行多列分布的形式存在。本文将介绍如何来实现该水印效果的方法,下面是详细步骤及方法。

一般情况下,在Word中添加文字水印仅支持添加一个文本字样的水印,但在复杂的办公环境中,由于对不同文档的设计要求,需要在Word文档中添加平铺水印效果,即文档中的水印文字以多行多列分布的形式存在。本文将介绍如何来实现该水印效果的方法,下面是详细步骤及方法。


 

dll引用

通过 NuGet 引入dll(2种方法)的方法

1.可以在Visual Studio中打开 【解决方案资源管理器】,鼠标右键点击 【引用】,【管理NuGet包】,然后搜索 【Free Spire.Doc】,点击【安装】。等待程序安装完成。

2.将以下内容复制到PM控制台安装:

Install-Package FreeSpire.Doc -Version 10.2

手动添加dll引用的方法

可通过手动 下载包 到本地,然后解压,找到BIN文件夹下的Spire.Doc.dll。然后在Visual Studio中打开“解决方案资源管理器”,鼠标右键点击“引用”,“添加引用”,将本地路径BIN文件夹下的dll文件添加引用至程序。


 

添加多行多列文字水印

在Word中添加多行文字水印时,实现的方法是通过在页眉中添加形状艺术字,并通过多次复制形状来模拟实现多行文字水印效果。以下是实现水印添加的主要代码步骤:

  1. 创建Document类的对象,并调用Document.LoadFromFile(string fileName)方法加载Word文档。
  2. 创建ShapeObject类的实例,并通过ShapeObject.WidthShapeObject.HeightShapeObject.VerticalPositionShapeObject.RotationShapeObject.WordArt.TextShapeObject.WordArt.FontFamilyShapeObject.FillColor等属性设置形状大小、位置、旋转角度、水印文字、字体及颜色等。
  3. for循环遍历所有Section,通过Section.HeadersFooters.Header属性获取页眉,并以HeaderFooter.AddParagraph()方法添加段落到页眉。
  4. 通过for循环以ShapeObject.Clone()方法多次复制形状,并通过ShapeObject.VerticalPositionShapeObject.HorizontalPosition属性设置形状位置排列。
  5. 调用Paragraph.ChildObjects.Add(IDocumentObject entity)方法添加形状到页眉段落。
  6. 最后,通过Document.SaveToFile(string fileName, FileFormat fileFormat)方法保存文档到指定路径。

C#

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("test.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);             System.Diagnostics.Process.Start("result.docx");          }     } }

VB.NET

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("test.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)             System.Diagnostics.Process.Start("result.docx")         End Sub     End Class End Namespace

水印效果:

C#/VB.NET 添加多行文本水印到Word文档

 

—END—