Add Watermark to PDFs using iTextSharp

This tutorial focuses on how you can use iTextSharp in c# projects to add watermark to your PDFs.Those of you who are not aware of iTextSharp, it is basically a .net PDF library which is used to create PDFs programmatically from .net Applications. You can also define some properties for pdf pages like headers/footers or watermarks etc through iTextSharp.

I have used two methods for the purpose of adding Watermark text for PDF Pages. Lets go through them one by one.

/// <summary>
/// This method adds watermark text under pdf content
/// </summary>
/// <param name="pdfData">pdf content bytes</param>
/// <param name="watermarkText">text to be shown as watermark</param>
/// <param name="font">base font</param>
/// <param name="fontSize">font sieze</param>
/// <param name="angle">angle at which watermark needs to be shown</param>
/// <param name="color">water mark color</param>
/// <param name="realPageSize">pdf page size</param>
public static void AddWaterMarkText(PdfContentByte pdfData, string watermarkText, BaseFont font, float fontSize, float angle, BaseColor color, Rectangle realPageSize)
{
var gstate = new PdfGState {FillOpacity = 0.35f, StrokeOpacity = 0.3f};
pdfData.SaveState();
pdfData.SetGState(gstate);
pdfData.SetColorFill(color);
pdfData.BeginText();
pdfData.SetFontAndSize(font, fontSize);
var x = (realPageSize.Right + realPageSize.Left)/2;
var y = (realPageSize.Bottom + realPageSize.Top)/2;
pdfData.ShowTextAligned(Element.ALIGN_CENTER, watermarkText, x, y, angle);
pdfData.EndText();
pdfData.RestoreState();
}

This method basically takes PDF data in the form of PDFContentByte, text which you need to add as watermark, page size , font properties & angle at which you want to add watermark.

To add to all pages  of a PDF document I have called above method inside below shown method. This method takes pdf content in form of byte array, Font & text which needs to be added as watermark.

// <summary>
/// This method calls another method to add watermark text for each page
/// </summary>
/// <param name="bytes">byte array of Pdf</param>
/// <param name="baseFont">Base font</param>
/// <param name="watermarkText">Text to be added as watermark</param>
/// <returns>Pdf bytes array having watermark added</returns>
private static byte[] AddWatermark(byte[] bytes, BaseFont baseFont, string watermarkText)
{
using (var ms = new MemoryStream(10 * 1024))
{
using (var reader = new PdfReader(bytes))
using (var stamper = new PdfStamper(reader, ms))
{
var pages = reader.NumberOfPages;
for (var i = 1; i <= pages; i++)
{
var dc = stamper.GetOverContent(i);
AddWaterMarkText(dc, watermarkText, baseFont, 50, 45, BaseColor.GRAY, reader.GetPageSizeWithRotation(i));
}
stamper.Close();
}
return ms.ToArray();
}
}

This method iterated through all PDF pages and call AddWatermarkText() on each page so that all gets the watermark. In case you want to omit some pages from getting watermarked you can put suitable conditions in loop for that or if you want to change watermark text for some pages then also you can change the method parameters as per need.

you need to need following name spaces in order to use iTextSharp:

using iTextSharp.text;
using iTextSharp.text.pdf;

Voila… now you can add watermark simply using these two methods.

Advertisements

3 thoughts on “Add Watermark to PDFs using iTextSharp

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s