欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#實(shí)現(xiàn)添加多行文本水印到Word文檔

 更新時(shí)間:2022年07月19日 11:10:28   作者:E-iceblue  
一般情況下,在Word中添加文字水印僅支持添加一個(gè)文本字樣的水印,由于對不同文檔的設(shè)計(jì)要求,需要在Word文檔中添加平鋪水印效果。本文將介紹如何來實(shí)現(xiàn)該水印效果的方法,感興趣的可以了解一下

一般情況下,在Word中添加文字水印僅支持添加一個(gè)文本字樣的水印,但在復(fù)雜的辦公環(huán)境中,由于對不同文檔的設(shè)計(jì)要求,需要在Word文檔中添加平鋪水印效果,即文檔中的水印文字以多行多列分布的形式存在。本文將介紹如何來實(shí)現(xiàn)該水印效果的方法,下面是詳細(xì)步驟及方法。

dll引用

通過 NuGet 引入dll(2種方法)的方法

1.可以在Visual Studio中打開 【解決方案資源管理器】,鼠標(biāo)右鍵點(diǎn)擊 【引用】,【管理NuGet包】,然后搜索 【Free Spire.Doc】,點(diǎn)擊【安裝】。等待程序安裝完成。

2.將以下內(nèi)容復(fù)制到PM控制臺安裝:

Install-Package FreeSpire.Doc -Version 10.2

手動添加dll引用的方法

可通過手動 下載包 到本地,然后解壓,找到BIN文件夾下的Spire.Doc.dll。然后在Visual Studio中打開“解決方案資源管理器”,鼠標(biāo)右鍵點(diǎn)擊“引用”,“添加引用”,將本地路徑BIN文件夾下的dll文件添加引用至程序。

添加多行多列文字水印

在Word中添加多行文字水印時(shí),實(shí)現(xiàn)的方法是通過在頁眉中添加形狀藝術(shù)字,并通過多次復(fù)制形狀來模擬實(shí)現(xiàn)多行文字水印效果。以下是實(shí)現(xiàn)水印添加的主要代碼步驟:

  • 創(chuàng)建Document類的對象,并調(diào)用Document.LoadFromFile(string fileName)方法加載Word文檔。
  • 創(chuàng)建ShapeObject類的實(shí)例,并通過ShapeObject.Width、ShapeObject.Height、ShapeObject.VerticalPositionShapeObject.Rotation、ShapeObject.WordArt.Text、ShapeObject.WordArt.FontFamily、ShapeObject.FillColor等屬性設(shè)置形狀大小、位置、旋轉(zhuǎn)角度、水印文字、字體及顏色等。
  • for循環(huán)遍歷所有Section,通過Section.HeadersFooters.Header屬性獲取頁眉,并以HeaderFooter.AddParagraph()方法添加段落到頁眉。
  • 通過for循環(huán)以ShapeObject.Clone()方法多次復(fù)制形狀,并通過ShapeObject.VerticalPositionShapeObject.HorizontalPosition屬性設(shè)置形狀位置排列。
  • 調(diào)用Paragraph.ChildObjects.Add(IDocumentObject entity)方法添加形狀到頁眉段落。
  • 最后,通過Document.SaveToFile(string fileName, FileFormat fileFormat)方法保存文檔到指定路徑。

實(shí)現(xiàn)代碼

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");

            //創(chuàng)建形狀,并設(shè)置大小、水印文字、位置及樣式
            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++)
                    {
                        //復(fù)制形狀并設(shè)置多行多列位置
                        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")

            '創(chuàng)建形狀,并設(shè)置大小、水印文字、位置及樣式
            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
                        '復(fù)制形狀并設(shè)置多行多列位置
                        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

水印效果:

到此這篇關(guān)于C#實(shí)現(xiàn)添加多行文本水印到Word文檔的文章就介紹到這了,更多相關(guān)C#添加文本水印內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論