We can use iTextSharp's IPdfPageEvent to display page numbers on your generated PDF file without manually computing the number of pages (or manually rendering some page number headers using GDI). In your class just add a code to import iTextSharp.text.pdf and make sure your class implements IPdfPageEVent.
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Public Class IPDFEvents
Implements IPdfPageEvent
...
This automatically adds default methods from the implement, but following are the most useful ones:
1. OnCloseDocument()
2. OnEndPage()
3. OnOpenDocument()
4. OnStartPage()
5. yourHeaderSetterFunction()
6. orYourFooterSetterFunction()
How it works? First off, let's define your header and footer setters:
Private Sub yourHeaderSetterFunction(ByVal oWriter As iTextSharp.text.pdf.PdfWriter, ByVal oDocument As Document)
Dim oTable As New PdfPTable(1)
Dim oCell As PdfPCell
With oTable
'---Column 1: some title
oCell = New PdfPCell( _
New iTextSharp.text.Phrase( _
rptHeader, _
FontFactory.GetFont("Arial", 8, iTextSharp.text.Font.BOLD)))
oCell.Border = 0
oCell.BackgroundColor = dColor
oCell.HorizontalAlignment = Element.ALIGN_LEFT
.AddCell(oCell)
'---Column 2: PageNumber
Dim iPageNumber As Integer = oWriter.PageNumber
Dim sText As String = "Page " & iPageNumber & " of "
oTable.TotalWidth = oDocument.PageSize.Width - 70
oTable.WriteSelectedRows(0, -1, 36, oDocument.PageSize.Height - 15, oWriter.DirectContent)
Dim fLen As Single = moBF.GetWidthPoint(sText, 10)
moCB.BeginText()
moCB.SetFontAndSize(moBF, 8)
moCB.SetTextMatrix(oDocument.PageSize.Width - 113, oDocument.PageSize.Height - 25)
moCB.ShowText(sText)
moCB.EndText()
moCB.AddTemplate(moTemplate, oDocument.PageSize.Width - 120 + fLen, oDocument.PageSize.Height - 25)
moCB.BeginText()
moCB.SetFontAndSize(moBF, 14)
moCB.SetTextMatrix(300, 300)
moCB.EndText()
End With
End Sub
Private Sub YourFooterSetterFunctionr( ByVal oWriter As iTextSharp.text.pdf.PdfWriter, ByVal oDocument As iTextSharp.text.Document)
Dim fLen As Single
Dim sText As String = "Your footer text"
Dim oCell As New PdfPCell
fLen = moBF.GetWidthPoint(sText, sText.Length())
moCB.BeginText()
moCB.SetFontAndSize(moBF, 8)
moCB.SetTextMatrix((oDocument.PageSize.Width - fLen) + (oDocument.PageSize.Width / 3) + _
(oDocument.PageSize.Width / 3), 26)
moCB.ShowText(sText)
moCB.EndText()
End Sub
Then we need to declare these variables inside our class:
Private moTemplate As PdfTemplate
Private moCB As PdfContentByte
Private moBF As BaseFont = Nothing
Dim dColor As iTextSharp.text.Color = New iTextSharp.text.Color(0, 255, 255)
Dim dFont As iTextSharp.text.Font = New iTextSharp.text.Font(iTextSharp.text.Font.TIMES_ROMAN, 8, iTextSharp.text.Font.BOLD)
And then issue calls to the necessary function derivatives such that:
Public Sub OnCloseDocument(ByVal writer As iTextSharp.text.pdf.PdfWriter, ByVal document As iTextSharp.text.Document) Implements iTextSharp.text.pdf.IPdfPageEvent.OnCloseDocument
moTemplate.BeginText()
moTemplate.SetFontAndSize(moBF, 8)
moTemplate.ShowText((writer.PageNumber - 1).ToString)
moTemplate.EndText()
End Sub
Public Sub OnEndPage(ByVal writer As iTextSharp.text.pdf.PdfWriter, ByVal document As iTextSharp.text.Document) Implements iTextSharp.text.pdf.IPdfPageEvent.OnEndPage
yourHeaderSetterFunction(writer, document)
yourFooterSetterFunction(writer, document)
End Sub
Public Sub OnOpenDocument(ByVal writer As iTextSharp.text.pdf.PdfWriter, ByVal document As iTextSharp.text.Document) Implements iTextSharp.text.pdf.IPdfPageEvent.OnOpenDocument
Try
moBF = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED)
moCB = writer.DirectContent
moTemplate = moCB.CreateTemplate(50, 50)
Catch de As DocumentException
MsgBox(de.Message())
Catch ioe As IOException
MsgBox(ioe.Message())
End Try
End Sub
Public Sub OnStartPage(ByVal writer As iTextSharp.text.pdf.PdfWriter, ByVal document As iTextSharp.text.Document) Implements iTextSharp.text.pdf.IPdfPageEvent.OnStartPage
pagecount = pagecount + 1
End Sub
andn viola! page number is displayed after implementing your class.
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Public Class IPDFEvents
Implements IPdfPageEvent
...
This automatically adds default methods from the implement, but following are the most useful ones:
1. OnCloseDocument()
2. OnEndPage()
3. OnOpenDocument()
4. OnStartPage()
5. yourHeaderSetterFunction()
6. orYourFooterSetterFunction()
How it works? First off, let's define your header and footer setters:
Private Sub yourHeaderSetterFunction(ByVal oWriter As iTextSharp.text.pdf.PdfWriter, ByVal oDocument As Document)
Dim oTable As New PdfPTable(1)
Dim oCell As PdfPCell
With oTable
'---Column 1: some title
oCell = New PdfPCell( _
New iTextSharp.text.Phrase( _
rptHeader, _
FontFactory.GetFont("Arial", 8, iTextSharp.text.Font.BOLD)))
oCell.Border = 0
oCell.BackgroundColor = dColor
oCell.HorizontalAlignment = Element.ALIGN_LEFT
.AddCell(oCell)
'---Column 2: PageNumber
Dim iPageNumber As Integer = oWriter.PageNumber
Dim sText As String = "Page " & iPageNumber & " of "
oTable.TotalWidth = oDocument.PageSize.Width - 70
oTable.WriteSelectedRows(0, -1, 36, oDocument.PageSize.Height - 15, oWriter.DirectContent)
Dim fLen As Single = moBF.GetWidthPoint(sText, 10)
moCB.BeginText()
moCB.SetFontAndSize(moBF, 8)
moCB.SetTextMatrix(oDocument.PageSize.Width - 113, oDocument.PageSize.Height - 25)
moCB.ShowText(sText)
moCB.EndText()
moCB.AddTemplate(moTemplate, oDocument.PageSize.Width - 120 + fLen, oDocument.PageSize.Height - 25)
moCB.BeginText()
moCB.SetFontAndSize(moBF, 14)
moCB.SetTextMatrix(300, 300)
moCB.EndText()
End With
End Sub
Private Sub YourFooterSetterFunctionr( ByVal oWriter As iTextSharp.text.pdf.PdfWriter, ByVal oDocument As iTextSharp.text.Document)
Dim fLen As Single
Dim sText As String = "Your footer text"
Dim oCell As New PdfPCell
fLen = moBF.GetWidthPoint(sText, sText.Length())
moCB.BeginText()
moCB.SetFontAndSize(moBF, 8)
moCB.SetTextMatrix((oDocument.PageSize.Width - fLen) + (oDocument.PageSize.Width / 3) + _
(oDocument.PageSize.Width / 3), 26)
moCB.ShowText(sText)
moCB.EndText()
End Sub
Then we need to declare these variables inside our class:
Private moTemplate As PdfTemplate
Private moCB As PdfContentByte
Private moBF As BaseFont = Nothing
Dim dColor As iTextSharp.text.Color = New iTextSharp.text.Color(0, 255, 255)
Dim dFont As iTextSharp.text.Font = New iTextSharp.text.Font(iTextSharp.text.Font.TIMES_ROMAN, 8, iTextSharp.text.Font.BOLD)
And then issue calls to the necessary function derivatives such that:
Public Sub OnCloseDocument(ByVal writer As iTextSharp.text.pdf.PdfWriter, ByVal document As iTextSharp.text.Document) Implements iTextSharp.text.pdf.IPdfPageEvent.OnCloseDocument
moTemplate.BeginText()
moTemplate.SetFontAndSize(moBF, 8)
moTemplate.ShowText((writer.PageNumber - 1).ToString)
moTemplate.EndText()
End Sub
Public Sub OnEndPage(ByVal writer As iTextSharp.text.pdf.PdfWriter, ByVal document As iTextSharp.text.Document) Implements iTextSharp.text.pdf.IPdfPageEvent.OnEndPage
yourHeaderSetterFunction(writer, document)
yourFooterSetterFunction(writer, document)
End Sub
Public Sub OnOpenDocument(ByVal writer As iTextSharp.text.pdf.PdfWriter, ByVal document As iTextSharp.text.Document) Implements iTextSharp.text.pdf.IPdfPageEvent.OnOpenDocument
Try
moBF = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED)
moCB = writer.DirectContent
moTemplate = moCB.CreateTemplate(50, 50)
Catch de As DocumentException
MsgBox(de.Message())
Catch ioe As IOException
MsgBox(ioe.Message())
End Try
End Sub
Public Sub OnStartPage(ByVal writer As iTextSharp.text.pdf.PdfWriter, ByVal document As iTextSharp.text.Document) Implements iTextSharp.text.pdf.IPdfPageEvent.OnStartPage
pagecount = pagecount + 1
End Sub
andn viola! page number is displayed after implementing your class.