To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
VBForums  

VB Wire News
Article :: Building Dynamic Systems with Expressions in .NET
How Is XML Like An Interface?
Understanding Covariance and Contravariance
Print VS 2010 Keyboard Shortcut References in Letter (8.5x11in) and A4 (210×297mm) Sizes
Updated Productivity Power Tools



Go Back   VBForums > VBForums CodeBank > CodeBank - Visual Basic .NET

Reply Post New Thread
 
Thread Tools Display Modes
Old Apr 16th, 2008, 09:04 AM   #1
stanav
PowerPoster
 
stanav's Avatar
 
Join Date: Jul 06
Location: Providence, RI - USA
Posts: 7,309
stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)
[VB.Net] Add watermark to existing Pdf file using iTextSharp

Hello all again,
Recently, a vbforum member posted a question in VB.Net forum on how to add watermark to Pdf pages. Although I've never had to do it, but I know for sure that iTextSharp is capable of this. So I spent some spare time writing some code to do it using the free iTextSharp library, and here it is, the 2 subroutines for adding an image and text as the watermark on each page of a pdf file.

Don't forget to add a reference to itextsharp.dll to your project!!!

vb.net Code:
  1. ''' <summary>
  2.     ''' Add and image as the watermark on each page of the source pdf to create a new pdf with watermark
  3.     ''' </summary>
  4.     ''' <param name="sourceFile">the full path to the source pdf</param>
  5.     ''' <param name="outputFile">the full path where the watermarked pdf will be saved to</param>
  6.     ''' <param name="watermarkImage">the full path to the image file to use as the watermark</param>
  7.     ''' <remarks>The watermark image will be align in the center of each page</remarks>
  8.     Public Shared Sub AddWatermarkImage(ByVal sourceFile As String, ByVal outputFile As String, ByVal watermarkImage As String)
  9.         Dim reader As iTextSharp.text.pdf.PdfReader = Nothing
  10.         Dim stamper As iTextSharp.text.pdf.PdfStamper = Nothing
  11.         Dim img As iTextSharp.text.Image = Nothing
  12.         Dim underContent As iTextSharp.text.pdf.PdfContentByte = Nothing
  13.         Dim rect As iTextSharp.text.Rectangle = Nothing
  14.         Dim X, Y As Single
  15.         Dim pageCount As Integer = 0
  16.         Try
  17.             reader = New iTextSharp.text.pdf.PdfReader(sourceFile)
  18.             rect = reader.GetPageSizeWithRotation(1)
  19.             stamper = New iTextSharp.text.pdf.PdfStamper(reader, New System.IO.FileStream(outputFile, IO.FileMode.Create))
  20.             img = iTextSharp.text.Image.GetInstance(watermarkImage)
  21.             If img.Width > rect.Width OrElse img.Height > rect.Height Then
  22.                 img.ScaleToFit(rect.Width, rect.Height)
  23.                 X = (rect.Width - img.ScaledWidth) / 2
  24.                 Y = (rect.Height - img.ScaledHeight) / 2
  25.             Else
  26.                 X = (rect.Width - img.Width) / 2
  27.                 Y = (rect.Height - img.Height) / 2
  28.             End If
  29.             img.SetAbsolutePosition(X, Y)
  30.             pageCount = reader.NumberOfPages()
  31.             For i As Integer = 1 To pageCount
  32.                 underContent = stamper.GetUnderContent(i)
  33.                 underContent.AddImage(img)
  34.             Next
  35.             stamper.Close()
  36.             reader.Close()
  37.         Catch ex As Exception
  38.             Throw ex
  39.         End Try
  40.     End Sub
  41.     ''' <summary>
  42.     ''' Add text as the watermark to each page of the source pdf to create a new pdf with text watermark
  43.     ''' </summary>
  44.     ''' <param name="sourceFile">the full path to the source pdf file</param>
  45.     ''' <param name="outputFile">the full path where the watermarked pdf file will be saved to</param>
  46.     ''' <param name="watermarkText">the text to use as the watermark</param>
  47.     ''' <param name="watermarkFont">the font to use for the watermark. The default font is HELVETICA</param>
  48.     ''' <param name="watermarkFontSize">the size of the font. The default size is 48</param>
  49.     ''' <param name="watermarkFontColor">the color of the watermark. The default color is blue</param>
  50.     ''' <param name="watermarkFontOpacity">the opacity of the watermark. The default opacity is 0.3</param>
  51.     ''' <param name="watermarkRotation">the rotation in degree of the watermark. The default rotation is 45 degree</param>
  52.     ''' <remarks></remarks>
  53.     Public Shared Sub AddWatermarkText(ByVal sourceFile As String, ByVal outputFile As String, ByVal watermarkText As String, _
  54.                                        Optional ByVal watermarkFont As iTextSharp.text.pdf.BaseFont = Nothing, _
  55.                                        Optional ByVal watermarkFontSize As Single = 48, _
  56.                                        Optional ByVal watermarkFontColor As iTextSharp.text.Color = Nothing, _
  57.                                        Optional ByVal watermarkFontOpacity As Single = 0.3F, _
  58.                                        Optional ByVal watermarkRotation As Single = 45.0F)
  59.         Dim reader As iTextSharp.text.pdf.PdfReader = Nothing
  60.         Dim stamper As iTextSharp.text.pdf.PdfStamper = Nothing
  61.         Dim gstate As iTextSharp.text.pdf.PdfGState = Nothing
  62.         Dim underContent As iTextSharp.text.pdf.PdfContentByte = Nothing
  63.         Dim rect As iTextSharp.text.Rectangle = Nothing
  64.         Dim pageCount As Integer = 0
  65.         Try
  66.             reader = New iTextSharp.text.pdf.PdfReader(sourceFile)
  67.             rect = reader.GetPageSizeWithRotation(1)
  68.             stamper = New iTextSharp.text.pdf.PdfStamper(reader, New System.IO.FileStream(outputFile, IO.FileMode.Create))
  69.             If watermarkFont Is Nothing Then
  70.                 watermarkFont = iTextSharp.text.pdf.BaseFont.CreateFont(iTextSharp.text.pdf.BaseFont.HELVETICA, _
  71.                                                               iTextSharp.text.pdf.BaseFont.CP1252, _
  72.                                                               iTextSharp.text.pdf.BaseFont.NOT_EMBEDDED)
  73.             End If
  74.             If watermarkFontColor Is Nothing Then
  75.                 watermarkFontColor = iTextSharp.text.Color.BLUE
  76.             End If
  77.             gstate = New iTextSharp.text.pdf.PdfGState()
  78.             gstate.FillOpacity = watermarkFontOpacity
  79.             gstate.StrokeOpacity = watermarkFontOpacity
  80.             pageCount = reader.NumberOfPages()
  81.             For i As Integer = 1 To pageCount
  82.                 underContent = stamper.GetUnderContent(i)
  83.                 With underContent
  84.                     .SaveState()
  85.                     .SetGState(gstate)
  86.                     .SetColorFill(watermarkFontColor)
  87.                     .BeginText()
  88.                     .SetFontAndSize(watermarkFont, watermarkFontSize)
  89.                     .SetTextMatrix(30, 30)
  90.                     .ShowTextAligned(iTextSharp.text.Element.ALIGN_CENTER, watermarkText, rect.Width / 2, rect.Height / 2, watermarkRotation)
  91.                     .EndText()
  92.                     .RestoreState()
  93.                 End With
  94.             Next
  95.             stamper.Close()
  96.             reader.Close()
  97.         Catch ex As Exception
  98.             Throw ex
  99.         End Try
  100.     End Sub

Last edited by stanav; Apr 21st, 2008 at 08:24 AM.
stanav is offline   Reply With Quote
Old Apr 16th, 2008, 12:36 PM   #2
stanav
PowerPoster
 
stanav's Avatar
 
Join Date: Jul 06
Location: Providence, RI - USA
Posts: 7,309
stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)
Re: [VB.Net] Add watermark to existing Pdf file using iTextSharp

This is the updated subroutine for adding multiple text line watermark to pdf pages. The number of lines depending on the number of elements in the string array watermarkText() you pass in.

vb.net Code:
  1. ''' <summary>
  2.     ''' Add text as the watermark to each page of the source pdf to create a new pdf with text watermark
  3.     ''' </summary>
  4.     ''' <param name="sourceFile">the full path to the source pdf file</param>
  5.     ''' <param name="outputFile">the full path where the watermarked pdf file will be saved to</param>
  6.     ''' <param name="watermarkText">the string array conntaining the text to use as the watermark. Each element is treated as a line in the watermark</param>
  7.     ''' <param name="watermarkFont">the font to use for the watermark. The default font is HELVETICA</param>
  8.     ''' <param name="watermarkFontSize">the size of the font. The default size is 48</param>
  9.     ''' <param name="watermarkFontColor">the color of the watermark. The default color is blue</param>
  10.     ''' <param name="watermarkFontOpacity">the opacity of the watermark. The default opacity is 0.3</param>
  11.     ''' <param name="watermarkRotation">the rotation in degree of the watermark. The default rotation is 45 degree</param>
  12.     ''' <remarks></remarks>
  13.     Public Shared Sub AddWatermarkText(ByVal sourceFile As String, ByVal outputFile As String, ByVal watermarkText() As String, _
  14.                                        Optional ByVal watermarkFont As iTextSharp.text.pdf.BaseFont = Nothing, _
  15.                                        Optional ByVal watermarkFontSize As Single = 48, _
  16.                                        Optional ByVal watermarkFontColor As iTextSharp.text.Color = Nothing, _
  17.                                        Optional ByVal watermarkFontOpacity As Single = 0.3F, _
  18.                                        Optional ByVal watermarkRotation As Single = 45.0F)
  19.         Dim reader As iTextSharp.text.pdf.PdfReader = Nothing
  20.         Dim stamper As iTextSharp.text.pdf.PdfStamper = Nothing
  21.         Dim gstate As iTextSharp.text.pdf.PdfGState = Nothing
  22.         Dim underContent As iTextSharp.text.pdf.PdfContentByte = Nothing
  23.         Dim rect As iTextSharp.text.Rectangle = Nothing
  24.         Dim currentY As Single = 0.0F
  25.         Dim offset As Single = 0.0F
  26.         Dim pageCount As Integer = 0
  27.         Try
  28.             reader = New iTextSharp.text.pdf.PdfReader(sourceFile)
  29.             rect = reader.GetPageSizeWithRotation(1)
  30.             stamper = New iTextSharp.text.pdf.PdfStamper(reader, New System.IO.FileStream(outputFile, IO.FileMode.Create))
  31.             If watermarkFont Is Nothing Then
  32.                 watermarkFont = iTextSharp.text.pdf.BaseFont.CreateFont(iTextSharp.text.pdf.BaseFont.HELVETICA, _
  33.                                                               iTextSharp.text.pdf.BaseFont.CP1252, _
  34.                                                               iTextSharp.text.pdf.BaseFont.NOT_EMBEDDED)
  35.             End If
  36.             If watermarkFontColor Is Nothing Then
  37.                 watermarkFontColor = iTextSharp.text.Color.BLUE
  38.             End If
  39.             gstate = New iTextSharp.text.pdf.PdfGState()
  40.             gstate.FillOpacity = watermarkFontOpacity
  41.             gstate.StrokeOpacity = watermarkFontOpacity
  42.             pageCount = reader.NumberOfPages()
  43.             For i As Integer = 1 To pageCount
  44.                 underContent = stamper.GetOverContent(i)
  45.                 With underContent
  46.                     .SaveState()
  47.                     .SetGState(gstate)
  48.                     .SetColorFill(watermarkFontColor)
  49.                     .BeginText()
  50.                     .SetFontAndSize(watermarkFont, watermarkFontSize)
  51.                     .SetTextMatrix(30, 30)
  52.                     If watermarkText.Length > 1 Then
  53.                         currentY = (rect.Height / 2) + ((watermarkFontSize * watermarkText.Length) / 2)
  54.                     Else
  55.                         currentY = (rect.Height / 2)
  56.                     End If
  57.                     For j As Integer = 0 To watermarkText.Length - 1
  58.                         If j > 0 Then
  59.                             offset = (j * watermarkFontSize) + (watermarkFontSize / 4) * j
  60.                         Else
  61.                             offset = 0.0F
  62.                         End If
  63.                         .ShowTextAligned(iTextSharp.text.Element.ALIGN_CENTER, watermarkText(j), rect.Width / 2, currentY - offset, watermarkRotation)
  64.                     Next
  65.                     .EndText()
  66.                     .RestoreState()
  67.                 End With
  68.             Next
  69.             stamper.Close()
  70.             reader.Close()
  71.         Catch ex As Exception
  72.             Throw ex
  73.         End Try
  74.     End Sub
stanav is offline   Reply With Quote
Old May 6th, 2008, 09:29 PM   #3
WrongWayRookie
New Member
 
Join Date: May 08
Posts: 2
WrongWayRookie is an unknown quantity at this point (<10)
Re: [VB.Net] Add watermark to existing Pdf file using iTextSharp

Thanks for the code. I'm very new to iTextSHarp.
WrongWayRookie is offline   Reply With Quote
Old May 6th, 2008, 09:35 PM   #4
WrongWayRookie
New Member
 
Join Date: May 08
Posts: 2
WrongWayRookie is an unknown quantity at this point (<10)
Re: [VB.Net] Add watermark to existing Pdf file using iTextSharp

How do I get CRLF in to the document? All of the code is on a single line.

Thanks for your help
WrongWayRookie is offline   Reply With Quote
Old May 7th, 2008, 08:00 AM   #5
stanav
PowerPoster
 
stanav's Avatar
 
Join Date: Jul 06
Location: Providence, RI - USA
Posts: 7,309
stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)
Re: [VB.Net] Add watermark to existing Pdf file using iTextSharp

Quote:
Originally Posted by WrongWayRookie
How do I get CRLF in to the document? All of the code is on a single line.

Thanks for your help
Use the overloaded AddWatermrtkText sub in post #2 which you pass in a string array instead of a single string. Each element in your string array will be rendered as a line in the watermark.
stanav is offline   Reply With Quote
Old Jul 18th, 2008, 04:15 AM   #6
anwar_khan
New Member
 
Join Date: Jul 08
Posts: 2
anwar_khan is an unknown quantity at this point (<10)
Re: [VB.Net] Add watermark to existing Pdf file using iTextSharp

hi this really a nice code but the prob lem with i don't wanna create another file i wanna put water mark on exssiting file and dispaly it plz help how can i do this

thanks
anwar khan
anwar_khan is offline   Reply With Quote
Old Jul 18th, 2008, 05:21 AM   #7
stanav
PowerPoster
 
stanav's Avatar
 
Join Date: Jul 06
Location: Providence, RI - USA
Posts: 7,309
stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)
Re: [VB.Net] Add watermark to existing Pdf file using iTextSharp

The reason why you have to create another file is that when you open the original file for reading, it's being used and you can not write to it. Having said that, you can simply create a new temp file, and once done, you delete the original and rename the temp file to the original's name. This can be done with just a few lines of extra code.
__________________
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
- Abraham Lincoln -
stanav is offline   Reply With Quote
Old Jul 18th, 2008, 05:25 AM   #8
anwar_khan
New Member
 
Join Date: Jul 08
Posts: 2
anwar_khan is an unknown quantity at this point (<10)
Re: [VB.Net] Add watermark to existing Pdf file using iTextSharp

thanks for the reply can you give me code how can do it or basically i am doing i am showing a pdf file in asp.net page what my requirement is that when user clikc on print then the watermark put on that file and print it can we do that

thanks
anwar khan

Last edited by anwar_khan; Jul 18th, 2008 at 05:32 AM.
anwar_khan is offline   Reply With Quote
Old Jul 18th, 2008, 07:12 AM   #9
stanav
PowerPoster
 
stanav's Avatar
 
Join Date: Jul 06
Location: Providence, RI - USA
Posts: 7,309
stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)
Re: [VB.Net] Add watermark to existing Pdf file using iTextSharp

Quote:
Originally Posted by anwar_khan
thanks for the reply can you give me code how can do it or basically i am doing i am showing a pdf file in asp.net page what my requirement is that when user clikc on print then the watermark put on that file and print it can we do that

thanks
anwar khan
That's beyond the scope of this thread. You should post your question in asp.net forum... However, you might have to reconsider your logics. When you show a non-watermarked pdf to the user, the user can just print that version (using the print button in Adobe Reader) or save it to their computer instead of clicking on your print button to print a watermarked pdf.
__________________
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
- Abraham Lincoln -
stanav is offline   Reply With Quote
Old Aug 7th, 2009, 03:55 PM   #10
pessi
New Member
 
Join Date: Aug 09
Posts: 2
pessi is an unknown quantity at this point (<10)
Re: [VB.Net] Add watermark to existing Pdf file using iTextSharp

Excellent code. I use it to create an online application.

I made it in asp.net using c#

http://watermark-images.com/pdf-watermark.aspx
pessi is offline   Reply With Quote
Old Aug 7th, 2009, 07:45 PM   #11
stanav
PowerPoster
 
stanav's Avatar
 
Join Date: Jul 06
Location: Providence, RI - USA
Posts: 7,309
stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)
Re: [VB.Net] Add watermark to existing Pdf file using iTextSharp

Quote:
Originally Posted by pessi View Post
Excellent code. I use it to create an online application.

I made it in asp.net using c#

http://watermark-images.com/pdf-watermark.aspx
Nice website... Keep up the good work
__________________
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
- Abraham Lincoln -
stanav is offline   Reply With Quote
Old Aug 10th, 2009, 04:46 AM   #12
pessi
New Member
 
Join Date: Aug 09
Posts: 2
pessi is an unknown quantity at this point (<10)
Re: [VB.Net] Add watermark to existing Pdf file using iTextSharp

Thank you. I am in the process of building 10 more nice applications that can be of use to lot of people.
pessi is offline   Reply With Quote
Old Sep 29th, 2009, 11:23 PM   #13
BigJRofC
New Member
 
Join Date: Sep 09
Posts: 4
BigJRofC is an unknown quantity at this point (<10)
Question Re: [VB.Net] Add watermark to existing Pdf file using iTextSharp

This is excellent code and this is what i needed. BTW, I have question if this is possible, can the watermark appear behind page?

Thanks
BigJRofC is offline   Reply With Quote
Old Sep 30th, 2009, 08:01 AM   #14
stanav
PowerPoster
 
stanav's Avatar
 
Join Date: Jul 06
Location: Providence, RI - USA
Posts: 7,309
stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)stanav is a name known to all (1000+)
Re: [VB.Net] Add watermark to existing Pdf file using iTextSharp

Quote:
Originally Posted by BigJRofC View Post
This is excellent code and this is what i needed. BTW, I have question if this is possible, can the watermark appear behind page?

Thanks
Yes, in the code I posted, the water mark is written to the over content of the page... This will ensure that the water mark will always be visible (even when the page contains an image, for example). If your page contains mostly text, it is recommended to put the water mark on the under content of the page instead. All you have to do is changing this line:
Code:
underContent = stamper.GetOverContent(i)
To this
Code:
underContent = stamper.GetUnderContent(i)
The rest of the code remains unchanged.
__________________
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
- Abraham Lincoln -
stanav is offline   Reply With Quote
Old Oct 1st, 2009, 12:40 AM   #15
BigJRofC
New Member
 
Join Date: Sep 09
Posts: 4
BigJRofC is an unknown quantity at this point (<10)
Re: [VB.Net] Add watermark to existing Pdf file using iTextSharp

Thanks, Stanav. This is great...
BigJRofC is offline   Reply With Quote
Old Oct 8th, 2009, 05:27 PM   #16
gnoter
New Member
 
Join Date: Oct 09
Posts: 2
gnoter is an unknown quantity at this point (<10)
Cool Re: [VB.Net] Add watermark to existing Pdf file using iTextSharp

For those of us who find this solution via a web search and want a C# (C-Sharp) version, here it is, tested and working.

Foremost: Major thx to stanav; excellent post.

You'll note there are 2 functions, with the first functions calling the second function. This mimics the "OPTIONAL" params in VB

C-Sharp Code:
    public static void AddWatermarkTextC(string sourceFile, string outputFile, string[] watermarkText)   {     iTextSharp.text.pdf.BaseFont tWatermarkFont = null;     float tWatermarkFontSize = 48F;     iTextSharp.text.Color tWatermarkFontColor = null;     float tWatermarkFontOpacity = 0.3F;     float tWatermarkRotation = 45.0F;     tWatermarkFont = iTextSharp.text.pdf.BaseFont.CreateFont(iTextSharp.text.pdf.BaseFont.HELVETICA, iTextSharp.text.pdf.BaseFont.CP1252, iTextSharp.text.pdf.BaseFont.NOT_EMBEDDED);     tWatermarkFontColor = iTextSharp.text.Color.BLUE;     AddWatermarkTextC(sourceFile, outputFile, watermarkText, tWatermarkFont, tWatermarkFontSize, tWatermarkFontColor, tWatermarkFontOpacity, tWatermarkRotation);   }//void AddWatermarkTextC(string sourceFile, string outputFile, string[] watermarkText )   public static void AddWatermarkTextC(string sourceFile, string outputFile, string[] watermarkText, iTextSharp.text.pdf.BaseFont watermarkFont, float watermarkFontSize, iTextSharp.text.Color watermarkFontColor, float watermarkFontOpacity, float watermarkRotation)   {     iTextSharp.text.pdf.PdfReader reader = null;     iTextSharp.text.pdf.PdfStamper stamper = null;     iTextSharp.text.pdf.PdfGState gstate = null;     iTextSharp.text.pdf.PdfContentByte underContent = null;     iTextSharp.text.Rectangle rect = null;     float currentY = 0.0F;     float offset = 0.0F;     int pageCount = 0;     try     {       reader = new iTextSharp.text.pdf.PdfReader(sourceFile);       rect = reader.GetPageSizeWithRotation(1);       stamper = new iTextSharp.text.pdf.PdfStamper(reader, new System.IO.FileStream(outputFile, System.IO.FileMode.Create));       if (watermarkFont == null)       {         watermarkFont = iTextSharp.text.pdf.BaseFont.CreateFont(iTextSharp.text.pdf.BaseFont.HELVETICA, iTextSharp.text.pdf.BaseFont.CP1252, iTextSharp.text.pdf.BaseFont.NOT_EMBEDDED);       }//if (watermarkFont == null)       if (watermarkFontColor == null)       {         watermarkFontColor = iTextSharp.text.Color.BLUE;       }//if (watermarkFontColor == null)       gstate = new iTextSharp.text.pdf.PdfGState();       gstate.FillOpacity = watermarkFontOpacity;       gstate.StrokeOpacity = watermarkFontOpacity;       pageCount = reader.NumberOfPages;       for (int i = 1; i <= pageCount; i++)       {         underContent = stamper.GetOverContent(i);         underContent.SaveState();         underContent.SetGState(gstate);         underContent.SetColorFill(watermarkFontColor);         underContent.BeginText();         underContent.SetFontAndSize(watermarkFont, watermarkFontSize);         underContent.SetTextMatrix(30, 30);         if (watermarkText.Length > 1)         {           currentY = (rect.Height / 2) + ((watermarkFontSize * watermarkText.Length) / 2);         }//if (watermarkText.Length > 1)         else         {           currentY = (rect.Height / 2);         }//else if (watermarkText.Length > 1)         for (int j = 0; j < watermarkText.Length; j++)         {           if (j > 0)           {             offset = (j * watermarkFontSize) + (watermarkFontSize / 4) * j;           }//if (j > 0)           else           {             offset = 0.0F;           }//else if (j > 0)           underContent.ShowTextAligned(iTextSharp.text.Element.ALIGN_CENTER, watermarkText[j], rect.Width / 2, currentY - offset, watermarkRotation);         }//for (int j = 0; j < watermarkText.Length; j++)         underContent.EndText();         underContent.RestoreState();       }//for (int i = 1; i <= pageCount; i++)       stamper.Close();       reader.Close();     }     catch (Exception ex)     {       throw ex;     }   }//void AddWatermarkTextC(string sourceFile, string outputFile, string[] watermarkText, iTextSharp.text.pdf.BaseFont watermarkFont, float watermarkFontSize, iTextSharp.text.Color watermarkFontColor, float watermarkFontOpacity, float watermarkRotation)

Signed,

MacSpudster / GNoter
==
ASPX = Apple Simply Performs eXcellently

Last edited by gnoter; Oct 8th, 2009 at 05:30 PM.
gnoter is offline   Reply With Quote
Old Oct 14th, 2009, 07:26 AM   #17
BigJRofC
New Member
 
Join Date: Sep 09
Posts: 4
BigJRofC is an unknown quantity at this point (<10)
Re: [VB.Net] Add watermark to existing Pdf file using iTextSharp

Hi Stanav,

I want to use Arial 8 pt in size font base. How to change the font name? Thanks in advance.

Regards,
Buddy
BigJRofC is offline   Reply With Quote
Old Oct 14th, 2009, 12:42 PM   #18
gnoter
New Member
 
Join Date: Oct 09
Posts: 2
gnoter is an unknown quantity at this point (<10)
Re: [VB.Net] Add watermark to existing Pdf file using iTextSharp

Buddy,

Dim MyNewFont As iTextSharp.text.Font = iTextSharp.text.FontFactory.GetFont(FontNameSTRING, FontSizeINT)

where FontNameSTRING = "Arial" and FontSizeINT = 8

such as

Dim Arial8Plain As iTextSharp.text.Font = iTextSharp.text.FontFactory.GetFont("Arial", 8)

Then use the variable Arial8Plain where you'd like.

Signed,

MacSpudster / GNoter
==
ASPX = Apple Simply Performs eXcellently
gnoter is offline   Reply With Quote
Old Oct 15th, 2009, 01:43 AM   #19
BigJRofC
New Member
 
Join Date: Sep 09
Posts: 4
BigJRofC is an unknown quantity at this point (<10)
Re: [VB.Net] Add watermark to existing Pdf file using iTextSharp

Thanks, MacSpudster.

What I need to do, instead of using Helvetica on the above code of Stanav I will use Arial font as a Base Font of the text in my watermark.

Regards,
Buddy
BigJRofC is offline   Reply With Quote
Old Oct 27th, 2009, 10:44 AM   #20
slow&steady
New Member
 
Join Date: Oct 09
Posts: 5
slow&steady is an unknown quantity at this point (<10)
Re: [VB.Net] Add watermark to existing Pdf file using iTextSharp

Hi

i want to get the following font
"Heading 1+93 pt, Bold, Outlined, Black, Times New Roman"

when i declared like below

Dim TNR_93 As iTextSharp.text.Font = iTextSharp.text.FontFactory.GetFont("TimesNewRoman", 93)

and pass it at watermarkFont like this

AddWatermarkText(FileName, Path & "\" & Fname(0) & "_WTRMK." & Fname(1), Ptext, TNR_93)

i am getting error like this

""Value of type 'iTextSharp.text.Font' cannot be converted 'iTextSharp.text.pdf.BaseFont'""

Please Help me

its very urgent !!!!!!!!!!

Thanks in advance

Sri
slow&steady is offline   Reply With Quote
Old Oct 27th, 2009, 12:05 PM   #21
slow&steady
New Member
 
Join Date: Oct 09
Posts: 5
slow&steady is an unknown quantity at this point (<10)
Re: [VB.Net] Add watermark to existing Pdf file using iTextSharp

One more question is

I want to watermark two lines

one with fontsize 93 and other with 14

also i want to include the date on the Topleft and company logo on topright

is it possible ?

i tried with the above code it is overwriting , instead of having all

Can anybody suggest how to do it

Thanks in advance,

Sri
slow&steady is offline   Reply With Quote
Old Dec 4th, 2009, 12:38 AM   #22
kpmsivachand
Addicted Member
 
Join Date: Feb 08
Location: XP & Vista
Posts: 181
kpmsivachand is on a distinguished road (10+)
Thumbs up Re: [VB.Net] Add watermark to existing Pdf file using iTextSharp

Quote:
Originally Posted by slow&steady View Post
One more question is

I want to watermark two lines

one with fontsize 93 and other with 14

also i want to include the date on the Topleft and company logo on topright

is it possible ?

i tried with the above code it is overwriting , instead of having all

Can anybody suggest how to do it

Thanks in advance,

Sri
May be my project will helpful to you : http://www.softpedia.com/get/Office-...xploiter.shtml

Just take a screen shot which u want like (one with fontsize 93 and other with 14 etc) and use second option as watermark by image method and do it

Hope it helpful!
kpmsivachand is offline   Reply With Quote
Old Apr 29th, 2010, 12:52 PM   #23
hackerspk
Junior Member
 
Join Date: Dec 09
Posts: 27
hackerspk is an unknown quantity at this point (<10)
Re: [VB.Net] Add watermark to existing Pdf file using iTextSharp

well done
hackerspk is offline   Reply With Quote
Reply

Go Back   VBForums > VBForums CodeBank > CodeBank - Visual Basic .NET


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 08:03 PM.





Acceptable Use Policy

Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.