Results 1 to 34 of 34

Thread: [VB.Net] Add watermark to existing Pdf file using iTextSharp

Threaded View

  1. #1

    Thread Starter
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    [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.  
    42.     ''' <summary>
    43.     ''' Add text as the watermark to each page of the source pdf to create a new pdf with text watermark
    44.     ''' </summary>
    45.     ''' <param name="sourceFile">the full path to the source pdf file</param>
    46.     ''' <param name="outputFile">the full path where the watermarked pdf file will be saved to</param>
    47.     ''' <param name="watermarkText">the text to use as the watermark</param>
    48.     ''' <param name="watermarkFont">the font to use for the watermark. The default font is HELVETICA</param>
    49.     ''' <param name="watermarkFontSize">the size of the font. The default size is 48</param>
    50.     ''' <param name="watermarkFontColor">the color of the watermark. The default color is blue</param>
    51.     ''' <param name="watermarkFontOpacity">the opacity of the watermark. The default opacity is 0.3</param>
    52.     ''' <param name="watermarkRotation">the rotation in degree of the watermark. The default rotation is 45 degree</param>
    53.     ''' <remarks></remarks>
    54.     Public Shared Sub AddWatermarkText(ByVal sourceFile As String, ByVal outputFile As String, ByVal watermarkText As String, _
    55.                                        Optional ByVal watermarkFont As iTextSharp.text.pdf.BaseFont = Nothing, _
    56.                                        Optional ByVal watermarkFontSize As Single = 48, _
    57.                                        Optional ByVal watermarkFontColor As iTextSharp.text.Color = Nothing, _
    58.                                        Optional ByVal watermarkFontOpacity As Single = 0.3F, _
    59.                                        Optional ByVal watermarkRotation As Single = 45.0F)
    60.  
    61.         Dim reader As iTextSharp.text.pdf.PdfReader = Nothing
    62.         Dim stamper As iTextSharp.text.pdf.PdfStamper = Nothing
    63.         Dim gstate As iTextSharp.text.pdf.PdfGState = Nothing
    64.         Dim underContent As iTextSharp.text.pdf.PdfContentByte = Nothing
    65.         Dim rect As iTextSharp.text.Rectangle = Nothing
    66.  
    67.         Dim pageCount As Integer = 0
    68.         Try
    69.             reader = New iTextSharp.text.pdf.PdfReader(sourceFile)
    70.             rect = reader.GetPageSizeWithRotation(1)
    71.             stamper = New iTextSharp.text.pdf.PdfStamper(reader, New System.IO.FileStream(outputFile, IO.FileMode.Create))
    72.             If watermarkFont Is Nothing Then
    73.                 watermarkFont = iTextSharp.text.pdf.BaseFont.CreateFont(iTextSharp.text.pdf.BaseFont.HELVETICA, _
    74.                                                               iTextSharp.text.pdf.BaseFont.CP1252, _
    75.                                                               iTextSharp.text.pdf.BaseFont.NOT_EMBEDDED)
    76.             End If
    77.             If watermarkFontColor Is Nothing Then
    78.                 watermarkFontColor = iTextSharp.text.Color.BLUE
    79.             End If
    80.             gstate = New iTextSharp.text.pdf.PdfGState()
    81.             gstate.FillOpacity = watermarkFontOpacity
    82.             gstate.StrokeOpacity = watermarkFontOpacity
    83.             pageCount = reader.NumberOfPages()
    84.             For i As Integer = 1 To pageCount
    85.                 underContent = stamper.GetUnderContent(i)
    86.                 With underContent
    87.                     .SaveState()
    88.                     .SetGState(gstate)
    89.                     .SetColorFill(watermarkFontColor)
    90.                     .BeginText()
    91.                     .SetFontAndSize(watermarkFont, watermarkFontSize)
    92.                     .SetTextMatrix(30, 30)
    93.                     .ShowTextAligned(iTextSharp.text.Element.ALIGN_CENTER, watermarkText, rect.Width / 2, rect.Height / 2, watermarkRotation)
    94.                     .EndText()
    95.                     .RestoreState()
    96.                 End With
    97.             Next
    98.             stamper.Close()
    99.             reader.Close()
    100.         Catch ex As Exception
    101.             Throw ex
    102.         End Try
    103.     End Sub
    Last edited by stanav; Apr 21st, 2008 at 08:24 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width