DEV Community

jelizaveta
jelizaveta

Posted on

C# Operate Word Watermarks: Add Text, Add Image, Remove—All in One Guide

If you build enterprise software, you’ll eventually face document automation—especially with Word files. Business documents often need visual status and protection cues like:

  • Confidential / Restricted labels
  • Draft indicators
  • Copyright or ownership marks
  • Approval signatures and brand logos

A watermark is ideal because it doesn’t block reading, yet clearly communicates the document’s attributes.

In this post, I’ll show you how to do three common watermark tasks with Spire.Doc for .NET using clean C#:

  1. Add a text watermark
  2. Add an image watermark
  3. Remove an existing watermark

1. Overview of Spire.Doc for .NET Component

Spire.Doc for .NET is a professional Word document manipulation component. It allows developers to create, read, modify, and convert Word documents directly through code without installing Microsoft Office. The component provides full watermark handling capabilities and supports two main types of watermarks:

Watermark Type Applicable Scenarios Core Class
Text Watermark Confidentiality labels, draft status, copyright statements TextWatermark
Picture Watermark Company logos, signatures, graphical identifiers PictureWatermark

Unlike complex solutions that simulate watermarks by manipulating headers/footers or inserting shapes, Spire.Doc includes a dedicated watermark object model, making watermark addition and management concise and standardized.

Environment Setup

Before starting to code, you need to install Spire.Doc into your project using the NuGet package manager. In Visual Studio’s “Package Manager Console”, run the following command:

Install-Package Spire.Doc
Enter fullscreen mode Exit fullscreen mode

After installation, you need to import the following namespaces in your code file:

using Spire.Doc;
using Spire.Doc.Documents;
using System.Drawing;  // Used for colors and image operations
Enter fullscreen mode Exit fullscreen mode

2. Add a Text Watermark

A text watermark is the most basic and commonly used form. In essence, a text watermark renders a string of text in a semi-transparent (or specified color) style onto the background layer of every page of the document. The text typically uses a large font size and is arranged diagonally or horizontally, visually distinguishing it from the main content.

Key Property Explanation

The TextWatermark class provides the following configurable properties:

Property Type Description
Text string The watermark text content to display, such as “Confidential” or “DO NOT COPY”
FontSize int Font size of the watermark text, commonly set to 40–80
Color Color Watermark text color; it’s recommended to choose a soft or semi-transparent tone
Layout WatermarkLayout Arrangement direction:Diagonal(diagonal) orHorizontal(horizontal)

Code Implementation

using System;
using System.Drawing;
using Spire.Doc;
using Spire.Doc.Documents;

namespace InsertTextWatermark
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // Create a Document instance
            Document document = new Document();

            // Load the Word document from disk
            document.LoadFromFile("input.docx");

            // Insert a text watermark
            InsertTextWatermark(document);

            // Save the document
            document.SaveToFile("TextWatermark.docx", FileFormat.Docx);
        }

        private static void InsertTextWatermark(Document document)
        {
            TextWatermark txtWatermark = new TextWatermark();
            txtWatermark.Text = "DO NOT COPY";           // Watermark text content
            txtWatermark.FontSize = 50;                   // Font size
            txtWatermark.Color = Color.Blue;              // Text color
            txtWatermark.Layout = WatermarkLayout.Diagonal; // Diagonal layout
            document.Watermark = txtWatermark;
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Important Notes

  • A watermark is associated with the entire document object, not with a specific page or section. This means that after you add the watermark once, it will automatically be applied to all pages of the document.
  • When saving, you need to explicitly specify the format as FileFormat.Docx to ensure watermark information is correctly written into the Word file structure.
  • If you want the watermark to be displayed centered horizontally, set the Layout property to WatermarkLayout.Horizontal.

3. Add an Image Watermark

Compared with text watermarks, image watermarks are suitable for richer scenarios:

  • Enterprises add the company logo as a watermark to internal documents
  • Design teams overlay copyright identifier images on design drafts
  • Use signature images as approval watermarks in documents

Key Property Explanation

The PictureWatermark class provides the following configurable properties:

Property Type Description
Picture Image The image object to display; supports PNG, JPG, BMP, GIF, etc.
Scaling int Scaling percentage relative to the original size, e.g., 200 means enlarge to 200%
IsWashout bool truefor a washout (faded) effect;falsekeeps the original colors

Code Implementation

using System;
using System.Drawing;
using Spire.Doc;
using Spire.Doc.Documents;

namespace InsertImageWatermark
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // Create a Document instance
            Document document = new Document();

            // Load the Word document
            document.LoadFromFile("Input.docx");

            // Insert an image watermark
            InsertImageWatermark(document);

            // Save the document
            document.SaveToFile("InsertImageWatermark.docx", FileFormat.Docx);        
        }

        private static void InsertImageWatermark(Document document)
        {
            PictureWatermark picture = new PictureWatermark();
            picture.Picture = Image.FromFile("watermark.png");  // Load the image file
            picture.Scaling = 200;                               // Scaling ratio (%)
            picture.IsWashout = false;                           // Whether to use a washout effect
            document.Watermark = picture;
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Two Visual Effects of Image Watermarks

Setting Method IsWashout = true IsWashout = false
Visual Effect Image is faded and semi-transparent, similar to a background watermark Image keeps its original color saturation
Applicable Scenarios Background decoration that does not interfere with reading the main content Clearly showcases logo or signature details
Typical Uses General document copyright labeling Formal contracts and approval documents

4. Remove Watermarks

A document’s lifecycle often goes through multiple stages, and watermark requirements are not permanent. For example, a document in the review stage may need a prominent “Confidential” watermark, but once the review is completed and it is officially published, the watermark may become redundant and needs to be removed.

Design Principle

The design for removing watermarks in Spire.Doc is straightforward: the Document.Watermark property is essentially an object reference. When it points to a TextWatermark or PictureWatermark instance, it indicates that the document currently has a watermark enabled. After setting this reference to a null reference, the watermark is detached from the document.

Code Implementation

using Spire.Doc;

namespace RemoveWatermark
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a Document instance
            Document doc = new Document();

            // Load a Word document that contains a watermark
            doc.LoadFromFile("Input.docx");

            // Remove the watermark from the document
            doc.Watermark = null;

            // Save the result document
            doc.SaveToFile("RemoveWatermark.docx", FileFormat.Docx2013);
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

⚠️ Notes

This removal approach is intended for standard watermarks added via Spire.Doc. If the “watermark” in the document was implemented using other non-standard methods (for example, inserting a semi-transparent shape in the header, or embedding a background image directly), then you cannot handle it using this method and must adopt other document content removal strategies.

5. Summary

With Spire.Doc for .NET, developers can use concise and standardized C# code to add and remove Word document watermarks. The core operations covered in this article are summarized below:

Operation Core Code Key Class
Add Text Watermark document.Watermark = new TextWatermark() TextWatermark
Add Image Watermark document.Watermark = new PictureWatermark() PictureWatermark
Remove Watermark document.Watermark = null -

Recommended Use Cases

  • Text Watermarks : Suitable for scenarios where watermark text needs to be generated dynamically, such as displaying different confidentiality levels based on user permissions.
  • Image Watermarks : Suitable for brand-standardized scenarios, such as using a company logo as the document background.
  • Remove Watermarks : Suitable for document handoff scenarios, such as removing watermarks before external release when documents go through internal review stages.

This solution does not rely on Office software environments. It is suitable for deployment in server-side applications or background services, offering good stability and extensibility.

Top comments (0)