华企号 元宇宙 C# 给Word文档设置水印

C# 给Word文档设置水印

Word中设置水印时,可加载图片设置为水印效果,但通常添加水印效果时,会对所有页面都设置成统一效果,如果需要对每一页或者某个页面设置不同的水印效果,则可以参考本文中的方法。下面,将以C#代码为例,对Word每一页设置不同的图片水印效果作详细介绍。

方法思路

在给Word每一页添加水印前,首先需要在Word文档每一页正文的最后一个字符后面插入“连续”分节符,然后在每一节的页眉段落里添加水印图片,并设置图片的坐标位置、对齐方式、衬于文字下方等。最后保存文档。

dll引入

方法1

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

方法2

通过NuGet安装。可通过以下2种方法安装:

  1. 可以在Visual Studio中打开“解决方案资源管理器”,鼠标右键点击“引用”,“管理NuGet包”,然后搜索“Free Spire.Doc”,点击“安装”。等待程序安装完成。
  2. 将以下内容复制到PM控制台安装。
1
Install-Package FreeSpire.Doc -Version 10.2.0

代码示例

给每页添加图片水印时,可参考如下步骤:

  • 创建 Document类的对象,并通过 LoadFromFile(string fileName)方法加载Word文档。
  • 通过 Document.Sections[]属性获取指定节。
  • 通过 HeadersFooters.Header属性获取页眉, HeaderFooter.AddParagraph()方法添加段落到页眉。
  • 通过 Paragraph.AppendPicture(string imgFile)方法添加图片到段落, DocPicture.VerticalPosition属性设置水印图片位置, DocPicture.HorizontalAlignment属性设置图片对齐方式。
  • 最后,通过 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
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
 
namespace ImageWatermark2
{
    class Program
    {
        static void Main(string[] args)
        {
            //加载Word测试文档
            Document doc = new Document();
            doc.LoadFromFile("test.docx");
 
            //获取文档第一节
            Section section1 = doc.Sections[0];
 
            //定义水印图片的纵向坐标位置
            float y = section1.PageSetup.PageSize.Height/3;
 
            //添加图片水印1
            HeaderFooter header1 = section1.HeadersFooters.Header;//获取页眉
            header1.Paragraphs.Clear();//删除原有页眉格式的段落
            Paragraph para1 = header1.AddParagraph();//重新添加段落
            DocPicture pic1 = para1.AppendPicture("logo1.png");//添加图片
            pic1.TextWrappingStyle = TextWrappingStyle.Behind;//图片置于文字下方
            pic1.VerticalPosition = y;
            pic1.HorizontalAlignment = ShapeHorizontalAlignment.Center;//设置图片对齐方式
 
            //同理设置第二节页眉中的图片水印2
            Section section2 = doc.Sections[1];
            HeaderFooter header2 = section2.HeadersFooters.Header;
            header2.Paragraphs.Clear();
            Paragraph para2 = header2.AddParagraph();
            DocPicture pic2 = para2.AppendPicture("logo2.png");
            pic2.TextWrappingStyle = TextWrappingStyle.Behind;
            pic2.VerticalPosition = y;
            pic2.HorizontalAlignment = ShapeHorizontalAlignment.Center;
 
            //同理设置第三节中的页眉中的图片水印3
            Section section3 = doc.Sections[2];
            HeaderFooter header3 = section3.HeadersFooters.Header;
            header3.Paragraphs.Clear();
            Paragraph para3 = header3.AddParagraph();
            DocPicture pic3 = para3.AppendPicture("logo3.png");
            pic3.TextWrappingStyle = TextWrappingStyle.Behind;
            pic3.VerticalPosition = y;
            pic3.HorizontalAlignment = ShapeHorizontalAlignment.Center;
 
            //保存文档
            doc.SaveToFile("DifferentImageWatermark.docx", FileFormat.Docx2013);
            System.Diagnostics.Process.Start("DifferentImageWatermark.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
54
55
56
57
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields
 
Namespace ImageWatermark2
         Class Program
                 Private Shared Sub Main(args As String())
                          '加载Word测试文档
                          Dim doc As New Document()
                          doc.LoadFromFile("test.docx")
 
                          '获取文档第一节
                          Dim section1 As Section = doc.Sections(0)
 
                          '定义水印图片的纵向坐标位置
                          Dim As Single = section1.PageSetup.PageSize.Height / 3
 
                          '添加图片水印1
                          Dim header1 As HeaderFooter = section1.HeadersFooters.Header
                          '获取页眉
                          header1.Paragraphs.Clear()
                          '删除原有页眉格式的段落
                          Dim para1 As Paragraph = header1.AddParagraph()
                          '重新添加段落
                          Dim pic1 As DocPicture = para1.AppendPicture("logo1.png")
                          '添加图片
                          pic1.TextWrappingStyle = TextWrappingStyle.Behind
                          '图片置于文字下方
                          pic1.VerticalPosition = y
                          pic1.HorizontalAlignment = ShapeHorizontalAlignment.Center
                          '设置图片对齐方式
                          '同理设置第二节页眉中的图片水印2
                          Dim section2 As Section = doc.Sections(1)
                          Dim header2 As HeaderFooter = section2.HeadersFooters.Header
                          header2.Paragraphs.Clear()
                          Dim para2 As Paragraph = header2.AddParagraph()
                          Dim pic2 As DocPicture = para2.AppendPicture("logo2.png")
                          pic2.TextWrappingStyle = TextWrappingStyle.Behind
                          pic2.VerticalPosition = y
                          pic2.HorizontalAlignment = ShapeHorizontalAlignment.Center
 
                          '同理设置第三节中的页眉中的图片水印3
                          Dim section3 As Section = doc.Sections(2)
                          Dim header3 As HeaderFooter = section3.HeadersFooters.Header
                          header3.Paragraphs.Clear()
                          Dim para3 As Paragraph = header3.AddParagraph()
                          Dim pic3 As DocPicture = para3.AppendPicture("logo3.png")
                          pic3.TextWrappingStyle = TextWrappingStyle.Behind
                          pic3.VerticalPosition = y
                          pic3.HorizontalAlignment = ShapeHorizontalAlignment.Center
 
                          '保存文档
                          doc.SaveToFile("DifferentImageWatermark.docx", FileFormat.Docx2013)
                          System.Diagnostics.Process.Start("DifferentImageWatermark.docx")
                 End Sub
         End Class
End Namespace

如图,每一页均可显示不同的图片水印效果:

C# 给Word文档设置水印插图

—END—

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

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

发表回复

联系我们

联系我们

028-84868647

在线咨询: QQ交谈

邮箱: tech@68v8.com

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

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

微信扫一扫关注我们

关注微博
返回顶部