How to Add Watermarks to PDFs in C# Using IronPDF
A custom watermark is a personalized background image or text overlay added to a PDF page. It serves various purposes, including branding with logos or names, enhancing security with labels like 'Confidential,' ensuring copyright protection, and indicating document status. Custom watermarks can include text, images, or both, be applied selectively or universally, and their opacity can be adjusted for versatility in personalizing, securing, and contextualizing PDFs.
IronPDF offers a one-liner to add watermarks to PDF format documents. The watermark feature accepts an HTML string to generate the watermark, which is capable of using all HTML features as well as CSS styling.
Get started with IronPDF
Start using IronPDF in your project today with a free trial.
Steps to Apply Custom Watermarks in C#
- Download the IronPDF Library for C#
- Render a new or import an existing PDF document.
- Configure the HTML string to be used as a watermark.
- Use the
ApplyWatermark
method to implement a watermark. - Customize watermark rotation, opacity, and location as needed.
Apply Watermark Example
Utilize the ApplyWatermark
method to apply a watermark to a newly rendered PDF or an existing one. This method accepts an HTML string as the watermark, allowing it to have all the features that HTML offers, including CSS styling. Let's use both an image and text as our watermark in the example below. Please note that the watermark will be applied to all the pages; it's not possible to apply the watermark to specific pages.
Code
:path=/static-assets/pdf/content-code-examples/how-to/custom-watermark-apply-watermark.cs
using IronPdf;
// This code demonstrates how to create a PDF document and apply a watermark to it using IronPdf library.
// Define the HTML content for the watermark. The HTML can include both text and image elements.
string watermarkHtml = @"
<img src='https://ironsoftware.com/img/products/ironpdf-logo-text-dotnet.svg' alt='IronPDF Logo'>
<h1>Iron Software</h1>";
// Create an instance of ChromePdfRenderer, which allows rendering PDFs from HTML content.
ChromePdfRenderer renderer = new ChromePdfRenderer();
// Render a simple PDF document containing an H1 header with the word "Watermark".
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Watermark</h1>");
// Apply the watermark to the entire PDF document. The watermarkHtml will be overlaid on each page of the PDF.
pdf.WatermarkAllPages(new IronPdf.PdfDocumentOptions.HtmlStamp() {
Html = watermarkHtml,
Opacity = 100, // Set the opacity level of the watermark where 100 is fully visible
HorizontalAlignment = IronPdf.Rendering.HtmlWatermarkOptions.HorizontalAlign.Center,
VerticalAlignment = IronPdf.Rendering.HtmlWatermarkOptions.VerticalAlign.Middle
});
// Save the watermarked PDF document to a file named "watermark.pdf".
pdf.SaveAs("watermark.pdf");
Output PDF
This is a very easy way to add image watermark text from a variety of image formats, such as PNG, and text watermark with a custom font.
Watermark Opacity and Rotation
Add a watermark with the default opacity of 50%. This level can be further configured according to the user's requirements. The ApplyWatermark
method supports an overload that also accepts rotation as a parameter. By specifying 'rotation:' and 'opacity:', we can adjust these two parameters.
Code
:path=/static-assets/pdf/content-code-examples/how-to/custom-watermark-apply-rotation-opacity.cs
// Import necessary namespaces from IronPdf and its editing utilities
using IronPdf;
using IronPdf.Editing;
// Define the HTML content to be used as a watermark
string watermarkHtml = @"
<img style='width: 200px;' src='https://ironsoftware.com/img/products/ironpdf-logo-text-dotnet.svg'>
<h1>Iron Software</h1>";
// Create a new instance of ChromePdfRenderer which will be used to render HTML into PDF
ChromePdfRenderer renderer = new ChromePdfRenderer();
// Render a simple HTML content as PDF for adding watermark later
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Watermark</h1>");
// TextWatermarkOptions is meant for text; for HTML watermarks, we will use HtmlStampOptions.
// Correct usage involves creating an instance of HtmlStampOptions and then applying it.
HtmlStampOptions htmlStampOptions = new HtmlStampOptions
{
Rotation = 45, // Rotate the watermark by 45 degrees
Opacity = 70 // Set watermark opacity to 70%
};
// Apply a watermark to the PDF document with specified HTML.
// The watermark will be rotated by 45 degrees and have an opacity of 0.7 (70%).
pdf.ApplyStamp(watermarkHtml, htmlStampOptions);
// Save the watermarked PDF to a file
pdf.SaveAs("watermarkOpacityAndRotation.pdf");
Output PDF
Watermark Location on PDF file
To specify the watermark location, use a 3x3 grid divided into 3 columns horizontally and 3 rows vertically. The horizontal options are left, center, and right, while the vertical options are top, middle, and bottom. With this configuration, we can set 9 different locations on each page of the document. Please refer to the image below for a visual representation of this concept.

