Add WaterMark To PDF using VB.NET

    General discussion


    1. Add Reference of iTextSharp.dll
    2. btnAddWaterMark_Click is a button name.
    3. AddWatermarkImage is a method name.
    4. Enter Input File's Path in input
    5. Enter Output File's Path in output
    6. Enter Image's Path in water
    7. Good Luck.

    Private Sub btnAddWaterMark_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddWaterMark.Click

    Dim input As String

    Dim output As String

    Dim water As String

    If txtPath.Text = "" Or txtimgpath.Text = "" Or txtoutpath.Text = "" Then

    MessageBox.Show("Please Enter Required Data", "Required Data Missing")


    input = txtPath.Text

    water = txtimgpath.Text

    output = txtoutpath.Text

    AddWatermarkImage(input, output, water)

    End If

    End Sub

    Public Shared Sub AddWatermarkImage(ByVal sourceFile As String, ByVal outputFile As String, ByVal watermarkImage As String)

    Dim reader As iTextSharp.text.pdf.PdfReader = Nothing

    Dim stamper As iTextSharp.text.pdf.PdfStamper = Nothing

    Dim img As iTextSharp.text.Image = Nothing

    Dim underContent As iTextSharp.text.pdf.PdfContentByte = Nothing

    Dim rect As iTextSharp.text.Rectangle = Nothing

    Dim X, Y As Single

    Dim pageCount As Integer = 0


    If System.IO.File.Exists(sourceFile) Then

    reader = New iTextSharp.text.pdf.PdfReader(sourceFile)

    rect = reader.GetPageSizeWithRotation(1)

    stamper = New iTextSharp.text.pdf.PdfStamper(reader, New System.IO.FileStream(outputFile, IO.FileMode.Create))

    img = iTextSharp.text.Image.GetInstance(watermarkImage)

    If img.Width > rect.Width OrElse img.Height > rect.Height Then

    img.ScaleToFit(rect.Width, rect.Height)

    X = (rect.Width - img.ScaledWidth) / 2

    Y = (rect.Height - img.ScaledHeight) / 2


    X = (rect.Width - img.Width) / 2

    Y = (rect.Height - img.Height) / 2

    End If

    img.SetAbsolutePosition(X, Y)

    pageCount = reader.NumberOfPages()

    For i As Integer = 1 To pageCount

    underContent = stamper.GetUnderContent(i)






    MessageBox.Show("File Does Not Exist", "Missing File")

    End If

    Catch ex As Exception

    Throw ex

    End Try

    End Sub

    Friday, November 14, 2008 4:20 AM

All replies

  • hi;

    could you please help to change code to do watermaking in commandline so then it can used for batch operation similar to commercial softwares such as ParmisPDF software?

    thanks a lot

    Wednesday, May 11, 2011 5:34 PM
  • Really thanks. I was long time ago trying to add my watermark in every pdf file, but VB is great. In every werbeartikel offer I have to send never have align the watermark but with this sequency I think it make it!!
    Tuesday, May 17, 2011 8:36 AM
  • How about a redo using Asp.Net?

    Edward R. Joell MCSD MCDBA

    Monday, August 20, 2012 4:48 PM
  •  iTextSharp.dll this dll is free or need to purchase?

    Every second counts..make use of it. Disclaimer: This posting is provided AS IS with no warranties or guarantees and confers no rights.

    Thursday, February 18, 2016 10:13 AM