最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#實現(xiàn)給Word每一頁設(shè)置不同文字水印的方法詳解

 更新時間:2022年07月26日 15:04:39   作者:E-iceblue  
Word中設(shè)置水印時,可使用預(yù)設(shè)的文字或自定義文字設(shè)置為水印效果,但通常添加水印效果時,會對所有頁面都設(shè)置成統(tǒng)一效果。本文以C#?代碼為例,對Word每一頁設(shè)置不同的文字水印效果作詳細介紹,感興趣的可以了解一下

Word中設(shè)置水印時,可使用預(yù)設(shè)的文字或自定義文字設(shè)置為水印效果,但通常添加水印效果時,會對所有頁面都設(shè)置成統(tǒng)一效果,如果需要對每一頁或者某個頁面設(shè)置不同的水印效果,則可以參考本文中的方法。下面,將以C# 代碼為例,對Word每一頁設(shè)置不同的文字水印效果作詳細介紹。

方法思路

在給Word每一頁添加文字水印前,首先需要在Word文檔每一頁正文的最后一個字符后面插入“連續(xù)”分節(jié)符,然后在每一節(jié)的頁眉段落里添加藝術(shù)字形狀,并設(shè)置形狀大小、對齊方式等。最后保存文檔。

dll引用

方法1

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

方法2

通過NuGet安裝??赏ㄟ^以下2種方法安裝:

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

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

Install-Package Spire.Doc -Version 10.1.14

代碼示例

給每頁添加文字水印時,可參考如下步驟:

  • 創(chuàng)建Document類的對象,并通過LoadFromFile(string fileName)方法加載Word文檔。
  • 通過Document.Sections[]屬性獲取指定節(jié)。
  • 通過HeadersFooters.Header屬性獲取頁眉,HeaderFooter.AddParagraph()方法添加段落到頁眉。
  • 創(chuàng)建ShapeObject類的對象,并傳入?yún)?shù)設(shè)置形狀類型為TextPlainText類型的藝術(shù)字。并調(diào)用方法設(shè)置藝術(shù)字樣式,如藝術(shù)字高度、寬度、旋轉(zhuǎn)、顏色、對齊方式等。
  • 使用DocumentObjectCollection.Add(IDocumentObject)方法將藝術(shù)字添加到段落。
  • 最后,通過Document.SaveToFile(string fileName, FileFormat fileFormat)方法保存文檔。

不同頁面中設(shè)置不一樣的文字水印效果,只需要獲取該頁面對應(yīng)的節(jié),然后參考上述用到的方法來添加即可。

C#

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;