Add watermark to a specific location using the VerticalAlignment and HorizontalAlignment enums in the IronPdf.Editing
namespace.
Code
:path=/static-assets/pdf/content-code-examples/how-to/custom-watermark-apply-watermark-top-right.cs
using IronPdf;
using IronPdf.Editing;
// Define HTML content to be used as a watermark. This includes an image and header text.
string watermarkHtml = @"
<img style='width: 200px;' src='https://ironsoftware.com/img/products/ironpdf-logo-text-dotnet.svg'>
<h1>Iron Software</h1>";
// Create an instance of ChromePdfRenderer to render HTML content as PDF.
ChromePdfRenderer renderer = new ChromePdfRenderer();
// Render a simple HTML snippet into a PDF document.
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>Watermark</h1>");
// Apply the defined watermark to the document. It is positioned at the top-right corner of each page.
// '50' represents the opacity level, where 100 is fully opaque and 0 is fully transparent.
// Note: Ensure that the watermark method provided supports these parameters.
pdf.WatermarkHTML(watermarkHtml, opacity: 50, verticalAlignment: VerticalAlignment.Top, horizontalAlignment: HorizontalAlignment.Right);
// Save the resulting PDF document with the watermark to a file named "watermarkLocation.pdf".
pdf.SaveAs("watermarkLocation.pdf");
Output PDF
Frequently Asked Questions
What is a custom PDF watermark?
A custom PDF watermark is a personalized background image or text overlay added to a PDF page. It serves purposes like branding, security, copyright protection, and indicating document status.
How can I apply a custom watermark using IronPDF?
IronPDF allows you to apply a custom watermark by using an HTML string with the `ApplyWatermark` method. This allows the use of HTML features and CSS styling for the watermark.
Can I apply a watermark to specific pages in a PDF using IronPDF?
No, the watermark will be applied to all pages of the PDF; it is not possible to apply the watermark to specific pages using IronPDF.
How can I customize the opacity and rotation of a watermark in IronPDF?
You can customize the opacity and rotation of a watermark in IronPDF by using an overload of the `ApplyWatermark` method that accepts parameters for these properties.
What are the steps to apply custom watermarks in C# using IronPDF?
The steps include downloading the IronPDF library, rendering or importing a PDF document, configuring the HTML string as a watermark, using the `ApplyWatermark` method, and customizing the watermark as needed.
How do I specify the location of a watermark on a PDF page using IronPDF?
You can specify the watermark location using a 3x3 grid with horizontal options (left, center, right) and vertical options (top, middle, bottom) by using the `VerticalAlignment` and `HorizontalAlignment` enums from the `IronPdf.Editing` namespace.
What formats can be used for image watermarks in IronPDF?
Image watermarks in IronPDF can be created from various image formats, such as PNG.
Does IronPDF support CSS styling in watermark HTML strings?
Yes, IronPDF supports CSS styling in the HTML strings used for creating watermarks.
Can I use both images and text in a single watermark with IronPDF?
Yes, you can use both images and text together in a single watermark by configuring the HTML string accordingly.
Where can I download the IronPDF library for C#?
You can download the IronPDF library for C# from the NuGet package manager.