华企号 元宇宙 C#/VB.NET 怎样添加水印Word文档

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

  • 创建 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#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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 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 As Integer = 0 To 4
 
                                               For 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—

作者: 华企网通王鹏程序员

我是程序员王鹏,热爱互联网软件开发和设计,专注于大数据、数据分析、数据库、php、java、python、scala、k8s、docker等知识总结。 我的座右铭:"业精于勤荒于嬉,行成于思毁于随"
上一篇
下一篇

发表回复

联系我们

联系我们

028-84868647

在线咨询: QQ交谈

邮箱: tech@68v8.com

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

关注微博
返回顶部