C#實(shí)現(xiàn)給Word每一頁設(shè)置不同圖片水印
Word中設(shè)置水印時(shí),可加載圖片設(shè)置為水印效果,但通常添加水印效果時(shí),會(huì)對所有頁面都設(shè)置成統(tǒng)一效果,如果需要對每一頁或者某個(gè)頁面設(shè)置不同的水印效果,則可以參考本文中的方法。下面,將以C#代碼為例,對Word每一頁設(shè)置不同的圖片水印效果作詳細(xì)介紹。
方法思路
在給Word每一頁添加水印前,首先需要在Word文檔每一頁正文的最后一個(gè)字符后面插入“連續(xù)”分節(jié)符,然后在每一節(jié)的頁眉段落里添加水印圖片,并設(shè)置圖片的坐標(biāo)位置、對齊方式、襯于文字下方等。最后保存文檔。
dll引入
方法1
在程序中引入Spire.Doc.dll文件;將 Free Spire.Doc for .NET 下載到本地,解壓,找到BIN文件夾下的Spire.Doc.dll。然后在Visual Studio中打開“解決方案資源管理器”,鼠標(biāo)右鍵點(diǎn)擊“引用”,“添加引用”,將本地路徑BIN文件夾下的dll文件添加引用至程序。
方法2
通過 NuGet 安裝??赏ㄟ^以下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.0
代碼示例
給每頁添加圖片水印時(shí),可參考如下步驟:
- 創(chuàng)建Document類的對象,并通過LoadFromFile(string fileName)方法加載Word文檔。
- 通過Document.Sections[]屬性獲取指定節(jié)。
- 通過HeadersFooters.Header屬性獲取頁眉,HeaderFooter.AddParagraph()方法添加段落到頁眉。
- 通過Paragraph.AppendPicture(string imgFile)方法添加圖片到段落,DocPicture.VerticalPosition屬性設(shè)置水印圖片位置,DocPicture.HorizontalAlignment屬性設(shè)置圖片對齊方式。
- 最后,通過Document.SaveToFile(string fileName, FileFormat fileFormat)方法保存文檔。
不同頁面中設(shè)置不一樣的圖片水印效果,只需要獲取該頁面對應(yīng)的節(jié),然后參考上述用到的方法來添加即可。
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"); //獲取文檔第一節(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();//重新添加段落 DocPicture pic1 = para1.AppendPicture("logo1.png");//添加圖片 pic1.TextWrappingStyle = TextWrappingStyle.Behind;//圖片置于文字下方 pic1.VerticalPosition = y; pic1.HorizontalAlignment = ShapeHorizontalAlignment.Center;//設(shè)置圖片對齊方式 //同理設(shè)置第二節(jié)頁眉中的圖片水印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; //同理設(shè)置第三節(jié)中的頁眉中的圖片水印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") '獲取文檔第一節(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() '重新添加段落 Dim pic1 As DocPicture = para1.AppendPicture("logo1.png") '添加圖片 pic1.TextWrappingStyle = TextWrappingStyle.Behind '圖片置于文字下方 pic1.VerticalPosition = y pic1.HorizontalAlignment = ShapeHorizontalAlignment.Center '設(shè)置圖片對齊方式 '同理設(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 pic2 As DocPicture = para2.AppendPicture("logo2.png") pic2.TextWrappingStyle = TextWrappingStyle.Behind pic2.VerticalPosition = y pic2.HorizontalAlignment = ShapeHorizontalAlignment.Center '同理設(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 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
如圖,每一頁均可顯示不同的圖片水印效果:
以上就是C#實(shí)現(xiàn)給Word每一頁設(shè)置不同圖片水印的詳細(xì)內(nèi)容,更多關(guān)于C# Word設(shè)置圖片水印的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
c#實(shí)現(xiàn)網(wǎng)站監(jiān)控查看是否正常示例
這篇文章主要介紹了使用c#監(jiān)控網(wǎng)站是否正常的功能示例,大家參考使用吧2014-01-01c#調(diào)用api控制windows關(guān)機(jī)示例(可以重啟/注銷)
本文介紹了c#控制windows關(guān)機(jī)、重啟、注銷的二種方法,分為調(diào)用windows自帶的shutdown.exe關(guān)機(jī)和調(diào)用API關(guān)機(jī)的方法2014-01-01Unity游戲開發(fā)中必備的設(shè)計(jì)模式之外觀模式詳解
外觀模式是一種結(jié)構(gòu)型設(shè)計(jì)模式,為復(fù)雜系統(tǒng)提供了簡單的接口,使得子系統(tǒng)間的通信更加簡潔和易于維護(hù)。在Unity游戲開發(fā)中,外觀模式可以幫助開發(fā)者更好地管理游戲?qū)ο蠛徒M件等復(fù)雜結(jié)構(gòu)2023-05-05ListView Adapter優(yōu)化 實(shí)例
ListView Adapter優(yōu)化 實(shí)例,需要的朋友可以參考一下2013-04-04C#實(shí)現(xiàn)延時(shí)并自動(dòng)關(guān)閉MessageBox的方法
這篇文章主要介紹了C#實(shí)現(xiàn)延時(shí)并自動(dòng)關(guān)閉MessageBox的方法,非常實(shí)用的功能,需要的朋友可以參考下2014-08-08WPF使用WrapPanel實(shí)現(xiàn)虛擬化效果
這篇文章主要為大家詳細(xì)介紹了如何利用WPF WrapPanel實(shí)現(xiàn)虛擬化效果,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)或工作有一定幫助,感興趣的小伙伴可以了解一下2022-09-09C#關(guān)鍵字in、out、ref的作用與區(qū)別
這篇文章介紹了C#關(guān)鍵字in、out、ref的作用與區(qū)別,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04C#實(shí)現(xiàn)啟用與禁用本地網(wǎng)絡(luò)的方式小結(jié)【3種方式】
這篇文章主要介紹了C#實(shí)現(xiàn)啟用與禁用本地網(wǎng)絡(luò)的方式,結(jié)合實(shí)例形式總結(jié)分析了使用Hnetcfg.dll、Shell32.dll及setupapi.dll三種啟用與禁用本地網(wǎng)絡(luò)的操作方法,需要的朋友可以參考下2016-07-07C#使用Interlocked實(shí)現(xiàn)線程同步
今天小編就為大家分享一篇關(guān)于C#使用Interlocked實(shí)現(xiàn)線程同步,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-10-10