In this article I will explain with an example, how to add Watermark to existing PDF using iTextSharp in ASP.Net with C# and VB.Net.
 
 

Download iTextSharp and XmlWorkerHelper Libraries

You can download the iTextSharp and XmlWorkerHelper libraries from the following links.
Note: You will need to add the reference of iTextSharp and XmlWorkerHelper libraries in your project.
 
 

PDF File Location

The sample PDF file and Watermark image are located inside the PDF and Images Folder (Directory) of ASP.Net project.
Add Watermark to existing PDF using iTextSharp in ASP.Net
 
 

HTML Markup

The HTML Markup consists of following control:
Button – For exporting PDF.
The Button has been assigned with an OnClick event handler.
<asp:Button runat="server" Text="Export" OnClick="OnExport" /> 
 
 

Namespaces

You will need to import the following namespaces.
C#
using System.IO;
using iTextSharp.text.pdf;
 
VB.Net
Imports System.IO
Imports iTextSharp.text.pdf
 
 

Adding Watermark to existing PDF using C# and VB.Net

When the Export Button is clicked, first the BYTE Array of the sample PDF file (explained earlier) is determined.
Then, the Watermark image is read using GetInstance method of Image class and its position is set using SetAbsolutePosition method of Image class.
After that, an object of PdfReader class is created which accepts BYTE Array of the sample PDF file as a parameter.
A PdfStamper class object is also created which accepts objects of PdfReader and MemoryStream class as a parameter.
Then, PDF document is created using existing PDF file and a FOR loop is executed over all the pages.

Adding Watermark

Inside the loop, the Watermark image is added to the PDF document using AddImage method of PdfContentByte class.
Then, the Response class properties are set.
1. Content-Disposition – It is a response header indicating, the download file is an attachment and allows setting the file name.
Note: For more details on Content-Disposition header, please refer What is Content Disposition Header in ASP.Net.
 
2. ContentType – It informs the Browser about the file type. In this case it is PDF.
Finally, the PDF document is written to the Response which initiates the download operation.
C#
protected void OnExport(object sender, EventArgs e)
{
    byte[] bytes = File.ReadAllBytes(Server.MapPath("~/PDF/Sample.pdf"));
 
    // Reading watermark image.
    iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(Server.MapPath("~/Images/Watermark.png"));
 
    // Setting watermark image position.
    img.SetAbsolutePosition(100, 500);
 
    using (MemoryStream stream = new MemoryStream())
    {
        // Reading pdf.
        PdfReader reader = new PdfReader(bytes);
        using (PdfStamper stamper = new PdfStamper(reader, stream))
        {
            // Loop through all pages.
            for (int i = 1; i <= reader.NumberOfPages; i++)
            {
                PdfContentByte pdfContentByte stamper.GetUnderContent(i);
 
                // Adding Image to the pdf.
                pdfContentByte.AddImage(img);
            }
        }
 
        bytes = stream.ToArray();
    }
 
    Response.Clear();
    Response.Buffer = true;
    Response.Charset "";
    Response.AddHeader("content-disposition", "attachment;filename=Sample_With_Watermark.pdf");
    Response.ContentType "application/pdf";
    Response.BinaryWrite(bytes);
    Response.Flush();
    Response.End();
}
 
VB.Net
Protected Sub OnExport(sender As Object, e As EventArgs)
    Dim bytes As Byte() = File.ReadAllBytes(Server.MapPath("~/PDF/Sample.pdf"))
 
    ' Reading watermark image.
    Dim img As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(Server.MapPath("~/Images/Watermark.png"))
 
    ' Setting watermark image position.
    img.SetAbsolutePosition(100, 500)
 
    Using stream As MemoryStream = New MemoryStream()
 
        ' Reading pdf.
        Dim reader As PdfReader = New PdfReader(bytes)
        Using stamper As PdfStamper = New PdfStamper(reader, stream)
 
            ' Loop through all pages.
            For i As Integer = 1 To reader.NumberOfPages
                Dim pdfContentByte As PdfContentByte stamper.GetUnderContent(i)
 
                ' Adding Image to the pdf.
                pdfContentByte.AddImage(img)
            Next
        End Using
 
        bytes = stream.ToArray()
    End Using
 
    Response.Clear()
    Response.Buffer = True
    Response.Charset ""
    Response.AddHeader("content-disposition", "attachment;filename=Sample_With_Watermark.pdf")
    Response.ContentType "application/pdf"
    Response.BinaryWrite(bytes)
    Response.Flush()
    Response.End()
End Sub
 
 

Screenshots

PDF before Exporting

Add Watermark to existing PDF using iTextSharp in ASP.Net
 

Form

Add Watermark to existing PDF using iTextSharp in ASP.Net
 

PDF after Exporting

Add Watermark to existing PDF using iTextSharp in ASP.Net
 
 

Downloads