Word中設置水印時,可使用預設的文字或自定義文字設置為水印效果,但通常添加水印效果時,會對所有頁面都設置成統一效果,如果需要對每一頁或者某個頁面設置不同的水印效果,則可以參考本文中的方法。下麵,將以C# 代碼為例,對Word每一頁設置不同的文字水印效果作詳細介紹。 方法思路 在給Word每一頁添加 ...
Word中設置水印時,可使用預設的文字或自定義文字設置為水印效果,但通常添加水印效果時,會對所有頁面都設置成統一效果,如果需要對每一頁或者某個頁面設置不同的水印效果,則可以參考本文中的方法。下麵,將以C# 代碼為例,對Word每一頁設置不同的文字水印效果作詳細介紹。
方法思路
在給Word每一頁添加文字水印前,首先需要在Word文檔每一頁正文的最後一個字元後面插入“連續”分節符,然後在每一節的頁眉段落里添加藝術字形狀,並設置形狀大小、對齊方式等。最後保存文檔。
dll引用
方法1
在程式中引入Spire.Doc.dll文件;將Spire.Doc for .NET下載到本地,解壓,找到BIN文件夾下的Spire.Doc.dll。然後在Visual Studio中打開“解決方案資源管理器”,滑鼠右鍵點擊“引用”,“添加引用”,將本地路徑BIN文件夾下的dll文件添加引用至程式。
方法2
通過NuGet安裝。可通過以下2種方法安裝:
1.可以在Visual Studio中打開“解決方案資源管理器”,滑鼠右鍵點擊“引用”,“管理NuGet包”,然後搜索“Spire.Doc”,點擊“安裝”。等待程式安裝完成。
2.將以下內容複製到PM控制台安裝。
Install-Package Spire.Doc -Version 10.1.14
代碼示例
給每頁添加文字水印時,可參考如下步驟:
- 創建Document類的對象,並通過LoadFromFile(string fileName)方法載入Word文檔。
- 通過Document.Sections[]屬性獲取指定節。
- 通過HeadersFooters.Header屬性獲取頁眉,HeaderFooter.AddParagraph()方法添加段落到頁眉。
- 創建ShapeObject類的對象,並傳入參數設置形狀類型為TextPlainText類型的藝術字。並調用方法設置藝術字樣式,如藝術字高度、寬度、旋轉、顏色、對齊方式等。
- 使用DocumentObjectCollection.Add(IDocumentObject)方法將藝術字添加到段落。
- 最後,通過Document.SaveToFile(string fileName, FileFormat fileFormat)方法保存文檔。
不同頁面中設置不一樣的文字水印效果,只需要獲取該頁面對應的節,然後參考上述用到的方法來添加即可。
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"); //獲取文檔第一節 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();//重新添加段落 //添加藝術字並設置大小 ShapeObject shape1 = new ShapeObject(doc, ShapeType.TextPlainText); shape1.Width = 362; shape1.Height = 118; //設置藝術字文本內容、位置及樣式(即文本水印字樣) shape1.Rotation = 315; shape1.WordArt.Text = "內部使用"; 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); //同理設置第二節頁眉中的文字水印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); //同理設置第三節中的頁眉中的文字水印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") '獲取文檔第一節 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 shape1 As New ShapeObject(doc, ShapeType.TextPlainText) shape1.Width = 362 shape1.Height = 118 '設置藝術字文本內容、位置及樣式(即文本水印字樣) shape1.Rotation = 315 shape1.WordArt.Text = "內部使用" 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) '同理設置第二節頁眉中的文字水印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) '同理設置第三節中的頁眉中的文字水印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
如圖,每一頁均可顯示不同的文字水印效果:
★相關推薦閱讀:
—END—