How to: Add Watermark to PDFs Programmatically using iTextSharp

Being able to add text or image watermark to PDF files is one of those handy things you can archive via program.

The only library required for the little program is iTextSharp. For those who haven't used it yet, iTextSharp is a C# version of iText, which allows you to generate or manipulate PDFs.

So let's cut the chitchat, and go straight to the code. By calling 'AddTextWatermark' or 'AddImageWatermark' function with required parameters supplied, you are ready to rock!


public void CreateTemplate(string watermarkText, string targetFileName){
    var document = new Document();
    var pdfWriter = PdfWriter.GetInstance(document, new FileStream(targetFileName, FileMode.Create));
    var font = new Font(Font.FontFamily.HELVETICA, 60, Font.NORMAL, BaseColor.LIGHT_GRAY);
    document.Open();
    // Specify font and alignment of text watermark
    ColumnText.ShowTextAligned(pdfWriter.DirectContent, Element.ALIGN_CENTER, new Phrase(watermarkText, font), 300, 400, 45);
    document.Close();
}

// Add watermark using a PDF file
public void AddTextWatermark(string sourceFilePath, string watermarkTemplatePath, string targetFilePath){
    var pdfReaderSource = new PdfReader(sourceFilePath);
    var pdfStamper = new PdfStamper(pdfReaderSource, new FileStream(targetFilePath, FileMode.Create));
    var pdfReaderTemplate = new PdfReader(watermarkTemplatePath);
    var page = pdfStamper.GetImportedPage(pdfReaderTemplate, 1);

    for (var i = 0; i < pdfReaderSource.NumberOfPages; i++)
    {
        var content = pdfStamper.GetUnderContent(i + 1);
        content.AddTemplate(page, 0, 0);
    }

    pdfStamper.Close();
    pdfReaderTemplate.Close();
}

// Add watermark to a file using an image
public void AddImageWatermark(string sourceFilePath, string watermarkImagePath, string targetFilePath){
    var pdfReader = new PdfReader(sourceFilePath);
    var pdfStamper = new PdfStamper(pdfReader, new FileStream(targetFilePath, FileMode.Create));
    var image = Image.GetInstance(watermarkImagePath);
    image.SetAbsolutePosition(200, 400);

    for (var i = 0; i < pdfReader.NumberOfPages; i++)
    {
        var content = pdfStamper.GetUnderContent(i + 1);
        content.AddImage(image);
    }

    pdfStamper.Close();
}

Comments

  1. How to use the methods - meaning, are they being overridden or we need to define a sep class.

    ReplyDelete
    Replies
    1. Hi, there are three methods each does slightly different things. You should be able to use them directly by putting in an existing class or a new one, given you've got the correct imports.

      Delete
  2. hi,
    watermark is displaying in only last page of the pdf i want it in all pages

    ReplyDelete
  3. Hello,

    I have used your solution and it is working as I expected.

    Thanks Bro..

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. itextsharp do nice work, however my project don't allow to use the open source library, so I find another cheap third part library to add watermark to pdf in c# https://www.iditect.com/tutorial/watermark-pdf/

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete

Post a Comment

Popular posts from this blog

LangChain Tutorial: Chain Types

Building LLM Applications with LangChain and Streamlit