C# 給Word每一頁設定不同圖片水印

jlttt發表於2022-02-23

Word中設定水印時,可載入圖片設定為水印效果,但通常新增水印效果時,會對所有頁面都設定成統一效果,如果需要對每一頁或者某個頁面設定不同的水印效果,則可以參考本文中的方法。下面,將以C#程式碼為例,對Word每一頁設定不同的圖片水印效果作詳細介紹。

方法思路

在給Word每一頁新增水印前,首先需要在Word文件每一頁正文的最後一個字元後面插入“連續”分節符,然後在每一節的頁首段落裡新增水印圖片,並設定圖片的座標位置、對齊方式、襯於文字下方等。最後儲存文件。

dll引入

方法1

在程式中引入Spire.Doc.dll檔案;將 Free Spire.Doc for .NET 下載到本地,解壓,找到BIN資料夾下的Spire.Doc.dll。然後在Visual Studio中開啟“解決方案資源管理器”,滑鼠右鍵點選“引用”,“新增引用”,將本地路徑BIN資料夾下的dll檔案新增引用至程式。

方法2

通過  NuGet 安裝。可通過以下2種方法安裝:

 1. 可以在Visual Studio中開啟“解決方案資源管理器”,滑鼠右鍵點選“引用”,“管理NuGet包”,然後搜尋“Free Spire.Doc”,點選“安裝”。等待程式安裝完成。

 2. 將以下內容複製到PM控制檯安裝。

Install-Package FreeSpire.Doc -Version 10.2.0

 

程式碼示例

給每頁新增圖片水印時,可參考如下步驟:

  • 建立Document類的物件,並通過LoadFromFile(string fileName)方法載入Word文件。
  • 通過Document.Sections[]屬性獲取指定節。
  • 通過HeadersFooters.Header屬性獲取頁首,HeaderFooter.AddParagraph()方法新增段落到頁首。
  • 通過Paragraph.AppendPicture(string imgFile)方法新增圖片到段落,DocPicture.VerticalPosition屬性設定水印圖片位置,DocPicture.HorizontalAlignment屬性設定圖片對齊方式。
  • 最後,通過Document.SaveToFile(string fileName, FileFormat fileFormat)方法儲存文件。

 

不同頁面中設定不一樣的圖片水印效果,只需要獲取該頁面對應的節,然後參考上述用到的方法來新增即可。

C#

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;

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

            //獲取文件第一節
            Section section1 = doc.Sections[0];

            //定義水印圖片的縱向座標位置
            float y = section1.PageSetup.PageSize.Height/3;

            //新增圖片水印1
            HeaderFooter header1 = section1.HeadersFooters.Header;//獲取頁首
            header1.Paragraphs.Clear();//刪除原有頁首格式的段落
            Paragraph para1 = header1.AddParagraph();//重新新增段落
            DocPicture pic1 = para1.AppendPicture("logo1.png");//新增圖片
            pic1.TextWrappingStyle = TextWrappingStyle.Behind;//圖片置於文字下方
            pic1.VerticalPosition = y;
            pic1.HorizontalAlignment = ShapeHorizontalAlignment.Center;//設定圖片對齊方式

            //同理設定第二節頁首中的圖片水印2
            Section section2 = doc.Sections[1];
            HeaderFooter header2 = section2.HeadersFooters.Header;
            header2.Paragraphs.Clear();
            Paragraph para2 = header2.AddParagraph();
            DocPicture pic2 = para2.AppendPicture("logo2.png");
            pic2.TextWrappingStyle = TextWrappingStyle.Behind;
            pic2.VerticalPosition = y;
            pic2.HorizontalAlignment = ShapeHorizontalAlignment.Center;

            //同理設定第三節中的頁首中的圖片水印3
            Section section3 = doc.Sections[2];
            HeaderFooter header3 = section3.HeadersFooters.Header;
            header3.Paragraphs.Clear();
            Paragraph para3 = header3.AddParagraph();
            DocPicture pic3 = para3.AppendPicture("logo3.png");
            pic3.TextWrappingStyle = TextWrappingStyle.Behind;
            pic3.VerticalPosition = y;
            pic3.HorizontalAlignment = ShapeHorizontalAlignment.Center;

            //儲存文件
            doc.SaveToFile("DifferentImageWatermark.docx", FileFormat.Docx2013);
            System.Diagnostics.Process.Start("DifferentImageWatermark.docx");
        }
    }
}

vb.net

Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields

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

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

            '定義水印圖片的縱向座標位置
            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()
            '重新新增段落
            Dim pic1 As DocPicture = para1.AppendPicture("logo1.png")
            '新增圖片
            pic1.TextWrappingStyle = TextWrappingStyle.Behind
            '圖片置於文字下方
            pic1.VerticalPosition = y
            pic1.HorizontalAlignment = ShapeHorizontalAlignment.Center
            '設定圖片對齊方式
            '同理設定第二節頁首中的圖片水印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 pic2 As DocPicture = para2.AppendPicture("logo2.png")
            pic2.TextWrappingStyle = TextWrappingStyle.Behind
            pic2.VerticalPosition = y
            pic2.HorizontalAlignment = ShapeHorizontalAlignment.Center

            '同理設定第三節中的頁首中的圖片水印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 pic3 As DocPicture = para3.AppendPicture("logo3.png")
            pic3.TextWrappingStyle = TextWrappingStyle.Behind
            pic3.VerticalPosition = y
            pic3.HorizontalAlignment = ShapeHorizontalAlignment.Center

            '儲存文件
            doc.SaveToFile("DifferentImageWatermark.docx", FileFormat.Docx2013)
            System.Diagnostics.Process.Start("DifferentImageWatermark.docx")
        End Sub
    End Class
End Namespace

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

 

—END—

 

相關文章