Watermark (2)
An image watermark is usually a logo or sign that appears on the background of digital documents, indicating the copyright owner of the content. Watermarking your PDF document with an image can prevent your data from being reused or modified. This article demonstrates how to add an image watermark to PDF in C# and VB.NET using Spire.PDF for .NET.
Install Spire.PDF for .NET
To begin with, you need to add the DLL files included in the Spire.PDF for.NET package as references in your .NET project. The DLLs files can be either downloaded from this link or installed via NuGet.
- Package Manager
PM> Install-Package Spire.PDF
Add an Image Watermark to PDF
The following are the main steps to add an image watermark to a PDF document.
- Create a PdfDocument object, and load a sample PDF file using PdfDocument.LoadFromFile() method.
- Load an image file using Image.FromFile() method.
- Loop through the pages in the document, and get the specific page through PdfDocument.Pages[] property.
- Set the image as background/watermark image of the current page through PdfPageBase.BackgroundImage property. Set the image position and size through PdfPageBase.BackgroundRegion property.
- Save the document to a different PDF file using PdfDocument.SaveToFile() method.
- C#
- VB.NET
using Spire.Pdf;
using System.Drawing;
namespace AddImageWatermark
{
class Program
{
static void Main(string[] args)
{
//Create a PdfDocument object
PdfDocument document = new PdfDocument();
//Load a sample PDF document
document.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.pdf");
//Load an image
Image image = Image.FromFile(@"C:\Users\Administrator\Desktop\logo.png");
//Get the image width and height
int imgWidth = image.Width;
int imgHeight = image.Height;
//Loop through the pages
for (int i = 0; i < document.Pages.Count; i++)
{
//Get the page width and height
float pageWidth = document.Pages[i].ActualSize.Width;
float pageHeight = document.Pages[i].ActualSize.Height;
//Set the background opacity
document.Pages[i].BackgroudOpacity = 0.3f;
//Set the background image of current page
document.Pages[i].BackgroundImage = image;
//Position the background image at the center of the page
Rectangle rect = new Rectangle((int)(pageWidth - imgWidth) / 2, (int)(pageHeight - imgHeight) / 2, imgWidth, imgHeight);
document.Pages[i].BackgroundRegion = rect;
}
//Save the document to file
document.SaveToFile("AddImageWatermark.pdf");
document.Close();
}
}
}
Imports Spire.Pdf
Imports System.Drawing
Namespace AddImageWatermark
Class Program
Shared Sub Main(ByVal args() As String)
'Create a PdfDocument object
Dim document As PdfDocument = New PdfDocument()
'Load a sample PDF document
document.LoadFromFile("C:\Users\Administrator\Desktop\sample.pdf")
'Load an image
Dim image As Image = Image.FromFile("C:\Users\Administrator\Desktop\logo.png")
'Get the image width and height
Dim imgWidth As Integer = image.Width
Dim imgHeight As Integer = image.Height
'Loop through the pages
Dim i As Integer
For i = 0 To document.Pages.Count- 1 Step i + 1
'Get the page width and height
Dim pageWidth As single = document.Pages(i).ActualSize.Width
Dim pageHeight As single = document.Pages(i).ActualSize.Height
'Set the background opacity
document.Pages(i).BackgroudOpacity = 0.3f
'Set the background image of current page
document.Pages(i).BackgroundImage = image
Dim rect As Rectangle = New Rectangle(CInt((pageWidth - imgWidth) / 2), CInt((pageHeight - imgHeight) / 2), imgWidth, imgHeight)
document.Pages(i).BackgroundRegion = rect
Next
'Save the document to file
document.SaveToFile("AddImageWatermark.pdf")
document.Close()
End Sub
End Class
End Namespace

Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

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.