Add WaterMark In PDF Files

Rate It (1)

Last post 06-29-2009 1:07 AM by naimish. 6 replies.

Sort Posts:

  • Add WaterMark In PDF Files

    11-14-2008, 12:12 AM
    • Member
      39 point Member
    • naimish
    • Member since 11-13-2006, 6:47 AM
    • Mumbai
    • Posts 25

     

    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")

    Else

    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

    Try

    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

    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()

    Else

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

    End If

    Catch ex As Exception

    Throw ex

    End Try

    End Sub

    Thanks & Regards,
    Naimish A. Pandya
    [MCTS : Web Applications]
    [MCPD : Web Developer]
    Filed under:
  • Re: Add WaterMark In PDF Files

    11-16-2008, 1:52 PM
    • Participant
      942 point Participant
    • Sagar Nishant
    • Member since 11-13-2008, 10:20 AM
    • Ahmedabad
    • Posts 152
    http://www.c-sharpcorner.com/UploadFile/scottlysle/WatermarkCS05072007024947AM/WatermarkCS.aspx
    Thanks,

    Nishant Sagar
  • Re: Add WaterMark In PDF Files... in use by another process

    12-09-2008, 4:57 AM
    • Member
      29 point Member
    • WeiLuDi
    • Member since 01-17-2006, 10:19 AM
    • Posts 8

     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

     

     

  • Re: ok, but at page bottom

    12-09-2008, 5:17 AM
    • Member
      29 point Member
    • WeiLuDi
    • Member since 01-17-2006, 10:19 AM
    • Posts 8

    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

     

  • Re: Add WaterMark In PDF Files... in use by another process

    06-10-2009, 11:39 PM
    • Member
      39 point Member
    • naimish
    • Member since 11-13-2006, 6:47 AM
    • Mumbai
    • Posts 25

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

    Thanks & Regards,
    Naimish A. Pandya
    [MCTS : Web Applications]
    [MCPD : Web Developer]
  • Re: Add WaterMark In PDF Files... in use by another process

    06-11-2009, 12:58 PM
    • Member
      4 point Member
    • micaela
    • Member since 06-11-2009, 4:32 PM
    • Posts 2

    Very nice thanks!

  • Re: Add WaterMark In PDF Files... in use by another process

    06-29-2009, 1:07 AM
    • Member
      39 point Member
    • naimish
    • Member since 11-13-2006, 6:47 AM
    • Mumbai
    • Posts 25

    Welcome....!

    Thanks & Regards,
    Naimish A. Pandya
    [MCTS : Web Applications]
    [MCPD : Web Developer]
Page 1 of 1 (7 items)