Watermarking
IronPDF provides methods to 'watermark' PDF documents with HTML.
Using the ApplyStamp
method, developers can add an HTML-based watermark to a PDF file. As shown in the example above, the HTML code for the watermark goes as the first argument to the method. Additional arguments to ApplyStamp
control the rotation, opacity, and position of the watermark.
Utilize the ApplyStamp
method in lieu of the ApplyWatermark
method for more granular control over watermark placement. For example, use ApplyStamp
to:
- Add Text, Image, or HTML watermarks to PDFs
- Apply the same watermark to every page of the PDF
- Apply different watermarks to specific PDF pages
- Adjust the placement of watermarks in front or behind page copy
- Adjust the opacity, rotation, and alignment of watermarks with more precision
How to Add Watermarks to PDF Files in C#
- Download and install the IronPDF library from NuGet.
- Create a new
PdfDocument
or use an existingPdfDocument
file. - Call the
ApplyStamp
method to add watermarks to the PDF. - Export the PDF file by calling
SaveAs
.
Example C# Code to Apply a Watermark Using IronPDF
Ensure you have installed the IronPDF library in your project. You can find more detailed instructions on the IronPDF NuGet package page.
using IronPdf;
public class PdfWatermarkExample
{
public static void Main()
{
// Create a new PDF document or load an existing one
var pdfDocument = PdfDocument.FromFile("example.pdf");
// Define HTML content for the watermark
string htmlWatermark = "<div style='font-size:72pt; color:red; opacity:0.3;'>Confidential</div>";
// Apply the watermark to the PDF
// Parameters: HTML, Rotation (degrees), X Position, Y Position, Opacity, Page(s)
pdfDocument.ApplyStamp(htmlWatermark,
rotationDegrees: 45,
left: 50,
top: 50,
opacity: 0.5,
pageRange: PageRange.AllPages);
// Save the modified PDF document
pdfDocument.SaveAs("watermarked_example.pdf");
}
}
using IronPdf;
public class PdfWatermarkExample
{
public static void Main()
{
// Create a new PDF document or load an existing one
var pdfDocument = PdfDocument.FromFile("example.pdf");
// Define HTML content for the watermark
string htmlWatermark = "<div style='font-size:72pt; color:red; opacity:0.3;'>Confidential</div>";
// Apply the watermark to the PDF
// Parameters: HTML, Rotation (degrees), X Position, Y Position, Opacity, Page(s)
pdfDocument.ApplyStamp(htmlWatermark,
rotationDegrees: 45,
left: 50,
top: 50,
opacity: 0.5,
pageRange: PageRange.AllPages);
// Save the modified PDF document
pdfDocument.SaveAs("watermarked_example.pdf");
}
}
CONVERTER NOT RUNNING
Explanation of the Code:
- We start by importing the
IronPdf
library, which provides all necessary classes and methods for PDF manipulation. - A PDF document is created or loaded using
PdfDocument.FromFile
, specifying the file path of the existing PDF. - HTML content is defined for the watermark. In this case, the watermark displays "Confidential" with specific styling.
- The
ApplyStamp
method is used to overlay the watermark on the PDF. This method allows for detailed customization:rotationDegrees
: Specifies the rotation, in degrees, of the watermark.left
andtop
: Dictate the X and Y position of the watermark, measured from the top-left corner.opacity
: Determines the transparency of the watermark.pageRange
: Specifies which pages should receive the watermark, allowing for diverse placement strategies.
- Finally, the
SaveAs
method exports the modified PDF to a new file.
In conclusion, the IronPDF ApplyStamp
method allows for precise control over watermarking PDF documents using HTML. This approach is flexible, accommodating various customization needs for positioning, styling, and applying watermarks to specified pages.