Products Technologies Demo Docs Blog Support Company

Adding Watermarks to PDF Documents in .NET C#

This article shows how to add watermarks to PDF documents using TX Text Control .NET Server. It discusses how to use SVG images as watermarks and how to position them in the document.

Adding Watermarks to PDF Documents in .NET C#

Watermarks are a fundamental feature in document processing, widely used for branding, copyright protection, confidentiality, and visual enhancement. Whether it's a company logo subtly appearing in the background of an official report or a bold "CONFIDENTIAL" stamp overlaying a classified document, watermarks add both security and professionalism.

When exporting documents to PDF, adding scalable, high-quality watermarks is critical. Scalable Vector Graphics (SVG) watermarks offer a distinct advantage over raster images because they render sharply at any zoom level without loss of quality. This makes them ideal for professional document workflows.

In this article, we'll explore how to insert SVG watermarks into documents using TX Text Control to ensure that they remain sharp and properly positioned when exported to PDF.

Watermarks in TX Text Control

Inserting SVG Watermarks

To add watermarks to documents, images are inserted into the page headers of each section. The contents of the headers and footers are automatically repeated on each page of the document. Watermark objects can be positioned behind the actual text. The text is still visible, and the watermark can be seen virtually in the background of a document.

To accomplish this, use the following Images.Add method implementation:

public bool Add(Image image,
                int pageNumber, 
                System.Drawing.Point location,
                ImageInsertionMode insertionMode);

FixedOnPage and BelowTheText are the required enumeration combination for InsertionMode:

hf.Images.Add(image,
              1,
              new System.Drawing.Point(0, 0),
              TXTextControl.ImageInsertionMode.FixedOnPage |
              TXTextControl.ImageInsertionMode.BelowTheText);

If there are different page sizes and orientations, the image must be centered vertically and horizontally on each page of each section. The location of the background images is calculated using the following formula:

([Page width] - [image width] - [both page margins]) / 2

Calculating the Exact Position

The diagram below is an illustration of the various values that must be taken into account in the calculation of the exact horizontal position:

Positioning of Watermarks in TX Text Control

Code Example

The following code loops through all sections to add a header with a background image that is automatically repeated on all pages.

public void AddWatermark(TXTextControl.ServerTextControl tx, string imagePath)
{
    tx.PageUnit = TXTextControl.MeasuringUnit.Twips;

    TXTextControl.Image watermarkImage = new TXTextControl.Image { FileName = imagePath };

    foreach (TXTextControl.Section section in tx.Sections)
    {
        // Remove existing headers for a clean state
        section.HeadersAndFooters.Remove(TXTextControl.HeaderFooterType.All);

        // Add new header to the section
        section.HeadersAndFooters.Add(TXTextControl.HeaderFooterType.Header);
        TXTextControl.HeaderFooter header = section.HeadersAndFooters.GetItem(TXTextControl.HeaderFooterType.Header);

        // Insert watermark image into the header
        header.Images.Add(
            watermarkImage,
            1,
            new System.Drawing.Point(0, 0),
            TXTextControl.ImageInsertionMode.FixedOnPage | TXTextControl.ImageInsertionMode.BelowTheText);

        // Compute centered location
        int locationX = (int)(section.Format.PageSize.Width - watermarkImage.Size.Width
            - section.Format.PageMargins.Left - section.Format.PageMargins.Right) / 2;

        int locationY = (int)(section.Format.PageSize.Height - watermarkImage.Size.Height
            - section.Format.PageMargins.Top - section.Format.PageMargins.Bottom) / 2;

        // Set the computed location
        watermarkImage.Location = new System.Drawing.Point(locationX, locationY);
    }
}

This code creates a document with 3 pages and includes the watermark in it.

using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl())
{
    try
    {
        tx.Create();

        // Set text content
        tx.Text = "This is a test.\fNew page\fAnother page";

        // Apply watermark
        AddWatermark(tx, "draft.svg");

        tx.Save("results.pdf", TXTextControl.StreamType.AdobePDF);
    }
    catch (Exception ex)
    {
        Console.WriteLine($"Error: {ex.Message}");
    }
}

When the document is exported to PDF, the watermark is rendered as a vector graphic, so it remains sharp and clear at any zoom level.

Watermarks in TX Text Control

Conclusion

Adding watermarks to documents is a common requirement in professional document workflows. Using scalable vector graphics (SVG) for watermarks ensures that watermarks remain crisp and clear at any zoom level when exported to PDF. TX Text Control provides a powerful API for inserting SVG watermarks into documents, making it easy to create professional-looking documents with high-quality watermarks.

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

ASP.NET

Integrate document processing into your applications to create documents such as PDFs and MS Word documents, including client-side document editing, viewing, and electronic signatures.

ASP.NET Core
Angular
Blazor
JavaScript
React
  • Angular
  • Blazor
  • React
  • JavaScript
  • ASP.NET MVC, ASP.NET Core, and WebForms

Learn more Trial token Download trial

Related Posts

ASP.NETAIASP.NET Core

Automating PDF/UA Accessibility with AI: Describing DOCX Documents Using TX…

This article shows how to use TX Text Control together with the OpenAI API to automatically add descriptive texts (alt text and labels) to images, links, and tables in a DOCX. The resulting…


ASP.NETASP.NET CoreJava

Converting Office Open XML (DOCX) to PDF in Java

Learn how to convert Office Open XML (DOCX) documents to PDF in Java using the powerful ServerTextControl library. This guide provides step-by-step instructions and code examples to help you…


ASP.NETASP.NET CoreDS Server

Extending DS Server with Custom Digital Signature APIs

In this article, we will explore how to extend the functionality of DS Server by integrating custom digital signature APIs. We will cover the necessary steps to create a plugin that allows DS…


ASP.NETASP.NET CorePDF

Why PDF/UA and PDF/A-3a Matter: Accessibility, Archiving, and Legal Compliance

It is more important than ever to ensure that documents are accessible, archivable, and legally compliant. PDF/UA and PDF/A-3a are two effective standards for addressing these needs. This article…


ASP.NETASP.NET CoreMarkdown

Convert Markdown to PDF in a Console Application on Linux and Windows

Learn how to convert Markdown files to PDF in a console application on Linux and Windows using TX Text Control .NET Server for ASP.NET. This tutorial provides step-by-step instructions and code…