跳至页脚内容
使用 IRONWORD

如何在 C# 中向 Word 文件添加水印

在当今的现代企业中,Word 文档是信息的代名词,并在部门和公司之间传递重要信息。 但是,使用电子文档也存在有人篡改或伪造无效或虚假 Word 文档的风险。

因此,对抗这种现象的一种方法是给 Word 文档添加水印。 然而,这并不会提高 Word 文档的安全性。 水印能够有效地区分真正的 Word 文档和伪造的文档。 此外,以编程方式添加水印相当具有挑战性,因为大多数情况下都需要用户手动使用 Microsoft Word 并在每个文档上添加水印,这使得整个过程非常漫长而痛苦。

幸运的是, IronWord可以通过编程方式在 Word 中添加图像作为水印。 这样一来,开发人员就可以以编程方式将它们添加到流程中,从而减少重复性工作,提高效率和一致性。

虽然水印有各种类型,例如形状水印和文本水印,但本文仅使用图像水印,以 IronWord 库为例进行讨论,并提供向 Word 文档中添加图像的实用示例。

在 Word 文档中添加水印

IronWord

如何在 C# 中向 Word 文件添加水印:图 1 - IronWord:C# DOCX 库

IronWord 是一个可靠且易于使用的 C# Docx 库,它允许开发人员使用 C# 构建和编辑 Word 文档,而无需依赖 Microsoft Office 或 Word Interop 等传统依赖项。

此外,它还拥有丰富的文档,并完全支持 .NET 8、7、6、Framework、Core 和 Azure。 这使得它能够跨平台兼容大多数应用程序,并且无论您正在处理什么应用程序,它都具有灵活性。

使用图像水印

许可证密钥

请注意,IronWord 需要许可证密钥才能运行。 您可以访问此链接,作为免费试用的一部分获得密钥。

// 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

收到试用密钥后,请在项目中设置此变量。

在 Word 文档中添加图像水印

让我们来看一个在 Word 文档中添加图像水印的例子。 为了减少重复代码,我们将不提及 static void main,而只展示主代码。

我们将使用此图像作为水印图像并将其添加到 Word 文档中。

如何在 C# 中向 Word 文件添加水印:图 2 - 输入图像

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
  1. 我们首先创建一个新的WordDocument实例。 这是 IronWord 中的文档类。 接下来,我们将创建一个名为doc新变量,并将图片水印添加到该变量中。
  2. 我们将上面输入的图像加载到一个新的Image类中。
  3. 我们相应地设置图像的宽度和高度。 此宽度和高度属性允许用户实现图像水印缩放。 在我们的示例中,宽度设置为 500,高度设置为 250。
  4. 我们使用AddImage将图像水印添加到文档中。
  5. 最后,我们将文档documentImageResized保存并导出为 Word 文件。

Word文档的输出结果如下。

如何在 C# 中向 Word 文件添加水印:图 3 - 输出 Word 文档

请注意,示例中设置的尺寸是为了适应整个 Word 文档并展示 IronWord 的功能。

除了添加图像之外,我们还可以使用WrapText属性来确保图像水印保持在背景文本的后面。

// 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

通过此属性,图像水印应环绕在文本后面,仅在背景中显示,从而使文本不受阻碍。

此外,开发者还可以尝试对图像水印进行更多自定义,使其独一无二。 IronWord 允许您调整图像相对于 Word 文档的尺寸,并管理图像水印的位置。

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

与上面的代码类似,我们将图像的 x 和 y 坐标尺寸设置为 50。我们还将图像的每一边偏移 100px,以在其周围创建边距。

结论

如何在 C# 中向 Word 文件添加水印:图 4 - IronWord 许可信息

在这些示例中,我们演示了使用IronWord库以编程方式在 C# 中操作和读取 Word 文档是多么简单。 该库的灵活性和可扩展性使其成为一个有价值的工具,允许开发人员在实际的、真实的示例中使用 IronWord,例如添加水印和文本水印。 了解 Word 如何与其他应用程序协同工作至关重要,因为它能为开发人员提供应对挑战的更多解决方案。

IronWord 提供免费试用许可证

常见问题解答

如何通过编程方式在 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 在佛罗里达州迈阿密长大,并在佛罗里达大学学习计算机科学和统计学。