''' <summary>
''' Add and image as the watermark on each page of the source pdf to create a new pdf with watermark
''' </summary>
''' <param name="sourceFile">the full path to the source pdf</param>
''' <param name="outputFile">the full path where the watermarked pdf will be saved to</param>
''' <param name="watermarkImage">the full path to the image file to use as the watermark</param>
''' <remarks>The watermark image will be align in the center of each page</remarks>
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
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()
Catch ex As Exception
Throw ex
End Try
End Sub
''' <summary>
''' Add text as the watermark to each page of the source pdf to create a new pdf with text watermark
''' </summary>
''' <param name="sourceFile">the full path to the source pdf file</param>
''' <param name="outputFile">the full path where the watermarked pdf file will be saved to</param>
''' <param name="watermarkText">the text to use as the watermark</param>
''' <param name="watermarkFont">the font to use for the watermark. The default font is HELVETICA</param>
''' <param name="watermarkFontSize">the size of the font. The default size is 48</param>
''' <param name="watermarkFontColor">the color of the watermark. The default color is blue</param>
''' <param name="watermarkFontOpacity">the opacity of the watermark. The default opacity is 0.3</param>
''' <param name="watermarkRotation">the rotation in degree of the watermark. The default rotation is 45 degree</param>
''' <remarks></remarks>
Public Shared Sub AddWatermarkText(ByVal sourceFile As String, ByVal outputFile As String, ByVal watermarkText As String, _
Optional ByVal watermarkFont As iTextSharp.text.pdf.BaseFont = Nothing, _
Optional ByVal watermarkFontSize As Single = 48, _
Optional ByVal watermarkFontColor As iTextSharp.text.Color = Nothing, _
Optional ByVal watermarkFontOpacity As Single = 0.3F, _
Optional ByVal watermarkRotation As Single = 45.0F)
Dim reader As iTextSharp.text.pdf.PdfReader = Nothing
Dim stamper As iTextSharp.text.pdf.PdfStamper = Nothing
Dim gstate As iTextSharp.text.pdf.PdfGState = Nothing
Dim underContent As iTextSharp.text.pdf.PdfContentByte = Nothing
Dim rect As iTextSharp.text.Rectangle = Nothing
Dim pageCount As Integer = 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, IO.FileMode.Create))
If watermarkFont Is Nothing Then
watermarkFont = iTextSharp.text.pdf.BaseFont.CreateFont(iTextSharp.text.pdf.BaseFont.HELVETICA, _
iTextSharp.text.pdf.BaseFont.CP1252, _
iTextSharp.text.pdf.BaseFont.NOT_EMBEDDED)
End If
If watermarkFontColor Is Nothing Then
watermarkFontColor = iTextSharp.text.Color.BLUE
End If
gstate = New iTextSharp.text.pdf.PdfGState()
gstate.FillOpacity = watermarkFontOpacity
gstate.StrokeOpacity = watermarkFontOpacity
pageCount = reader.NumberOfPages()
For i As Integer = 1 To pageCount
underContent = stamper.GetUnderContent(i)
With underContent
.SaveState()
.SetGState(gstate)
.SetColorFill(watermarkFontColor)
.BeginText()
.SetFontAndSize(watermarkFont, watermarkFontSize)
.SetTextMatrix(30, 30)
.ShowTextAligned(iTextSharp.text.Element.ALIGN_CENTER, watermarkText, rect.Width / 2, rect.Height / 2, watermarkRotation)
.EndText()
.RestoreState()
End With
Next
stamper.Close()
reader.Close()
Catch ex As Exception
Throw ex
End Try
End Sub