DEV Community

Leon Davis
Leon Davis

Posted on

How to Add Watermarks to PDF Documents Using C#

PDF documents are widely used across industries due to their stability, cross-platform compatibility, and high security. As PDF files become the go-to format for sharing and storing important information, protecting these documents from unauthorized distribution and ensuring proper ownership attribution has become increasingly important. In business, legal, financial, and administrative environments, adding watermarks to PDFs is one of the most effective ways to indicate ownership, highlight confidentiality, and prevent misuse.

This article provides a comprehensive guide for C# developers on how to add different types of watermarks—such as text watermarks and image watermarks—to PDF documents in a .NET environment. We will walk through the technical essentials and provide full code examples to help you implement watermark features quickly and efficiently.

Why Add Watermarks to PDF Files?

A PDF watermark overlays text, images, or patterns onto a document to indicate ownership, restrict unauthorized usage, or communicate important information about the document’s status. Watermarks are not merely decorative—they serve as an important security layer that helps prevent tampering and trace document origins.

Common use cases include:

  • Copyright protection – Declare document ownership and prevent unauthorized copying.
  • Document status indication – For example: “DRAFT,” “APPROVED,” or “VOID.”
  • Confidentiality marking – Such as “CONFIDENTIAL,” “TOP SECRET,” or “INTERNAL USE ONLY.”
  • Anti-forgery tracking – Embed QR codes, serial numbers, or tracking markers.
  • Branding or personalization – Add company logos or personalized identifiers.

Choosing a PDF Library in C

When working with PDF files in C#, developers typically rely on third-party libraries to implement watermark functionality. The most commonly used libraries include both open-source and commercial tools. This article focuses on Spire.PDF and iText7, two widely adopted options in the C# ecosystem.

Comparison of Mainstream PDF Libraries

Library Type Features License Best Use Case
Spire.PDF Commercial Easy to use, intuitive API, supports conversion, forms, images Commercial Fast integration with complete functionality
iText7 Open-source/Commercial Powerful, enterprise-grade creation/editing/encryption/signing AGPLv3 / Commercial Complex enterprise-level customization

Note: iText7’s AGPLv3 license requires your application to be open-sourced if distributed. For commercial use, a paid license is needed.

Adding a Text Watermark Using Spire.PDF

In this section, we'll use Spire.PDF to show how to add a text watermark to a PDF. Below is a step-by-step example.

1. Install Spire.PDF

Install the library via NuGet:

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

2. Basic Example: Adding a Text Watermark

The following code adds a diagonal, semi-transparent text watermark—“Confidential”—to every page:

using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;

public class PdfWatermark
{
    public static void AddTextWatermark(string inputFilePath, string outputFilePath, string watermarkText)
    {
        PdfDocument doc = new PdfDocument();
        doc.LoadFromFile(inputFilePath);

        PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 24, FontStyle.Bold));
        PdfSolidBrush brush = new PdfSolidBrush(new PdfRGBColor(Color.LightGray));

        foreach (PdfPageBase page in doc.Pages)
        {
            PdfTextWatermark watermark = new PdfTextWatermark(watermarkText)
            {
                Font = font,
                Brush = brush,
                StringFormat = new PdfStringFormat(PdfTextAlignment.Center),
                RotateAngle = -45
            };

            page.AddWatermark(watermark);
        }

        doc.SaveToFile(outputFilePath);
        doc.Close();
    }

    public static void Main(string[] args)
    {
        string inputPdf = "input.pdf"; 
        string outputPdf = "TextWatermark.pdf"; 
        string watermarkText = "Confidential";

        AddTextWatermark(inputPdf, outputPdf, watermarkText);
        Console.WriteLine($"Watermark added to {outputPdf}");
    }
}
Enter fullscreen mode Exit fullscreen mode

How This Works

  • Load PDF using PdfDocument
  • Set up font & brush
  • Loop through pages and apply watermark
  • Save the output file

Adding an Image Watermark Using Spire.PDF

Image watermarks are ideal for branding, anti-forgery, or embedding company logos. Below is a complete example of adding an image watermark to every page.

1. Basic Example: Adding an Image Watermark

using Spire.Pdf;
using System.Drawing;

namespace AddImageWatermark
{
    class Program
    {
        static void Main(string[] args)
        {
            PdfDocument document = new PdfDocument();
            document.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.pdf");

            Image image = Image.FromFile(@"C:\Users\Administrator\Desktop\logo.png");

            int imgWidth = image.Width;
            int imgHeight = image.Height;

            for (int i = 0; i < document.Pages.Count; i++)
            {
                float pageWidth = document.Pages[i].ActualSize.Width;
                float pageHeight = document.Pages[i].ActualSize.Height;

                document.Pages[i].BackgroudOpacity = 0.3f;
                document.Pages[i].BackgroundImage = image;

                Rectangle rect = new Rectangle(
                    (int)(pageWidth - imgWidth) / 2,
                    (int)(pageHeight - imgHeight) / 2,
                    imgWidth,
                    imgHeight);

                document.Pages[i].BackgroundRegion = rect;
            }

            document.SaveToFile("ImageWatermark.pdf");
            document.Close();
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

How This Works

  • Load input PDF
  • Load watermark image
  • Retrieve image dimensions
  • Set background opacity + background image
  • Center the image manually
  • Save the file

Conclusion

This article explained how to add both text and image watermarks to PDF documents using C#. With libraries like Spire.PDF, implementing watermark protection becomes simple and flexible. Whether your goal is copyright protection, classification, branding, or anti-tampering, these techniques greatly enhance your document's security and traceability.

Top comments (0)