文档彩票走势图>>E-iceblue中文文档>>在 Word 中插入水印
在 Word 中插入水印
水印是在 Word 文档的背景中以淡色或灰色显示的文本或图像。它们可用于声明文档的机密性、版权或其他属性,或者仅用作装饰以使文档更具吸引力。本文展示了一种在Spire.Doc for .NET的帮助下在 Word 文档中插入水印的简单方法,包括文本水印和图像水印。
为 .NET 安装 Spire.Doc
首先,您需要将 Spire.Doc for.NET 包中包含的 DLL 文件添加为 .NET 项目中的引用。DLL 文件可以从此链接下载或通过NuGet安装。
PM> Install-Package Spire.Doc
在 Word 文档中插入文本水印
详细步骤如下:
- 创建Document类的对象。
- 使用Document.LoadFromFile()方法从磁盘加载 Word 文档。
- 使用自定义方法InsertTextWatermark()在文档中插入文本水印。
- 使用Doucment.SaveToFile()方法保存文档。
【C#】
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 文档中插入图像水印
详细步骤如下:
- 创建Document类的对象。
- 使用Document.LoadFromFile()方法从磁盘加载 Word 文档。
- 使用自定义方法InsertImageWatermark()在文档中插入图像水印。
- 使用Document.SaveToFile()方法保存文档。
【C#】
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