Adding Watermarks to PDFs with C#: A Comprehensive Guide
In this article, we will explore how to add watermarks to PDF files using C#. We will discuss key concepts, provide detailed context, and include code samples to help you implement this functionality in your applications. We will cover two scenarios: adding a watermark as a header and adding a watermark throughout the pages of a PDF document.
What is a Watermark?
A watermark is a graphical image or text that is superimposed on a document to identify its ownership, confidentiality, or origin. Watermarks can be transparent or semi-transparent, allowing the underlying content to be visible. Watermarks are commonly used in PDF documents to indicate copyright, confidentiality, or draft status.
Adding a Watermark as a Header
To add a watermark as a header, we will use the iText 7 library, which is a powerful PDF manipulation library that supports various features, including watermarking. First, you need to install the iText 7 package via NuGet.
Install-Package itext7
Once you have installed the package, you can use the following code to add a watermark as a header:
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
// Create a new PDF document
using (PdfDocument document = new PdfDocument(new PdfWriter("output.pdf")))
{
// Create a new PDF document
document.AddNewPage();
// Create a new watermark
Watermark watermark = new Watermark("Watermark Text", new FontSet());
// Add the watermark as a header
document.AddHeaderWatermark(watermark);
// Add a paragraph to the document
document.Add(new Paragraph("Document Content"));
}
In this example, we create a new PDF document, add a new page, and then create a new watermark with the text "Watermark Text". We then add the watermark as a header using the AddHeaderWatermark() method. Finally, we add some content to the document using the Add() method.
Adding a Watermark Throughout the Pages of a PDF Document
To add a watermark throughout the pages of a PDF document, we will modify the previous example by using a PageEvent to add the watermark to each page. Here is the modified code:
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Kernel.Pdf.Event;
using iText.Kernel.Geom;
public class WatermarkEvent : PdfPageEventHelper
{
private Watermark _watermark;
public WatermarkEvent(Watermark watermark)
{
_watermark = watermark;
}
public override void OnEndPage(PdfDocument document, PdfPage page)
{
base.OnEndPage(document, page);
// Get the canvas for the current page
PdfCanvas canvas = new PdfCanvas(page.NewContentStreamBefore(), page.GetResources(), new PdfDocumentPac(document));
// Calculate the position and size of the watermark
Rectangle pageSize = page.GetPageSize();
float x = (pageSize.GetRight() - pageSize.GetLeft()) / 2;
float y = (pageSize.GetTop() - pageSize.GetBottom()) / 2;
float width = 200;
float height = 50;
// Create a new watermark
Watermark watermark = new Watermark(_watermark.GetText(), new FontSet());
watermark.SetFixedSize(width, height);
watermark.SetHorizontalAlignment(_watermark.GetHorizontalAlignment());
watermark.SetVerticalAlignment(_watermark.GetVerticalAlignment());
// Add the watermark to the canvas
watermark.Draw(canvas, x, y);
}
}
// Create a new PDF document
using (PdfDocument document = new PdfDocument(new PdfWriter("output.pdf")))
{
// Create a new watermark
Watermark watermark = new Watermark("Watermark Text", new FontSet());
// Add a page event to add the watermark to each page
document.AddEventHandler(PdfDocumentEvent.END_PAGE, new WatermarkEvent(watermark));
// Add new pages to the document
for (int i = 0; i < 5; i++)
{
document.AddNewPage();
// Add a paragraph to the document
document.Add(new Paragraph("Page " + (i + 1)));
}
}
In this example, we define a new class called WatermarkEvent, which inherits from PdfPageEventHelper. This class overrides the OnEndPage() method, which is called at the end of each page. We then create a new watermark using the Watermark() constructor, just like in the previous example. However, instead of adding the watermark as a header, we add a page event using the AddEventHandler() method. We then pass an instance of WatermarkEvent that includes the watermark we want to add. Finally, we add five pages to the document, just like in the previous example.
- A watermark is a graphical image or text used to identify the ownership, confidentiality, or origin of a document.
- To add a watermark as a header, you can use the
AddHeaderWatermark() method of the PdfDocument class.
- To add a watermark throughout the pages of a PDF document, you can use a
PageEvent that overrides the OnEndPage() method.
- The iText 7 library is a powerful PDF manipulation library that supports various features, including watermarking.
References
- iText 7: https://itextpdf.com/itext7
- iText 7 Documentation: https://developers.itextpdf.com/content/itext-7-developer-guide
- GitHub: https://github.com/itext/itext7