How to Add Watermarks to PDF Documents Using C#
Source: Dev.to
Why Add Watermarks to PDFs
- Copyright protection – Declare document ownership and deter unauthorized copying.
- Document status indication – e.g., “DRAFT,” “APPROVED,” or “VOID.”
- Confidentiality marking – e.g., “CONFIDENTIAL,” “TOP SECRET,” “INTERNAL USE ONLY.”
- Anti‑forgery tracking – Embed QR codes, serial numbers, or other tracking markers.
- Branding or personalization – Add company logos or personalized identifiers.
Choosing a PDF Library for C#
| Library | Type | Features | License | Best Use Case |
|---|---|---|---|---|
| Spire.PDF | Commercial | Easy to use, intuitive API, supports conversion, forms, images | Commercial | Fast integration with full 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 (Spire.PDF)
Install the library via NuGet:
Install-Package Spire.PDF
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}");
}
}
Steps
- Load the PDF with
PdfDocument. - Create a font and a semi‑transparent brush.
- Iterate through each page and add a
PdfTextWatermark(centered, rotated ‑45°). - Save the modified document.
Adding an Image Watermark (Spire.PDF)
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();
}
}
}
Steps
- Load the source PDF.
- Load the watermark image and obtain its dimensions.
- For each page, set
BackgroudOpacity(e.g., 0.3) and assignBackgroundImage. - Center the image using a
Rectangleplaced at the middle of the page. - Save the resulting PDF.
Conclusion
Using libraries such as Spire.PDF, you can quickly implement both text and image watermarks in C# applications. These techniques help enforce copyright protection, indicate document status, embed branding, and improve overall document security and traceability.