Add Water mark image to PDF using iTextsharp, C# and VB.Net in ASP.Net

Last Reply on Sep 21, 2017 06:26 AM By AnandM

Posted on Sep 21, 2017 04:56 AM

Hi All , 

 

I Have Create one Merge Pdf File, within that file i would like to add stamp to all pages, i have tried lots, but nver got the solution, 

 

please help me.

 

Please check the Below Code for your reference.

 

Regards, 

Sandip Patil

 private void PdfStamp(string FileLocation, string sourceFilePath)
        {
            string watermarkImagePath = @"D:/Projects/MaterialDetails/MaterialDetails/img/Stamp.png";

            var pdfReader = new PdfReader(sourceFilePath);
            var pdfStamper = new PdfStamper(pdfReader, new FileStream(FileLocation, FileMode.Create));
            var image = iTextSharp.text.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();
        }

 

Posted on Sep 21, 2017 06:26 AM

Hi sandip.AP,

Refer below sample code.

C#

private void PdfStampWithNewFile(string watermarkImagePath, string sourceFilePath, string destinationFilePath)
{
    var pdfReader = new PdfReader(sourceFilePath);
    var pdfStamper = new PdfStamper(pdfReader, new FileStream(destinationFilePath, FileMode.Create));
    var image = iTextSharp.text.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();
}

private void PdfStampInExistingFile(string watermarkImagePath, string sourceFilePath)
{
    byte[] bytes = File.ReadAllBytes(sourceFilePath);
    var img = iTextSharp.text.Image.GetInstance(watermarkImagePath);
    img.SetAbsolutePosition(200, 400);
    PdfContentByte waterMark;

    using (MemoryStream stream = new MemoryStream())
    {
        PdfReader reader = new PdfReader(bytes);
        using (PdfStamper stamper = new PdfStamper(reader, stream))
        {
            int pages = reader.NumberOfPages;
            for (int i = 1; i <= pages; i++)
            {
                waterMark = stamper.GetUnderContent(i);
                waterMark.AddImage(img);
            }
        }
        bytes = stream.ToArray();
    }
    File.WriteAllBytes(sourceFilePath, bytes);
}

VB.Net

Private Sub PdfStampWithNewFile(watermarkImagePath As String, sourceFilePath As String, destinationFilePath As String)
    Dim pdfReader = New iTextSharp.text.pdf.PdfReader(sourceFilePath)
    Dim pdfStamper = New iTextSharp.text.pdf.PdfStamper(pdfReader, New FileStream(destinationFilePath, FileMode.Create))
    Dim image = iTextSharp.text.Image.GetInstance(watermarkImagePath)
    image.SetAbsolutePosition(200, 400)
    For i As Integer = 0 To pdfReader.NumberOfPages - 1
        Dim content = pdfStamper.GetUnderContent(i + 1)
        content.AddImage(image)
    Next
    pdfStamper.Close()
End Sub

Private Sub PdfStampInExistingFile(watermarkImagePath As String, sourceFilePath As String)
    Dim bytes As Byte() = File.ReadAllBytes(sourceFilePath)
    Dim img = iTextSharp.text.Image.GetInstance(watermarkImagePath)
    img.SetAbsolutePosition(200, 400)
    Dim waterMark As PdfContentByte

    Using stream As New MemoryStream()
        Dim reader As New PdfReader(bytes)
        Using stamper As New PdfStamper(reader, stream)
            Dim pages As Integer = reader.NumberOfPages
            For i As Integer = 1 To pages
                waterMark = stamper.GetUnderContent(i)
                waterMark.AddImage(img)
            Next
        End Using
        bytes = stream.ToArray()
    End Using
    File.WriteAllBytes(sourceFilePath, bytes)
End Sub