亲宝软件园·资讯

展开

C#添加文本水印

E-iceblue 人气:1

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

实现代码

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

水印效果:

加载全部内容

相关教程
猜你喜欢
用户评论