I am using iTextSharp to export some data to PDF from my aspx page. What I want to do is to add a text at the bottom of PDF page.
My problem is that above this text I also export a gridview, which can be expanded according to how many lines are added by the user. In case the user adds a new row on the gridview table this text go one line below (at the pdf page). This is just an information
text and I want it always appear on the same position. I dont want it to change position if gridview gets bigger. For instance this text may appear on the second PDF page if gridview gets bigger which is not what I want to do.
I always want this text to appear at the bottom of the page.
Is there any way of doing this? Note that I am using c#.
Imports iTextSharp.text.pdf
Imports iTextSharp.text
Public Class pdfEvents
Inherits PdfPageEventHelper
' This is the contentbyte object of the writer
Private cb As PdfContentByte
' we will put the final number of pages in a template
Private template As PdfTemplate
' this is the BaseFont we are going to use for the header / footer
Private bf As BaseFont = Nothing
' This keeps track of the creation time
Dim PrintTime As Date = Now
Private _HeaderRightText As String
Public Property HeaderRightText() As String
Get
Return _HeaderRightText
End Get
Set(ByVal value As String)
_HeaderRightText = value
End Set
End Property
Private _HeaderLeftText As String
Public Property HeaderLeftText() As String
Get
Return _HeaderLeftText
End Get
Set(ByVal value As String)
_HeaderLeftText = value
End Set
End Property
Private _HeaderFont As Font
Public Property HeaderFont() As Font
Get
Return _HeaderFont
End Get
Set(value As Font)
_HeaderFont = value
End Set
End Property
Private _FooterFont As Font
Public Property FooterFont() As Font
Get
Return _FooterFont
End Get
Set(value As Font)
_FooterFont = value
End Set
End Property
#Region "Fields"
Private start_page_count As Integer
Private bShowFooter As Boolean
Sub New(showFooter As Boolean)
bShowFooter = showFooter
End Sub
#End Region 'Fields
#Region "Methods"
Public Overrides Sub OnOpenDocument(writer As PdfWriter, document As Document)
'MyBase.OnOpenDocument(writer, document)
PrintTime = Now
bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED)
cb = writer.DirectContent
template = cb.CreateTemplate(100, 50)
End Sub
Public Overrides Sub OnStartPage(writer As PdfWriter, document As Document)
MyBase.OnStartPage(writer, document)
Dim pageSize As Rectangle = document.PageSize
If Not String.IsNullOrEmpty(HeaderLeftText & HeaderRightText) Then
Dim HeaderTable As New PdfPTable(2)
HeaderTable.DefaultCell.VerticalAlignment = Element.ALIGN_MIDDLE
HeaderTable.TotalWidth = pageSize.Width - 50
HeaderTable.SetWidthPercentage(New Single() {45, 45}, pageSize)
Dim HeaderLeftCell As New PdfPCell(New Phrase(8, HeaderLeftText, HeaderFont))
HeaderLeftCell.Padding = 5
HeaderLeftCell.PaddingBottom = 8
HeaderLeftCell.BorderWidthRight = 0
HeaderLeftCell.Border = 0
HeaderTable.AddCell(HeaderLeftCell)
Dim HeaderRightCell As New PdfPCell(New Phrase(8, HeaderRightText, HeaderFont))
HeaderRightCell.HorizontalAlignment = PdfPCell.ALIGN_RIGHT
HeaderRightCell.Padding = 5
HeaderRightCell.PaddingBottom = 8
HeaderRightCell.BorderWidthLeft = 0
HeaderRightCell.Border = 0
HeaderTable.AddCell(HeaderRightCell)
cb.SetRGBColorFill(0, 0, 0)
HeaderTable.WriteSelectedRows(0, -1, pageSize.GetLeft(15), pageSize.GetTop(15), cb)
End If
End Sub
Public Overrides Sub OnEndPage(writer As PdfWriter, document As Document)
MyBase.OnEndPage(writer, document)
Dim pageN As Integer = writer.PageNumber
Dim text As [String] = "Page " & pageN & " of "
Dim len As Single = bf.GetWidthPoint(text, 8)
Dim pageSize As Rectangle = document.PageSize
cb.SetRGBColorFill(100, 100, 100)
If bShowFooter Then
cb.BeginText()
cb.SetFontAndSize(bf, 8)
cb.SetTextMatrix(pageSize.GetLeft(40), pageSize.GetBottom(30))
cb.ShowText(text)
cb.EndText()
cb.AddTemplate(template, pageSize.GetLeft(40) + len, pageSize.GetBottom(30))
cb.BeginText()
cb.SetFontAndSize(bf, 8)
cb.ShowTextAligned(PdfContentByte.ALIGN_RIGHT, "Printed On " & PrintTime.ToString(), pageSize.GetRight(40), pageSize.GetBottom(30), 0)
cb.EndText()
End If
start_page_count += 1
End Sub
Public Overrides Sub OnCloseDocument(writer As PdfWriter, document As Document)
MyBase.OnCloseDocument(writer, document)
template.BeginText()
template.SetFontAndSize(bf, 8)
template.SetTextMatrix(0, 0)
template.ShowText("" & (writer.PageNumber - 1))
template.EndText()
End Sub
#End Region 'Methods
End Class
Then I call like such:
' the PDF document itself;
Dim doc As New iTextSharp.text.Document()
Dim writer As PdfWriter = Nothing
writer = PdfWriter.GetInstance(doc, ms)
Dim oEventsHandler As New pdfEvents(docOptions.ShowFooter)
writer.PageEvent = oEventsHandler
oEventsHandler.HeaderFont = headerFont
oEventsHandler.HeaderLeftText = docOptions.HeaderLeftText
oEventsHandler.HeaderRightText = "" 'docOptions.HeaderRightText
iTextSharppdfCexport
Please Mark as Answer if You Find Useful!
But don't expect me to do your job!
Member
13 Points
58 Posts
Add text on specific X & Y coordinates at the PDF export file (i.e. bottom of PDF file)
Nov 12, 2014 01:52 AM|ImpossibleIsNothing|LINK
Hi,
I am using iTextSharp to export some data to PDF from my aspx page. What I want to do is to add a text at the bottom of PDF page.
My problem is that above this text I also export a gridview, which can be expanded according to how many lines are added by the user. In case the user adds a new row on the gridview table this text go one line below (at the pdf page). This is just an information text and I want it always appear on the same position. I dont want it to change position if gridview gets bigger. For instance this text may appear on the second PDF page if gridview gets bigger which is not what I want to do.
I always want this text to appear at the bottom of the page.
Is there any way of doing this? Note that I am using c#.
Thanks
iTextSharp pdf C export
Participant
1470 Points
554 Posts
Re: Add text on specific X & Y coordinates at the PDF export file (i.e. bottom of PDF file)
Nov 14, 2014 11:39 AM|UstesG|LINK
you can use the itextsharp PdfPageEventHelper to add footers. In your footer add the text you need/
iTextSharp pdf C export
But don't expect me to do your job!
Member
13 Points
58 Posts
Re: Add text on specific X & Y coordinates at the PDF export file (i.e. bottom of PDF file)
Nov 15, 2014 10:24 AM|ImpossibleIsNothing|LINK
Can you give me an example please?
iTextSharp pdf C export
Participant
1470 Points
554 Posts
Re: Add text on specific X & Y coordinates at the PDF export file (i.e. bottom of PDF file)
Nov 17, 2014 12:02 PM|UstesG|LINK
This is what I used last.
My pdfPageEventHelper:
Then I call like such:
iTextSharp pdf C export
But don't expect me to do your job!