namespace TextWatermark2
{
    class Program
    {
        static void Main(string[] args)
        {
            //加載Word測試文檔
            Document doc = new Document();
            doc.LoadFromFile("test.docx");

            //獲取文檔第一節(jié)
            Section section1 = doc.Sections[0];

            //定義水印文字的縱向坐標(biāo)位置
            float y = section1.PageSetup.PageSize.Height/3;

            //添加文字水印1
            HeaderFooter header1 = section1.HeadersFooters.Header;//獲取頁眉
            header1.Paragraphs.Clear();//刪除原有頁眉格式的段落
            Paragraph para1 = header1.AddParagraph();//重新添加段落
            
            //添加藝術(shù)字并設(shè)置大小
            ShapeObject shape1 = new ShapeObject(doc, ShapeType.TextPlainText);
            shape1.Width = 362;
            shape1.Height = 118;
            //設(shè)置藝術(shù)字文本內(nèi)容、位置及樣式(即文本水印字樣)
            shape1.Rotation = 315;
            shape1.WordArt.Text = "內(nèi)部使用";
            shape1.FillColor = Color.ForestGreen;
            shape1.LineStyle = ShapeLineStyle.Single;
            shape1.StrokeColor = Color.ForestGreen;
            shape1.StrokeWeight = 0.5;
            shape1.VerticalPosition = y;
            shape1.HorizontalAlignment = ShapeHorizontalAlignment.Center;
            para1.ChildObjects.Add(shape1);

            //同理設(shè)置第二節(jié)頁眉中的文字水印2
            Section section2 = doc.Sections[1];
            HeaderFooter header2 = section2.HeadersFooters.Header;
            header2.Paragraphs.Clear();
            Paragraph para2 = header2.AddParagraph();
            ShapeObject shape2 = new ShapeObject(doc, ShapeType.TextPlainText);
            shape2.Width = 362;
            shape2.Height = 118;
            shape2.Rotation = 315;
            shape2.WordArt.Text = "絕密資料";
            shape2.FillColor = Color.HotPink;
            shape2.LineStyle = ShapeLineStyle.Single;
            shape2.StrokeColor = Color.HotPink;
            shape2.StrokeWeight = 0.5;
            shape2.VerticalPosition = y;
            shape2.HorizontalAlignment = ShapeHorizontalAlignment.Center;
            para2.ChildObjects.Add(shape2);

            //同理設(shè)置第三節(jié)中的頁眉中的文字水印3
            Section section3 = doc.Sections[2];
            HeaderFooter header3 = section3.HeadersFooters.Header;
            header3.Paragraphs.Clear();
            Paragraph para3 = header3.AddParagraph();
            ShapeObject shape3 = new ShapeObject(doc, ShapeType.TextPlainText);
            shape3.Width = 362;
            shape3.Height = 118;
            shape3.Rotation = 315;
            shape3.WordArt.Text = "禁止傳閱";
            shape3.FillColor = Color.DarkOrange;
            shape3.LineStyle = ShapeLineStyle.Single;
            shape3.StrokeColor = Color.DarkOrange;
            shape3.StrokeWeight = 0.5;
            shape3.VerticalPosition = y;
            shape3.HorizontalAlignment = ShapeHorizontalAlignment.Center;
            para3.ChildObjects.Add(shape3);

            //保存文檔
            doc.SaveToFile("DifferentTextWatermark.docx", FileFormat.Docx2013);
            System.Diagnostics.Process.Start("DifferentTextWatermark.docx");
        }
    }
}

VB.NET

Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields
Imports System.Drawing

Namespace TextWatermark2
    Class Program
        Private Shared Sub Main(args As String())
            '加載Word測試文檔
            Dim doc As New Document()
            doc.LoadFromFile("test.docx")

            '獲取文檔第一節(jié)
            Dim section1 As Section = doc.Sections(0)

            '定義水印文字的縱向坐標(biāo)位置
            Dim y As Single = section1.PageSetup.PageSize.Height / 3

            '添加文字水印1
            Dim header1 As HeaderFooter = section1.HeadersFooters.Header
            '獲取頁眉
            header1.Paragraphs.Clear()
            '刪除原有頁眉格式的段落
            Dim para1 As Paragraph = header1.AddParagraph()
            '重新添加段落
            '添加藝術(shù)字并設(shè)置大小
            Dim shape1 As New ShapeObject(doc, ShapeType.TextPlainText)
            shape1.Width = 362
            shape1.Height = 118
            '設(shè)置藝術(shù)字文本內(nèi)容、位置及樣式(即文本水印字樣)
            shape1.Rotation = 315
            shape1.WordArt.Text = "內(nèi)部使用"
            shape1.FillColor = Color.ForestGreen
            shape1.LineStyle = ShapeLineStyle.[Single]
            shape1.StrokeColor = Color.ForestGreen
            shape1.StrokeWeight = 0.5
            shape1.VerticalPosition = y
            shape1.HorizontalAlignment = ShapeHorizontalAlignment.Center
            para1.ChildObjects.Add(shape1)

            '同理設(shè)置第二節(jié)頁眉中的文字水印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 shape2 As New ShapeObject(doc, ShapeType.TextPlainText)
            shape2.Width = 362
            shape2.Height = 118
            shape2.Rotation = 315
            shape2.WordArt.Text = "絕密資料"
            shape2.FillColor = Color.HotPink
            shape2.LineStyle = ShapeLineStyle.[Single]
            shape2.StrokeColor = Color.HotPink
            shape2.StrokeWeight = 0.5
            shape2.VerticalPosition = y
            shape2.HorizontalAlignment = ShapeHorizontalAlignment.Center
            para2.ChildObjects.Add(shape2)

            '同理設(shè)置第三節(jié)中的頁眉中的文字水印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 shape3 As New ShapeObject(doc, ShapeType.TextPlainText)
            shape3.Width = 362
            shape3.Height = 118
            shape3.Rotation = 315
            shape3.WordArt.Text = "禁止傳閱"
            shape3.FillColor = Color.DarkOrange
            shape3.LineStyle = ShapeLineStyle.[Single]
            shape3.StrokeColor = Color.DarkOrange
            shape3.StrokeWeight = 0.5
            shape3.VerticalPosition = y
            shape3.HorizontalAlignment = ShapeHorizontalAlignment.Center
            para3.ChildObjects.Add(shape3)

            '保存文檔
            doc.SaveToFile("DifferentTextWatermark.docx", FileFormat.Docx2013)
            System.Diagnostics.Process.Start("DifferentTextWatermark.docx")
        End Sub
    End Class
