ToB企服应用市场:ToB评测及商务社交产业平台

标题: C#/VB.NET 添加多行文本水印到Word文档 [打印本页]

作者: 天空闲话    时间: 2022-8-29 06:10
标题: C#/VB.NET 添加多行文本水印到Word文档
一般情况下,在Word中添加文字水印仅支持添加一个文本字样的水印,但在复杂的办公环境中,由于对不同文档的设计要求,需要在Word文档中添加平铺水印效果,即文档中的水印文字以多行多列分布的形式存在。本文将介绍如何来实现该水印效果的方法,下面是详细步骤及方法。
 
dll引用

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

1.可以在Visual Studio中打开 【解决方案资源管理器】,鼠标右键点击 【引用】,【管理NuGet包】,然后搜索 【Free Spire.Doc】,点击【安装】。等待程序安装完成。
2.将以下内容复制到PM控制台安装:
  1. Install-Package FreeSpire.Doc -Version 10.2
复制代码
手动添加dll引用的方法

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

在Word中添加多行文字水印时,实现的方法是通过在页眉中添加形状艺术字,并通过多次复制形状来模拟实现多行文字水印效果。以下是实现水印添加的主要代码步骤:
C#
  1. using Spire.Doc;
  2. using Spire.Doc.Documents;
  3. using Spire.Doc.Fields;
  4. namespace MultiLineTextWatermark
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             //加载Word文档
  11.             Document doc = new Document();
  12.             doc.LoadFromFile("test.docx");
  13.             //创建形状,并设置大小、水印文字、位置及样式
  14.             ShapeObject shape = new ShapeObject(doc, ShapeType.TextPlainText);
  15.             shape.Width = 60;
  16.             shape.Height =15;
  17.             shape.VerticalPosition = 25;
  18.             shape.HorizontalPosition = 20;
  19.             shape.Rotation = 320;
  20.             shape.WordArt.Text = "草稿副本";
  21.             shape.WordArt.FontFamily = "宋体";
  22.             shape.FillColor = System.Drawing.Color.Red;
  23.             shape.StrokeColor = System.Drawing.Color.Red;
  24.             //遍历所有section
  25.             for (int n = 0; n < doc.Sections.Count; n++)
  26.             {
  27.                 Section section = doc.Sections[n];
  28.                 //获取页眉
  29.                 HeaderFooter header = section.HeadersFooters.Header;
  30.                
  31.                 //添加段落到页眉
  32.                 Paragraph paragraph1 = header.AddParagraph();
  33.                 for (int i = 0; i < 5; i++)
  34.                 {
  35.                     
  36.                     for (int j = 0; j < 6; j++)
  37.                     {
  38.                         //复制形状并设置多行多列位置
  39.                         shape = (ShapeObject)shape.Clone();
  40.                         shape.VerticalPosition = 50 + 150 * i;
  41.                         shape.HorizontalPosition = 20 + 160 * j;
  42.                         //添加形状到段落
  43.                         paragraph1.ChildObjects.Add(shape);
  44.                     }
  45.                 }
  46.             }
  47.             //保存文档
  48.             doc.SaveToFile("result.docx", FileFormat.Docx2013);
  49.             System.Diagnostics.Process.Start("result.docx");
  50.         }
  51.     }
  52. }
复制代码
VB.NET
  1. Imports Spire.Doc
  2. Imports Spire.Doc.Documents
  3. Imports Spire.Doc.Fields
  4. Namespace MultiLineTextWatermark
  5.     Class Program
  6.         Private Shared Sub Main(args As String())
  7.             '加载Word文档
  8.             Dim doc As New Document()
  9.             doc.LoadFromFile("test.docx")
  10.             '创建形状,并设置大小、水印文字、位置及样式
  11.             Dim shape As New ShapeObject(doc, ShapeType.TextPlainText)
  12.             shape.Width = 60
  13.             shape.Height = 15
  14.             shape.VerticalPosition = 25
  15.             shape.HorizontalPosition = 20
  16.             shape.Rotation = 320
  17.             shape.WordArt.Text = "草稿副本"
  18.             shape.WordArt.FontFamily = "宋体"
  19.             shape.FillColor = System.Drawing.Color.Red
  20.             shape.StrokeColor = System.Drawing.Color.Red
  21.             '遍历所有section
  22.             For n As Integer = 0 To doc.Sections.Count - 1
  23.                 Dim section As Section = doc.Sections(n)
  24.                 '获取页眉
  25.                 Dim header As HeaderFooter = section.HeadersFooters.Header
  26.                 '添加段落到页眉
  27.                 Dim paragraph1 As Paragraph = header.AddParagraph()
  28.                 For i As Integer = 0 To 4
  29.                     For j As Integer = 0 To 5
  30.                         '复制形状并设置多行多列位置
  31.                         shape = DirectCast(shape.Clone(), ShapeObject)
  32.                         shape.VerticalPosition = 50 + 150 * i
  33.                         shape.HorizontalPosition = 20 + 160 * j
  34.                         '添加形状到段落
  35.                         paragraph1.ChildObjects.Add(shape)
  36.                     Next
  37.                 Next
  38.             Next
  39.             '保存文档
  40.             doc.SaveToFile("result.docx", FileFormat.Docx2013)
  41.             System.Diagnostics.Process.Start("result.docx")
  42.         End Sub
  43.     End Class
  44. End Namespace
复制代码
水印效果:

 
—END—
 

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4