使用 IRONWORD 如何在 C# 中向 Word 文件添加水印 Jordi Bardia 已更新:七月 28, 2025 Download IronWord NuGet 下载 Start Free Trial Copy for LLMs Copy for LLMs Copy page as Markdown for LLMs Open in ChatGPT Ask ChatGPT about this page Open in Gemini Ask Gemini about this page Open in Grok Ask Grok about this page Open in Perplexity Ask Perplexity about this page Share Share on Facebook Share on X (Twitter) Share on LinkedIn Copy URL Email article In today's modern enterprise, Word Documents are synonymous with information and carry important information between divisions and companies. However, with digital documents, there is also the risk of someone tampering with or forging an invalid or fake Word document. As such, one way to combat it is to add watermarks to Word documents. However, this doesn't increase the security of a Word document. A watermark allows primary differentiation between the actual Word document and the forged one. Furthermore, adding a watermark programmatically is quite challenging, as most instances require users to manually use Microsoft Word and add a watermark on each document, making it a painstakingly long process. Fortunately, IronWord can programmatically add images as watermarks in Word. This allows developers to add them in the pipeline programmatically, reducing repetitive work and increasing efficiency and consistency. Although there are various kinds of watermarks, such as shape watermark and text watermark, this article will only use image and picture watermarks, discuss IronWord as a library, and provide practical examples of adding images to the Word document. Adding Watermarks to Word Documents IronWord IronWord is a reliable and easy-to-use C# Docx library that allows developers to build and edit Word documents using C# without relying on traditional dependencies such as Microsoft Office or Word Interop. Furthermore, it has extensive documentation and fully supports .NET 8, 7, 6, Framework, Core, and Azure. This makes it cross-platform compatible with most applications and flexible no matter the application you are dealing with. Working with Image Watermarks License Key Please remember that IronWord requires a licensing key for operation. You can get a key as part of a free trial by visiting this link. // Replace the license key variable with the trial key you obtained IronWord.License.LicenseKey = "REPLACE-WITH-YOUR-KEY"; // Replace the license key variable with the trial key you obtained IronWord.License.LicenseKey = "REPLACE-WITH-YOUR-KEY"; ' Replace the license key variable with the trial key you obtained IronWord.License.LicenseKey = "REPLACE-WITH-YOUR-KEY" $vbLabelText $csharpLabel After receiving a trial key, set this variable in your project. Add Image Watermark to Word Document Let's examine an example of adding an image watermark to a Word Document. We won't mention static void main to reduce repetitive code but will showcase only the main code. We will use this image as the watermark image and add it to the Word document. using IronWord; using IronWord.Models; using IronWord.Models.Enums; // Set the license key IronWord.License.LicenseKey = "YOUR-KEY"; // Create a new Word document WordDocument doc = new WordDocument(); // Load the image to be used as a watermark IronWord.Models.Image image = new IronWord.Models.Image("ironword.png"); // Set the width and height of the image image.Width = 500; // In pixels image.Height = 250; // In pixels // Add the image as a watermark to the document doc.AddImage(image); // Save the document as a .docx file doc.SaveAs("documentImageResized.docx"); using IronWord; using IronWord.Models; using IronWord.Models.Enums; // Set the license key IronWord.License.LicenseKey = "YOUR-KEY"; // Create a new Word document WordDocument doc = new WordDocument(); // Load the image to be used as a watermark IronWord.Models.Image image = new IronWord.Models.Image("ironword.png"); // Set the width and height of the image image.Width = 500; // In pixels image.Height = 250; // In pixels // Add the image as a watermark to the document doc.AddImage(image); // Save the document as a .docx file doc.SaveAs("documentImageResized.docx"); Imports IronWord Imports IronWord.Models Imports IronWord.Models.Enums ' Set the license key IronWord.License.LicenseKey = "YOUR-KEY" ' Create a new Word document Dim doc As New WordDocument() ' Load the image to be used as a watermark Dim image As New IronWord.Models.Image("ironword.png") ' Set the width and height of the image image.Width = 500 ' In pixels image.Height = 250 ' In pixels ' Add the image as a watermark to the document doc.AddImage(image) ' Save the document as a .docx file doc.SaveAs("documentImageResized.docx") $vbLabelText $csharpLabel We first create a new instance of the WordDocument. This is the document class from IronWord. In the next steps, we create a new variable called doc and add the picture watermark to it. We load our input image from above into a new Image class. We set the width and height of the image accordingly. This width and height property allows users to accomplish image watermark scaling. In our example, the width is set to 500, and the height is set to 250. We add the image watermark to the document using AddImage. Finally, we save and export the document documentImageResized as a Word file. The output of the Word document is below. Keep in mind that the dimensions set in the example are made to fit the whole Word document and display IronWord's capabilities. In addition to adding the image, we can use the WrapText property to ensure that the image watermark stays behind the text in the background. // Set the image to wrap behind the text image.WrapText = WrapText.BehindText; // Set the image to wrap behind the text image.WrapText = WrapText.BehindText; ' Set the image to wrap behind the text image.WrapText = WrapText.BehindText $vbLabelText $csharpLabel Through this property, the image watermark should wrap behind the text, only showing in the background, allowing the text to be unhindered. Furthermore, developers can try more customizations for the image watermark to make it unique. IronWord allows you to offset the dimensions of the image relative to the Word document and manage the position of the image watermark as well. using IronWord; using IronWord.Models; // Set the license key IronWord.License.LicenseKey = "YOUR-KEY"; // Create a new Word document WordDocument doc = new WordDocument(); // Load the image to be used as a watermark IronWord.Models.Image image = new IronWord.Models.Image("ironword.png"); // Create an ElementPosition object to set the position ElementPosition elementPosition = new ElementPosition(); elementPosition.SetXPosition(50); elementPosition.SetYPosition(50); // Set the dimensions of the image image.Width = 50; // In pixels image.Height = 50; // In pixels // Set the image position using the ElementPosition object image.Position = elementPosition; // Offset the image from each side by 100 pixels image.DistanceFromTop = 100; image.DistanceFromBottom = 100; image.DistanceFromLeft = 100; image.DistanceFromRight = 100; using IronWord; using IronWord.Models; // Set the license key IronWord.License.LicenseKey = "YOUR-KEY"; // Create a new Word document WordDocument doc = new WordDocument(); // Load the image to be used as a watermark IronWord.Models.Image image = new IronWord.Models.Image("ironword.png"); // Create an ElementPosition object to set the position ElementPosition elementPosition = new ElementPosition(); elementPosition.SetXPosition(50); elementPosition.SetYPosition(50); // Set the dimensions of the image image.Width = 50; // In pixels image.Height = 50; // In pixels // Set the image position using the ElementPosition object image.Position = elementPosition; // Offset the image from each side by 100 pixels image.DistanceFromTop = 100; image.DistanceFromBottom = 100; image.DistanceFromLeft = 100; image.DistanceFromRight = 100; Imports IronWord Imports IronWord.Models ' Set the license key IronWord.License.LicenseKey = "YOUR-KEY" ' Create a new Word document Dim doc As New WordDocument() ' Load the image to be used as a watermark Dim image As New IronWord.Models.Image("ironword.png") ' Create an ElementPosition object to set the position Dim elementPosition As New ElementPosition() elementPosition.SetXPosition(50) elementPosition.SetYPosition(50) ' Set the dimensions of the image image.Width = 50 ' In pixels image.Height = 50 ' In pixels ' Set the image position using the ElementPosition object image.Position = elementPosition ' Offset the image from each side by 100 pixels image.DistanceFromTop = 100 image.DistanceFromBottom = 100 image.DistanceFromLeft = 100 image.DistanceFromRight = 100 $vbLabelText $csharpLabel Similar to the code above, we set the dimensions of the image's x and y locations to 50. We also offset the dimensions from each side by 100px to create a margin around it. Conclusion Throughout the examples, we demonstrated how straightforward it is to use the IronWord library to manipulate and read Word documents programmatically in C#. The library's flexibility and scalability make it a valuable tool that allows developers to use IronWord in practical, real-life examples, such as adding a watermark and a text watermark. It's essential to grasp how Word works with other applications because it provides developers with additional solutions to their challenges. IronWord offers a free trial license. 常见问题解答 如何通过编程方式在 C# 中向 Word 文档添加水印? 使用 IronWord,开发人员可以通过创建一个 Word 文档实例、加载图像,并使用 AddImage 方法将其插入为水印,从而向 Word 文档添加图像水印。 在 Word 文档中使用水印有什么好处? 水印通过标记文件为草稿、机密或定稿,帮助区分真实文件和伪造文件,并增强文档管理。 IronWord 如何在 Word 文档中处理图像水印? IronWord 允许开发人员加载图像,设置其尺寸和位置,并将其作为水印添加到 Word 文档中。可以使用 WrapText.BehindText 属性将图像放置在文本后面。 哪些版本的 .NET 与 IronWord 兼容? IronWord 支持 .NET 8、7、6、.NET Framework、.NET Core 和 Azure,使其高度多功能且兼容跨平台。 使用 IronWord 是否需要许可证? 是的,IronWord 需要许可证密钥才能完全使用功能。可以从 IronWord 网站获取试用密钥以进行初始评估。 如何使用 IronWord 自定义 Word 文档中图像水印的位置? IronWord 允许通过设置 x 和 y 坐标并调整尺寸,以特定像素值从每侧偏移图像来自定义图像水印的位置。 IronWord 可以用于向 Word 文档添加文字水印吗? 虽然文章重点讨论图像水印,但 IronWord 也可以通过将文本渲染为图像并以类似于图像水印的方式应用来添加文本水印。 该文章为使用 IronWord 提供了哪些实用示例? 文章提供了创建 Word 文档、加载图像作为水印、调整其尺寸并将其位置设置在文本后面以展示 IronWord 功能的示例。 Jordi Bardia 立即与工程团队聊天 软件工程师 Jordi 最擅长 Python、C# 和 C++,当他不在 Iron Software 利用这些技能时,他就在游戏编程。分享产品测试、产品开发和研究的责任,Jordi 在持续的产品改进中增加了巨大的价值。多样的经验使他面临挑战并保持投入,他表示这是在 Iron Software 工作的最喜欢的方面之一。Jordi 在佛罗里达州迈阿密长大,并在佛罗里达大学学习计算机科学和统计学。 相关文章 已更新九月 18, 2025 ASP .NET Core 导入和导出 Word 文件 本指南探讨了如何使用 IronWord 库导入现有的 Word 文档、显示其内容并从头创建文档 阅读更多 已更新七月 28, 2025 VS 2022 编程创建新 Word 文档 (教程) 在今天的教程中,我将简要解释如何使用 IronWord 编程创建 Microsoft Word 文档并提供简短示例。 阅读更多 已更新六月 22, 2025 如何使用 C# 在 Word 中对齐文本 深入了解 IronWord NuGet 包以及使用该包对齐文本或段落的方法 阅读更多 如何使用 Word 模板在 C# 中生成 Word 文档如何在 C# 中从 Word 中提取文本
已更新七月 28, 2025 VS 2022 编程创建新 Word 文档 (教程) 在今天的教程中,我将简要解释如何使用 IronWord 编程创建 Microsoft Word 文档并提供简短示例。 阅读更多