How to insert a text watermark in the document using C# and .NET

  1. Add SautinSoft.Document from Nuget.
  2. Load a DOCX document.
  3. Create a watermark.
  4. Insert the watermark into the document header.

This code example shows how to insert a text watermark into an existing example.docx document at a specified place.

Download the resulting file: result.docx

Complete code

using System;
using System.IO;
using System.Linq;
using SautinSoft.Document;
using SautinSoft.Document.Drawing;

namespace Example
{
    class Program
    {
        static void Main(string[] args)
        {
            // Get your free 100-day key here:   
            // https://sautinsoft.com/start-for-free/

            InsertTextAsWatermark();
        }
        /// <summary>
        /// How to insert a text watermark in the existing PDF, DOCX, any document to the each page.
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/document/help/net/developer-guide/insert-text-watermark-in-the-existing-pdf-docx-document-to-each-page-net-csharp-vb.php
        /// </remarks>
        static void InsertTextAsWatermark()
        {
            // Let us say, we want to insert a textual "Watermark" at the each page of the
            // DOCX document and specify angle of 45 degree for it.
            
            // If we'll insert the text at the document header (or footer), so it will appear on the each page.
            
            // Also let's insert our Watermark behind the main content.
            string inpFile = @"..\..\..\example.docx";
            string outFile = @"Result.docx";

            // 1. Load an existing DOCX document.
            DocumentCore dc = DocumentCore.Load(inpFile);

            // 2. Create a Shape with our Watermark text.
            // Place the watermark:
            // 30mm - from the page left;            
            // 150mm - from the page top.
            // 60 - angle.
            float posFromLeft = 30f;
            float posFromTop = 150f;
            float angle = -60f;
            // Size of the Shape, 200mm x 40mm.            
            SautinSoft.Document.Drawing.Size size = new Size(200f, 40f, LengthUnit.Millimeter);
            Shape watermark = new Shape(dc, new FloatingLayout(new HorizontalPosition(posFromLeft, LengthUnit.Millimeter, HorizontalPositionAnchor.Page),
            new VerticalPosition(posFromTop, LengthUnit.Millimeter, VerticalPositionAnchor.Page), size));
            // Rotate shape.
            watermark.Rotation = angle;
            // Create the text.
            Run text = new Run(dc, "Watermark!", new CharacterFormat()
            {
                Size = 100f,
                FontColor = Color.Black,
                FontName = "Arial"
            });
            watermark.Text.Blocks.Add(new Paragraph(dc, text));
            // Set shape Behind the main document contents.
            (watermark.Layout as FloatingLayout).WrappingStyle = WrappingStyle.BehindText;
            // Remove the shape borders.
            watermark.Outline.Fill.SetEmpty();

            // 3. Iterate through Sections, and insert our Watermark to the default header of the each section.
            foreach (Section section in dc.Sections)
            {
                // 2.1. Check the document header, maybe is it already exist?
                var header = dc.Sections[0].HeadersFooters[HeaderFooterType.HeaderDefault];

                if (header == null)
                {
                    // Create a new header, add it into the section.
                    header = new HeaderFooter(dc, HeaderFooterType.HeaderDefault);
                    // Add the header to the section.
                    section.HeadersFooters.Add(header);
                }
                // Add the watermark to the header.
                header.Content.End.Insert(watermark.Content);
            }

            // 4. Save the document back.
            dc.Save(outFile);
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(outFile) { UseShellExecute = true });
        }
    }
}

Download

Imports System
Imports System.IO
Imports System.Linq
Imports SautinSoft.Document
Imports SautinSoft.Document.Drawing

Module Sample
    Sub Main()
        InsertTextAsWatermark()
    End Sub
    ''' Get your free 100-day key here:   
    ''' https://sautinsoft.com/start-for-free/
    ''' <summary>
    ''' How to insert a text watermark in the existing PDF, DOCX, any document to the each page.
    ''' </summary>
    ''' <remarks>
    ''' Details: https://sautinsoft.com/products/document/help/net/developer-guide/insert-text-watermark-in-the-existing-pdf-docx-document-to-each-page-net-csharp-vb.php
    ''' </remarks>
    Sub InsertTextAsWatermark()
        ' Let us say, we want to insert a textual "Watermark" at the each page of the
        ' DOCX document and specify angle of 45 degree for it.

        ' If we'll insert the text at the document header (or footer), so it will appear on the each page.

        ' Also let's insert our Watermark behind the main content.
        Dim inpFile As String = "..\..\..\example.docx"
        Dim outFile As String = "Result.docx"

        ' 1. Load an existing DOCX document.
        Dim dc As DocumentCore = DocumentCore.Load(inpFile)

        ' 2. Create a Shape with our Watermark text.
        ' Place the watermark:
        ' 30mm - from the page left;            
        ' 150mm - from the page top.
        ' 60 - angle.
        Dim posFromLeft As Single = 30.0F
        Dim posFromTop As Single = 150.0F
        Dim angle As Single = -60.0F
        ' Size of the Shape, 200mm x 40mm.            
        Dim size As SautinSoft.Document.Drawing.Size = New Size(200.0F, 40.0F, LengthUnit.Millimeter)
        Dim watermark As New Shape(dc, New FloatingLayout(New HorizontalPosition(posFromLeft, LengthUnit.Millimeter, HorizontalPositionAnchor.Page), New VerticalPosition(posFromTop, LengthUnit.Millimeter, VerticalPositionAnchor.Page), size))
        ' Rotate shape.
        watermark.Rotation = angle
        ' Create the text.
        Dim text As New Run(dc, "Watermark!", New CharacterFormat() With {
                .Size = 100.0F,
                .FontColor = Color.Black,
                .FontName = "Arial"
            })
        watermark.Text.Blocks.Add(New Paragraph(dc, text))
        ' Set shape Behind the main document contents.
        TryCast(watermark.Layout, FloatingLayout).WrappingStyle = WrappingStyle.BehindText
        ' Remove the shape borders.
        watermark.Outline.Fill.SetEmpty()

        ' 3. Iterate through Sections, and insert our Watermark to the default header of the each section.
        For Each section As Section In dc.Sections
            ' 2.1. Check the document header, maybe is it already exist?
            Dim header = dc.Sections(0).HeadersFooters(HeaderFooterType.HeaderDefault)

            If header Is Nothing Then
                ' Create a new header, add it into the section.
                header = New HeaderFooter(dc, HeaderFooterType.HeaderDefault)
                ' Add the header to the section.
                section.HeadersFooters.Add(header)
            End If
            ' Add the watermark to the header.
            header.Content.End.Insert(watermark.Content)
        Next section

        ' 4. Save the document back.
        dc.Save(outFile)
        System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(outFile) With {.UseShellExecute = True})
    End Sub

End Module

Download


If you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page) or use the Form below:



Questions and suggestions from you are always welcome!

We are developing .Net components since 2002. We know PDF, DOCX, RTF, HTML, XLSX and Images formats. If you need any assistance with creating, modifying or converting documents in various formats, we can help you. We will write any code example for you absolutely free.