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.WidthShapeObject.HeightShapeObject.VerticalPositionShapeObject.RotationShapeObject.WordArt.TextShapeObject.WordArt.FontFamilyShapeObject.FillColor等屬性設置形狀大小、位置、旋轉角度、水印文字、字體及顏色等。
  3. for循環遍歷所有Section,通過Section.HeadersFooters.Header屬性獲取頁眉,並以HeaderFooter.AddParagraph()方法添加段落到頁眉。
  4. 通過for循環以ShapeObject.Clone()方法多次複製形狀,並通過ShapeObject.VerticalPositionShapeObject.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

水印效果:

 

—END—

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章