locked
Add WaterMark In PDF Files RRS feed

  • Question

  • User271624304 posted

     

    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. Help ??
    8. 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 = "" ThenMessageBox.Show("Please Enter Required Data", "Required Data Missing")

    Else

    input = txtPath.Text

    water = txtimgpath.Text

    output = txtoutpath.Text

    AddWatermarkImage(input, output, water)

    End If

    End SubPublic 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 SingleDim pageCount As Integer = 0

    Try

    If System.IO.File.Exists(sourceFile) Thenreader = 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

    Else

    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)

    underContent.AddImage(img)

    Next

    stamper.Close()

    reader.Close()

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

    End IfCatch ex As Exception

    Throwex

    End Try

    End Sub

    Friday, November 14, 2008 12:12 AM

All replies

  • User-2083332017 posted
    http://www.c-sharpcorner.com/UploadFile/scottlysle/WatermarkCS05072007024947AM/WatermarkCS.aspx
    Sunday, November 16, 2008 1:52 PM
  • User1081535768 posted

     Hello.

    thanks for your code...
    anyway, when i try to use it, i get this error:

    +        ex    {"The process cannot access the file 'C:\WwwRoot\Osiris.4\Portals\0\FileLezioni\105235373_Lesson_233.pdf' because it is being used by another process."}    System.Exception

    i've just created the pdf in this way...

                    Dim document As iTextSharp.text.Document = New iTextSharp.text.Document(rect)
                    Dim writer As iTextSharp.text.pdf.PdfWriter              
                    writer = iTextSharp.text.pdf.PdfWriter.GetInstance(document, New FileStream(path & nameFile, FileMode.Create))
                    ....
                    do my job ... then i do....


                    document.Close()
                    writer.Close()                

                    Dim watermarkImage As String = watermarkimagepath & watermarkimageName              
                    AddWatermarkImage(path & nomeFile, path & nomeFile, watermarkImage)   'callyour procedure

     

    is there something i forget... if the document and writer are closed... what is missing??

    thanks

    Rudy Vicelli

     

     

    Tuesday, December 9, 2008 4:57 AM
  • User1081535768 posted

    hii, i undestood that source and destination cannot be the same...

    i just wanted to ask how to put the watermark at page-bottom

    thanks

     

    Tuesday, December 9, 2008 5:17 AM
  • User271624304 posted

    It's an error for not closing the connection properly, check again to properly close() the connection.

    Wednesday, June 10, 2009 11:39 PM
  • User65472418 posted

    Very nice thanks!

    Thursday, June 11, 2009 12:58 PM
  • User271624304 posted

    Welcome....!

    Monday, June 29, 2009 1:07 AM
  • User-107455022 posted

    Thanks naimish  :) 

    Thursday, December 23, 2010 2:18 PM