Go4Expert (http://www.go4expert.com/)
-   ASP (http://www.go4expert.com/articles/asp-tutorials/)
-   -   Add WaterMark In PDF Files (http://www.go4expert.com/articles/add-watermark-pdf-files-t18258/)

arithforu 30Jun2009 07:10

Add WaterMark In PDF Files
  • Add Reference of iTextSharp.dll
  • btnAddWaterMark_Click is a button name.
  • AddWatermarkImage is a method name.
  • Enter Input File's Path in input
  • Enter Output File's Path in output
  • Enter Image's Path in water
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

naimish 30Jun2009 08:40

Re: Add WaterMark In PDF Files
Nice one buddy, I have used similar one in my project.

shabbir 30Jun2009 09:44

Re: Add WaterMark In PDF Files
I don't understand the logic but Naimish and arithforu looks same same users only seeing this.

nimesh 30Jun2009 11:12

Re: Add WaterMark In PDF Files
It's also available on MSDN - here
And on vbforums - here

nimesh 30Jun2009 11:29

Re: Add WaterMark In PDF Files

Originally Posted by shabbir (Post 50771)
I don't understand the logic but Naimish and arithforu looks same same users only seeing this.

And also from posts 4,6 on this thread:

naimish 30Jun2009 14:48

Re: Add WaterMark In PDF Files
Yes, Naimish and arithforu are the same users as me, I had problem while registering on the site, so I created naimish, after which arithforu worked fine, so posted it, on next naimish's account started working fine, so decided not to use arithforu and to use naimish as my proflie.

About http://forums.asp.net/p/1348029/2744846.aspx

Please see that that topic has been posted by me only, my username there is naimish only.

This code has been developed by me only in my project, if someone wants clarification can contact me for ruther details.


shabbir 30Jun2009 15:01

Re: Add WaterMark In PDF Files
Moved to Articles section and also try using only one account as use of 2 accounts simultaneously is not allowed.

naimish 30Jun2009 15:29

Re: Add WaterMark In PDF Files
Yes, as I said, I have decided to use naimish instead of arithforu, you can delete arithforu if you want. Thanks.

nimesh 30Jun2009 20:53

Re: Add WaterMark In PDF Files
Yes, I saw your name on ASP.NET site but didn't notice on MSDN Forum :)

btw, I didn't knew about this iTestsharp.
Now I can use it in my application. haha


naimish 1Jul2009 09:47

Re: Add WaterMark In PDF Files
Welcome Buddy, nice to see your positive reply. Thanks a lot. Please also check out my new article, which I am going to write now on, Add Password In PDF :)

All times are GMT +5.5. The time now is 22:34.