天天看點

C#/VB.NET 添加多行文本水印到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.Width、ShapeObject.Height、ShapeObject.VerticalPosition、ShapeObject.Rotation、ShapeObject.WordArt.Text、ShapeObject.WordArt.FontFamily、ShapeObject.FillColor等屬性設定形狀大小、位置、旋轉角度、水印文字、字型及顔色等。
  3. for循環周遊所有Section,通過Section.HeadersFooters.Header屬性擷取頁眉,并以HeaderFooter.AddParagraph()方法添加段落到頁眉。
  4. 通過for循環以ShapeObject.Clone()方法多次複制形狀,并通過ShapeObject.VerticalPosition和ShapeObject.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—