Skip to content

Instantly share code, notes, and snippets.

@alfredfrancis
Created April 18, 2017 12:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alfredfrancis/76ccfafe0d75a43afc7f2f609ccbefb0 to your computer and use it in GitHub Desktop.
Save alfredfrancis/76ccfafe0d75a43afc7f2f609ccbefb0 to your computer and use it in GitHub Desktop.
Imports iTextSharp.text.pdf
Imports iTextSharp.text
Imports System.IO
Imports iTextSharp.text.io
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System
Imports System.Collections.Generic
Imports System.Windows.Forms
Public Class Form1
Dim img As iTextSharp.text.Image
Dim imgs As New Paragraph
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
Dim PdfDoc As New Document()
Dim PdfWrite As PdfWriter = PdfWriter.GetInstance(PdfDoc, New FileStream("C:\Temp\Simpl.pdf", FileMode.Create))
PdfDoc.Open()
PdfDoc.Add(New Paragraph("Hello World"))
PdfDoc.Close()
''' calling function to add water mark
AddWatermarkText("C:\Temp\Simpl.pdf","C:\Temp\SimplWaterMarked.pdf","Apollo Tyres")
End Sub
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)
''' <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>
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
End Class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment