1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Add WaterMark In PDF Files

Discussion in 'ASP' started by arithforu, Jun 30, 2009.

  1. arithforu

    arithforu New Member

    Joined:
    Jun 29, 2009
    Messages:
    2
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    SE
    Location:
    On Earth
    • 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.
    Code:
    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
    
     
    nimesh likes this.
  2. naimish

    naimish New Member

    Joined:
    Jun 29, 2009
    Messages:
    1,046
    Likes Received:
    18
    Trophy Points:
    0
    Occupation:
    Software Engineer
    Location:
    On Earth
    Nice one buddy, I have used similar one in my project.
     
  3. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,287
    Likes Received:
    364
    Trophy Points:
    83
    I don't understand the logic but Naimish and arithforu looks same same users only seeing this.
     
  4. nimesh

    nimesh New Member

    Joined:
    Apr 13, 2009
    Messages:
    770
    Likes Received:
    20
    Trophy Points:
    0
    Occupation:
    Oracle Apps Admin
    Location:
    Mumbai
    Home Page:
    It's also available on MSDN - here
    And on vbforums - here
     
  5. nimesh

    nimesh New Member

    Joined:
    Apr 13, 2009
    Messages:
    770
    Likes Received:
    20
    Trophy Points:
    0
    Occupation:
    Oracle Apps Admin
    Location:
    Mumbai
    Home Page:
  6. naimish

    naimish New Member

    Joined:
    Jun 29, 2009
    Messages:
    1,046
    Likes Received:
    18
    Trophy Points:
    0
    Occupation:
    Software Engineer
    Location:
    On Earth
    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.

    Thanks.
     
  7. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,287
    Likes Received:
    364
    Trophy Points:
    83
    Moved to Articles section and also try using only one account as use of 2 accounts simultaneously is not allowed.
     
  8. naimish

    naimish New Member

    Joined:
    Jun 29, 2009
    Messages:
    1,046
    Likes Received:
    18
    Trophy Points:
    0
    Occupation:
    Software Engineer
    Location:
    On Earth
    Yes, as I said, I have decided to use naimish instead of arithforu, you can delete arithforu if you want. Thanks.
     
  9. nimesh

    nimesh New Member

    Joined:
    Apr 13, 2009
    Messages:
    770
    Likes Received:
    20
    Trophy Points:
    0
    Occupation:
    Oracle Apps Admin
    Location:
    Mumbai
    Home Page:
    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

    Thanks:2thumbsup
     
  10. naimish

    naimish New Member

    Joined:
    Jun 29, 2009
    Messages:
    1,046
    Likes Received:
    18
    Trophy Points:
    0
    Occupation:
    Software Engineer
    Location:
    On Earth
    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 :)
     
  11. naimish

    naimish New Member

    Joined:
    Jun 29, 2009
    Messages:
    1,046
    Likes Received:
    18
    Trophy Points:
    0
    Occupation:
    Software Engineer
    Location:
    On Earth
    Posting Done, Waiting for approval, please have a look once it got approved :)
     
  12. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,287
    Likes Received:
    364
    Trophy Points:
    83
    Its already approved quite sometime back
     
  13. naimish

    naimish New Member

    Joined:
    Jun 29, 2009
    Messages:
    1,046
    Likes Received:
    18
    Trophy Points:
    0
    Occupation:
    Software Engineer
    Location:
    On Earth
    Yeah, Seen It, Thank you very much for approving it :)
     
  14. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,287
    Likes Received:
    364
    Trophy Points:
    83
  15. naimish

    naimish New Member

    Joined:
    Jun 29, 2009
    Messages:
    1,046
    Likes Received:
    18
    Trophy Points:
    0
    Occupation:
    Software Engineer
    Location:
    On Earth
    Ohh, Thanks Shabbir :)
     
  16. naimish

    naimish New Member

    Joined:
    Jun 29, 2009
    Messages:
    1,046
    Likes Received:
    18
    Trophy Points:
    0
    Occupation:
    Software Engineer
    Location:
    On Earth
    Please check out my new article on Add Text WaterMark In PDF Files :D
     
  17. abhi.aradhye

    abhi.aradhye New Member

    Joined:
    Nov 23, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    i have ude this code in my page .
    page run but watermark is not added in pdf.
    please help m.
    my code

    PdfReader pdfReader = new PdfReader(Server.MapPath("321.pdf"));
    PdfStamper stamp = new PdfStamper(pdfReader, new System.IO.FileStream(Server.MapPath("watermark.pdf"), System.IO.FileMode.Create));

    iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(Server.MapPath("AIPL-watermark.jpg"));
    img.SetAbsolutePosition(100, 100);

    for (int j = 1; j < pdfReader.NumberOfPages; j++)
    {
    PdfContentByte waterMark = stamp.GetUnderContent(1);
    waterMark.AddImage(img);
    }
    stamp.FormFlattening = true;
    stamp.Close();
    pdfReader.Close();
     
  18. technica

    technica New Member

    Joined:
    Dec 15, 2007
    Messages:
    107
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    hey mate great code, I wanted to use it in one of project that creates reports of user tracking data with company name as the watermark on the report.
     
  19. almedajohnson

    almedajohnson New Member

    Joined:
    Sep 17, 2010
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    It was completely new for me and I loved learning this new technique. I am goint to try this for sure. Thanks for sharing such nice resource.
     
  20. szoasis

    szoasis New Member

    Joined:
    Nov 23, 2010
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    I have decided to use naimish instead of arithforu, you can delete arithforu if you want.
     

Share This Page