在这篇操作指南文章中,我们将讨论使用一种流行的注释库使用 C# 在 Word 中添加水印的整个工作流程。本教程提供了一个代码示例,通过使用几个 API 调用使用 C# 在 Word 中插入水印。此外,我们将解释如何设置注释包和编写代码示例以在 DOCX 中插入格式化水印。以下是完整的说明以及工作示例。
使用 C# 在 Word 中添加水印的步骤
- 从 .NET 应用程序中的 NuGet 包管理器安装 GroupDocs.Annotation for .NET 包以在 Word 中插入水印
- 添加对 GroupDocs.Annotation 命名空间的引用以在 Word 中创建水印
- 通过传递输入 DOCX 路径初始化 Annotator 类
- 初始化 WatermarkAnnotation 类并设置水印的属性
- 调用Annotator类的Add方法,将WatermarkAnnotation对象传给它
- 最后调用Annotator.Save方法将Word文档保存到磁盘
上述步骤可帮助您使用 C#* 在 Word 中快速*创建水印,而无需在您的系统上设置任何额外的第三方工具。您可以在 Windows、macOS、Linux 和任何支持 .NET 的操作系统上按照这些步骤在 DOCX 中插入水印注释。以下代码示例根据 WatermarkAnnotation 的设置属性生成带水印的 Word 文档。
使用 C# 在 Word 中添加水印的代码
using System; | |
using GroupDocs.Annotation; | |
using GroupDocs.Annotation.Models; | |
using GroupDocs.Annotation.Models.AnnotationModels; | |
namespace AddWatermarkinWordUsingCSharp | |
{ | |
internal class Program | |
{ | |
static void Main(string[] args) | |
{ | |
// Set License to avoid the limitations of Annotation library | |
License lic = new License(); | |
lic.SetLicense(@"Conholdate.Annotator.lic"); | |
//Instantiate Annotator object and pass input Word document | |
using (Annotator annotator = new Annotator("input.docx")) | |
{ | |
//Instantiate WatermarkAnnotation object and set it's properties | |
WatermarkAnnotation watermark = new WatermarkAnnotation | |
{ | |
Angle = 75, | |
Box = new Rectangle(200, 200, 100, 50), | |
CreatedOn = DateTime.Now, | |
Text = "Watermark", | |
FontColor = 65535, | |
FontSize = 12, | |
Message = "This is watermark annotation", | |
Opacity = 0.7, | |
AutoScale = true, | |
HorizontalAlignment = HorizontalAlignment.Center, | |
VerticalAlignment = VerticalAlignment.Center | |
}; | |
//Add Watermark in Word document | |
annotator.Add(watermark); | |
//Save the final output DOCX | |
annotator.Save("result.docx"); | |
} | |
} | |
} | |
} |
上面的示例代码示例已构建为通过简单的步骤将水印添加到 C#* 中的 word 文档。您可以修改此代码示例并借助 WatermarkAnnotation 属性根据您的要求设置水印属性。最近,我们推出了一篇去除 PDF 水印注释的文章,请查看 如何使用C#从PDF中删除水印注释 指南了解详情。