跳過到頁腳內容
使用 IRONWORD

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

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

How to Add Watermark to A Word File in C#: Figure 1 - IronWord: The C# DOCX Library

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.

How to Add Watermark to A Word File in C#: Figure 2 - Input Image

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. 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.
  2. We load our input image from above into a new Image class.
  3. 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.
  4. We add the image watermark to the document using AddImage.
  5. Finally, we save and export the document documentImageResized as a Word file.

The output of the Word document is below.

How to Add Watermark to A Word File in C#: Figure 3 - Output Word Document

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

How to Add Watermark to A Word File in C#: Figure 4 - IronWord licensing information

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屬性定位在文本後面。

IronWord 與哪些 .NET 版本兼容?

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 在佛罗里达州迈阿密长大,曾在佛罗里达大学学习计算机科学和统计学。