文檔首頁>>E-iceblue中文文檔>>在 Word 中插入水印
在 Word 中插入水印
水印是在 Word 文檔的背景中以淡色或灰色顯示的文本或圖像。它們可用于聲明文檔的機密性、版權(quán)或其他屬性,或者僅用作裝飾以使文檔更具吸引力。本文展示了一種在Spire.Doc for .NET的幫助下在 Word 文檔中插入水印的簡單方法,包括文本水印和圖像水印。
為 .NET 安裝 Spire.Doc
首先,您需要將 Spire.Doc for.NET 包中包含的 DLL 文件添加為 .NET 項目中的引用。DLL 文件可以從此鏈接下載或通過NuGet安裝。
PM> Install-Package Spire.Doc
在 Word 文檔中插入文本水印
詳細步驟如下:
- 創(chuàng)建Document類的對象。
- 使用Document.LoadFromFile()方法從磁盤加載 Word 文檔。
- 使用自定義方法InsertTextWatermark()在文檔中插入文本水印。
- 使用Doucment.SaveToFile()方法保存文檔。
【C?!?/span>
using System; using System.Drawing; using Spire.Doc; using Spire.Doc.Documents; namespace InsertImageWatermark { internal class Program { static void Main(string[] args) { //Create an object of Document class Document document = new Document(); //Load a Word document from disk document.LoadFromFile(@"D:\Samples\Sample.docx"); //Insert a text watermark InsertTextWatermark(document.Sections[0]); //Save the document document.SaveToFile("InsertTextWatermark.docx", FileFormat.Docx); } private static void InsertTextWatermark(Section section) { TextWatermark txtWatermark = new TextWatermark(); txtWatermark.Text = "DO NOT COPY"; txtWatermark.FontSize = 50; txtWatermark.Color = Color.Blue; txtWatermark.Layout = WatermarkLayout.Diagonal; section.Document.Watermark = txtWatermark; } } }
【VB.NET】
Imports System Imports System.Drawing Imports Spire.Doc Imports Spire.Doc.Documents Module Program Sub Main(args As String()) 'Create an object of Document class Dim document As New Document() 'Load a Word document from disk document.LoadFromFile("D:\Samples\Sample.docx") 'Insert a text watermark InsertTextWatermark(document.Sections(0)) 'Save the document document.SaveToFile("InsertTextWatermark.docx", FileFormat.Docx) End Sub Private Sub InsertTextWatermark(ByVal section As Section) Dim txtWatermark As New Spire.Doc.TextWatermark() txtWatermark.Text = "DO NOT COPY" txtWatermark.FontSize = 50 txtWatermark.Color = Color.Blue txtWatermark.Layout = WatermarkLayout.Diagonal section.Document.Watermark = txtWatermark End Sub End Module
在 Word 文檔中插入圖像水印
詳細步驟如下:
- 創(chuàng)建Document類的對象。
- 使用Document.LoadFromFile()方法從磁盤加載 Word 文檔。
- 使用自定義方法InsertImageWatermark()在文檔中插入圖像水印。
- 使用Document.SaveToFile()方法保存文檔。
【C?!?/span>
using System; using System.Drawing; using Spire.Doc; using Spire.Doc.Documents; namespace InsertWatermark { internal class Program { static void Main(string[] args) { //Create an object of Document class Document document = new Document(); //Load a Word document from disk document.LoadFromFile(@"D:\Samples\Sample.docx"); //Insert an image watermark InsertImageWatermark(document); //Save the document document.SaveToFile("InsertImageWatermark.docx", FileFormat.Docx); } private static void InsertImageWatermark(Document document) { PictureWatermark picture = new PictureWatermark(); picture.Picture = Image.FromFile(@"D:\Samples\Watermark.png"); picture.Scaling = 200; picture.IsWashout = false; document.Watermark = picture; } } }
【VB.NET】
Imports System Imports System.Drawing Imports Spire.Doc Imports Spire.Doc.Documents Module Program Sub Main(args As String()) 'Create an object of Document class Dim document As New Document() 'Load a Word document from disk document.LoadFromFile("D:\Samples\Sample.docx") 'Insert an image watermark InsertImageWatermark(document) 'Save the document document.SaveToFile("InsertImageWatermark.docx", FileFormat.Docx) End Sub Private Sub InsertImageWatermark(ByVal document As Document) Dim picture As New PictureWatermark() picture.Picture = Image.FromFile("D:\Samples\Watermark.png") picture.Scaling = 250 picture.IsWashout = False document.Watermark = picture End Sub End Module