End Namespace

如圖,每一頁均可顯示不同的文字水印效果:

到此這篇關(guān)于C#實現(xiàn)給Word每一頁設(shè)置不同文字水印的方法詳解的文章就介紹到這了,更多相關(guān)C# Word設(shè)置文字水印內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

  • Unity Shader相交算法實現(xiàn)簡易防能量盾

    Unity Shader相交算法實現(xiàn)簡易防能量盾

    這篇文章主要為大家詳細介紹了Unity Shader相交算法實現(xiàn)簡易防能量盾,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-04-04
  • C# Redis學(xué)習(xí)系列(二)Redis基本設(shè)置

    C# Redis學(xué)習(xí)系列(二)Redis基本設(shè)置

    這篇文章主要為大家分享了C# Redis學(xué)習(xí)系列教程第二篇, Redis基本設(shè)置,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • C#字符串中去除多余的空格保留一個的實例

    C#字符串中去除多余的空格保留一個的實例

    下面小編就為大家分享一篇C#字符串中去除多余的空格保留一個的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2017-12-12
  • C#中string用法實例詳解

    C#中string用法實例詳解

    這篇文章主要介紹了C#中string用法,非常詳細的總結(jié)了比較常見的關(guān)于C#中string的幾個常用方法,需要的朋友可以參考下
    2014-08-08
  • C#多線程學(xué)習(xí)之(一)多線程的相關(guān)概念分析

    C#多線程學(xué)習(xí)之(一)多線程的相關(guān)概念分析

    這篇文章主要介紹了C#多線程學(xué)習(xí)之多線程的相關(guān)概念,涉及C#中多線程的相關(guān)概念與使用技巧,非常具有實用價值,需要的朋友可以參考下
    2015-04-04
  • c#實現(xiàn)metro文件壓縮解壓示例

    c#實現(xiàn)metro文件壓縮解壓示例

    這篇文章主要介紹了c#實現(xiàn)metro文件壓縮解壓示例,實現(xiàn)了zip中增加一張新圖片、刪除文件的方法,需要的朋友可以參考下
    2014-03-03
  • C#中數(shù)組初始化、反轉(zhuǎn)和排序用法實例

    C#中數(shù)組初始化、反轉(zhuǎn)和排序用法實例

    這篇文章主要介紹了C#中數(shù)組初始化、反轉(zhuǎn)和排序用法,涉及C#中數(shù)組常見的定義、初始化、排序等操作技巧,非常具有實用價值,需要的朋友可以參考下
    2015-04-04
  • c#哈希算法的實現(xiàn)方法及思路

    c#哈希算法的實現(xiàn)方法及思路

    這篇文章主要介紹了c#哈希算法的實現(xiàn)方法及思路,有需要的朋友可以參考一下
    2013-12-12
  • C# 編譯生成dll文件供程序調(diào)用的兩種方法

    C# 編譯生成dll文件供程序調(diào)用的兩種方法

    這篇文章主要介紹了C# 編譯生成dll文件供程序調(diào)用的兩種方法,需要的朋友可以參考下
    2018-03-03
  • 最新評論

    伊川县| 美姑县| 镇康县| 修武县| 富平县| 苗栗市| 五莲县| 边坝县| 福贡县| 高阳县| 湾仔区| 沐川县| 郧西县| 阿拉善右旗| 望都县| 银川市| 平乐县| 如东县| 武强县| 璧山县| 闸北区| 呼图壁县| 娱乐| 府谷县| 全州县| 新巴尔虎左旗| 六安市| 嘉善县| 阿瓦提县| 广元市| 桦川县| 三台县| 观塘区| 休宁县| 西乡县| 双城市| 肇庆市| 托克逊县| 连江县| 陆丰市| 黄龙县|