Add Watermarks to Word Documents in C#
In daily office work, adding watermarks to Word documents is a common need — it may be to mark the "draft" status, indicate the "confidential" level, or use a company logo as a background for anti-counterfeiting. If you need to batch process documents, manual operation is too inefficient; automated processing with code is the way to go.
Today, I'll share a simple and efficient solution: using C# with the free library Free Spire.Doc for .NET to add watermarks to Word documents (including text watermarks and image watermarks). It has no dependency on Office components and is lightweight and easy to use.
1. Preparation: Get Free Spire.Doc
First, you need to install Free Spire.Doc for .NET. It is a free Word processing class library that can easily manipulate various elements of Word documents (including watermarks). You can install it directly via NuGet:
- Right-click the project in Visual Studio's "Solution Explorer"
- Select "Manage NuGet Packages", search for "FreeSpire.Doc", and click Install
2. Adding Text Watermarks: C# Code Example
Text watermarks are the most commonly used type (such as "Confidential", "Internal Use"). Free Spire.Doc provides the TextWatermark class, which allows you to directly set properties like text content, style, and layout.
Complete Code Example:
using System;
using Spire.Doc;
using Spire.Doc.Documents;
using System.Drawing;
namespace WordWatermarkDemo
{
class Program
{
static void Main(string[] args)
{
// 1. Load the Word document (supports .doc and .docx)
Document doc = new Document();
doc.LoadFromFile("Sample Document.docx");
// 2. Create a TextWatermark object
TextWatermark textWatermark = new TextWatermark();
// Set the watermark text content
textWatermark.Text = "Confidential Document";
// Set the font (name, size, color)
textWatermark.FontName = "Microsoft YaHei";
textWatermark.FontSize = 40;
textWatermark.Color = Color.LightGray; // Light gray does not affect the readability of the main text
// Set layout: Diagonal or Horizontal
textWatermark.Layout = WatermarkLayout.Diagonal;
// 3. Apply the watermark to the document
doc.Watermark = textWatermark;
// 4. Save the document
doc.SaveToFile("Document with Text Watermark.docx", FileFormat.Docx2013);
doc.Close();
}
}
}
3. Adding Image Watermarks
If you need to use images (such as company logos, copyright logos) as watermarks, Free Spire.Doc's PictureWatermark class can meet your needs, supporting settings for image path, scaling ratio, and transparency.
Complete Code Example:
using System;
using Spire.Doc;
using Spire.Doc.Documents;
namespace WordImageWatermarkDemo
{
class Program
{
static void Main(string[] args)
{
// 1. Load the Word document
Document doc = new Document();
doc.LoadFromFile("Sample Document.docx");
// 2. Create a PictureWatermark object
PictureWatermark pictureWatermark = new PictureWatermark();
// Set the image path (supports local images in formats such as .png, .jpg, etc.)
pictureWatermark.Picture = System.Drawing.Image.FromFile("logo.png"); // Replace with your image path
// Set the scaling ratio (100 is the original size; less than 100 for reduction, more than 100 for enlargement)
pictureWatermark.Scaling = 50; // Scale down to 50% to avoid blocking the main text
// Set the washout effect
pictureWatermark.IsWashout = false;
// 3. Apply the watermark to the document
doc.Watermark = pictureWatermark;
// 4. Save the document
doc.SaveToFile("Document with Image Watermark.docx", FileFormat.Docx2013);
doc.Close();
}
}
}
4. Notes
Free Version Limitations
The free version of Free Spire.Doc supports processing up to 500 paragraphs per document, with no watermarks of any kind, making it suitable for small or personal projects.Path Issues
When loading documents and images, it is recommended to use absolute paths (e.g.,@"C:\Files\Sample Document.docx") to avoid file not found errors due to incorrect relative paths.Watermark Coverage
The above code will add a watermark to the entire document. If you need to add a watermark to specific pages (e.g., the first page), you can obtain the specific section viadoc.Sections[0]and set the section's watermark separately.
5. Summary
Adding watermarks to Word documents in C# using Free Spire.Doc for .NET involves very clear steps: load the document → create a watermark object (text/image) → set properties → apply the watermark → save the document. Compared to relying on Office Interop (which requires Office installation and has poor performance), this solution is more lightweight and stable.