
Adding watermarks to PDF documents is a common requirement for protecting sensitive information, indicating document status, or branding files. Watermarks can enhance the security of your documents by discouraging unauthorized copying or distribution. In this article, we'll explore how to add text watermarks to PDFs in C# using Spire.PDF for .NET, a powerful library designed for PDF manipulation.
- Getting Started with Spire.PDF for .NET
- Understanding the Coordinate System
- Step-by-Step Guide: Adding a Text Watermark to PDF in C#
- Conclusion
- FAQs
Getting Started with Spire.PDF for .NET
Spire.PDF for .NET is a robust API that allows developers to create, edit, and manipulate PDF documents programmatically. It supports a wide range of features, including:
- PDF generation and conversion
- Text and image extraction
- Digital signatures
- Watermarking
Before proceeding, ensure you have installed the Spire.PDF NuGet package. You can install it via the NuGet Package Manager in Visual Studio:
Install-Package Spire.PDF
Alternatively, download it from our official website and reference the DLL file mannually.
Understanding the Coordinate System
Before writing code, it's crucial to understand how PDFs handle coordinates. In Spire.PDF:
- The origin (0, 0) is at the top-left corner of the page.
- The X-axis increases from left to right.
- The Y-axis increases from top to bottom.
- Measurements are in points (72 points = 1inch).
This coordinate system is essential when positioning watermarks, text, or images on a PDF page.

Step-by-Step Guide: Adding a Text Watermark to PDF in C#
Let’s break down the process of adding a watermark to a PDF document using Spire.PDF.
Step 1: Import Namespaces
First, include the necessary namespaces. These namespaces provide the essential classes needed for PDF manipulation and graphics rendering, enabling you to work with PDF files in your C# application.
using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;
Step 2: Load the PDF Document
This step involves initializing a new PdfDocument object and loading the PDF file you want to modify, making it ready for further operations.
PdfDocument doc = new PdfDocument();
doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Input.pdf");
Step 3: Customize a Function to Add Watermaks
Define a helper function to apply a watermark to a single page. This function is designed to position and draw the watermark text on the page, ensuring it is centered and visually appealing, while also applying transparency for a subtle effect.
private static void AddWatermark(PdfPageBase page, string watermarkText, PdfTrueTypeFont font, PdfBrush brush, float opacity)
{
page.Canvas.SetTransparency(opacity);
SizeF textSize = font.MeasureString(watermarkText);
float pageWidth = page.ActualSize.Width;
float pageHeight = page.ActualSize.Height;
float x = (pageWidth - textSize.Width) / 2;
float y = (pageHeight - textSize.Height) / 2;
page.Canvas.DrawString(watermarkText, font, brush, x, y);
}
Step 4: Apply the Watermark to All Pages
In this step, you set up the font, color, and opacity for the watermark. The loop iterates through each page in the document, applying the watermark consistently across all pages.
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial Black", 50f), true);
PdfBrush brush = PdfBrushes.Blue;
string watermarkText = "DO NOT COPY";
float opacify = 0.6f;
foreach (PdfPageBase page in doc.Pages)
{
AddWatermark(page, watermarkText, font, brush, opacify);
}
Step 5: Save the Modifyed PDF
Finally, save the document under a new filename, ensuring that your watermark is preserved in the final output.
doc.SaveToFile("Watermark.pdf");
Result:

Conclusion
Adding watermarks to PDFs in C# is straightforward with Spire.PDF for .NET . By understanding the coordinate system and using the library’s drawing capabilities, you can customize watermarks with different fonts, colors, and transparency levels. This method is useful for document security, branding, or marking drafts.
FAQs:
Q1. How do I rotate the watermark?
To add a diagonal watermark to a PDF in C#, you can rotate the coordinate system before drawing the text. Here is the code for your reference:
page.Canvas.RotateTransform(-45);
page.Canvas.DrawString(watermarkText, font, brush, x, y);
Q2. How can I adjust the watermark position?
Change the x and y values in DrawString() to reposition the watermark.
Q3. Can I add an image watermark instead of text?
Yes, you can add an image watermark instead of text. Use PdfPage.Canvas.DrawImage() for rendering the image directly on the page. Alternatively, set a background image for each page using the PdfPageBase.BackgroundImage property.
Q4: How do I add handwritten text to a PDF?
You can create an image of your handwritten text, and then draw it on each page as an image watermark.
Get a Free License
To fully experience the capabilities of Spire.PDF for .NET without any evaluation limitations, you can request a free 30-day trial license.
