iTextSharp–Add watermark to PDF

In the last post iTextSharp–Add header/footer to PDF we saw how to create a pdf with a header/footer. To add a watermark to pdf, you need to create a class that implements IPdfPageEvent as shown below code:

public class PdfWriterEvents : IPdfPageEvent
   {
       string watermarkText = string.Empty;
 
       public PdfWriterEvents(string watermark)
       {
           watermarkText = watermark;
       }
       public void OnStartPage(PdfWriter writer, Document document)
       {
           float fontSize = 80;
           float xPosition = iTextSharp.text.PageSize.A4.Width / 2;
           float yPosition = (iTextSharp.text.PageSize.A4.Height - 140f) / 2;
           float angle = 45;
           try
           {
               PdfContentByte under = writer.DirectContentUnder;
               BaseFont baseFont = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.EMBEDDED);
               under.BeginText();
               under.SetColorFill(BaseColor.LIGHT_GRAY);
               under.SetFontAndSize(baseFont, fontSize);
               under.ShowTextAligned(PdfContentByte.ALIGN_CENTER, watermarkText, xPosition, yPosition, angle);
               under.EndText();
           }
           catch (Exception ex)
           {
               Console.Error.WriteLine(ex.Message);
           }
       }
       public void OnEndPage(PdfWriter writer, Document document) { }
       public void OnParagraph(PdfWriter writer, Document document, float paragraphPosition) { }
       public void OnParagraphEnd(PdfWriter writer, Document document, float paragraphPosition) { }
       public void OnChapter(PdfWriter writer, Document document, float paragraphPosition, Paragraph title) { }
       public void OnChapterEnd(PdfWriter writer, Document document, float paragraphPosition) { }
       public void OnSection(PdfWriter writer, Document document, float paragraphPosition, int depth, Paragraph title) { }
       public void OnSectionEnd(PdfWriter writer, Document document, float paragraphPosition) { }
       public void OnGenericTag(PdfWriter writer, Document document, Rectangle rect, String text) { }
       public void OnOpenDocument(PdfWriter writer, Document document) { }
       public void OnCloseDocument(PdfWriter writer, Document document) { }
   }

Now let’s see how to add a watermark using the above class:

PdfWriter pdfWriter = PdfWriter.GetInstance(pdfDoc, msReport); 
pdfWriter.PageEvent = new PdfWriterEvents("Testing");

After using the code above, it adds a watermark, as shown below:

iTextSharp Watermark example

I hope it helps!


Posted

in

,

by

Comments

2 responses to “iTextSharp–Add watermark to PDF”

  1. Unknown Avatar

    Hello Sir I wana to add watermark in IOS can you help me.

  2. jw noze Avatar

    Add watermark to PDF using another tool, iDiTect.PDF, supports watermark text and images to pdf, in page or header/footer.

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: