TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Dipak V Nakum
NA
39
3.2k
Header print on first page not to all using itextsharp
Apr 7 2018 1:51 AM
i am generating pdf using itextsharp, it show gridview's header on first page only not to all.
code as below.
Response.ContentType =
"application/pdf"
Response.AddHeader(
"content-disposition"
,
"attachment;filename=eVMS_Astral "
& Now &
".pdf"
)
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Dim
sw
As
StringWriter =
New
StringWriter()
Dim
hw
As
HtmlTextWriter =
New
HtmlTextWriter(sw)
Dim
gv
As
GridView =
New
GridView()
gv.HeaderStyle.Font.Bold =
True
gv.HeaderStyle.Font.Name =
"Arial"
gv.HeaderStyle.Font.Size = 7
gv.HeaderStyle.ForeColor = SystemColors.ActiveCaption
gv.HeaderStyle.BackColor = System.Drawing.Color.FromName(
"#144282"
)
gv.RowStyle.Font.Name =
"Arial"
gv.RowStyle.Font.Size = 7
Dim
ds
As
Data.DataSet = GetData()
'ds.Tables(0).Columns.RemoveAt(0)
gv.DataSource = ds
gv.AllowPaging =
False
gv.AllowSorting =
False
gv.GridLines = GridLines.None
gv.HeaderStyle.ForeColor = System.Drawing.Color.FromName(
"#144282"
)
gv.DataBind()
gv.RenderControl(hw)
Dim
sr
As
StringReader =
New
StringReader(sw.ToString())
Dim
pdfDoc
As
Document =
New
Document(PageSize.A4, 15.0F, 15.0F, 45.0F, 45.0F)
Dim
htmlparser
As
HTMLWorker =
New
HTMLWorker(pdfDoc)
Dim
pdfWriter
As
PdfWriter = pdfWriter.GetInstance(pdfDoc, Response.OutputStream)
'Dim page As PdfPage = New PdfPage()
Dim
page
As
myApp.pdfPage =
New
myApp.pdfPage()
pdfWriter.PageEvent = page
page.Title =
"Getrag Transmissions India Pvt Ltd - "
& Session(
"ReportTitle"
) & Space(60) &
"Printed By: "
& Session(
"UserName"
)
pdfDoc.AddCreationDate()
pdfDoc.Open()
htmlparser.Parse(sr)
pdfDoc.Close()
Response.Write(pdfDoc)
Response.
End
()
Imports
Microsoft.VisualBasic
Imports
System
Imports
iTextSharp.text
Imports
iTextSharp.text.pdf
Namespace
myApp
Public
Class
pdfPage
Inherits
iTextSharp.text.pdf.PdfPageEventHelper
Private
cb
As
PdfContentByte
Private
template
As
PdfTemplate
Private
template1
As
PdfTemplate
Private
bf
As
BaseFont =
Nothing
Private
PrintTime
As
DateTime = DateTime.Now
Private
_Title
As
String
Public
Property
Title()
As
String
Get
Return
_Title
End
Get
Set
(
ByVal
value
As
String
)
_Title = value
End
Set
End
Property
Private
_HeaderLeft
As
String
Public
Property
HeaderLeft()
As
String
Get
Return
_HeaderLeft
End
Get
Set
(
ByVal
value
As
String
)
_HeaderLeft = value
End
Set
End
Property
Private
_HeaderRight
As
String
Public
Property
HeaderRight()
As
String
Get
Return
_HeaderRight
End
Get
Set
(
ByVal
value
As
String
)
_HeaderRight = value
End
Set
End
Property
Private
_HeaderFont
As
Font
Public
Property
HeaderFont()
As
Font
Get
Return
_HeaderFont
End
Get
Set
(
ByVal
value
As
Font)
_HeaderFont = value
End
Set
End
Property
Private
_FooterFont
As
Font
Public
Property
FooterFont()
As
Font
Get
Return
_FooterFont
End
Get
Set
(
ByVal
value
As
Font)
_FooterFont = value
End
Set
End
Property
Public
Overrides
Sub
OnOpenDocument(
ByVal
writer
As
PdfWriter,
ByVal
document
As
Document)
Try
PrintTime = DateTime.Now
bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED)
cb = writer.DirectContent
template = cb.CreateTemplate(50, 50)
template1 = cb.CreateTemplate(50, 50)
Catch
de
As
DocumentException
Catch
ioe
As
System.IO.IOException
End
Try
End
Sub
Public
Overrides
Sub
OnStartPage(
ByVal
writer
As
PdfWriter,
ByVal
document
As
Document)
MyBase
.OnStartPage(writer, document)
Dim
pageSize
As
Rectangle = document.PageSize
Dim
pageN
As
Integer
= writer.PageNumber
Dim
text
As
String
=
"Page "
& pageN &
" of "
Dim
len
As
Single
= bf.GetWidthPoint(text, 8)
' cb.SetRGBColorFill(100, 100, 100)
cb.BeginText()
cb.SetFontAndSize(bf, 8)
cb.SetTextMatrix(pageSize.GetRight(80), pageSize.GetTop(30))
cb.ShowText(text)
cb.EndText()
cb.AddTemplate(template, pageSize.GetRight(80) + len, pageSize.GetTop(30))
If
Title <>
String
.Empty
Then
cb.BeginText()
cb.SetFontAndSize(bf, 10)
'cb.SetRGBColorFill(50, 50, 200)
cb.SetTextMatrix(pageSize.GetLeft(40), pageSize.GetTop(40))
cb.ShowText(Title)
cb.EndText()
End
If
If
HeaderLeft & HeaderRight <>
String
.Empty
Then
Dim
HeaderTable
As
PdfPTable =
New
PdfPTable(2)
HeaderTable.DefaultCell.VerticalAlignment = Element.ALIGN_MIDDLE
HeaderTable.TotalWidth = pageSize.Width - 80
HeaderTable.SetWidthPercentage(
New
Single
() {45, 45}, pageSize)
Dim
HeaderLeftCell
As
PdfPCell =
New
PdfPCell(
New
Phrase(8, HeaderLeft, HeaderFont))
HeaderLeftCell.Padding = 5
HeaderLeftCell.PaddingBottom = 8
HeaderLeftCell.BorderWidthRight = 0
HeaderTable.AddCell(HeaderLeftCell)
Dim
HeaderRightCell
As
PdfPCell =
New
PdfPCell(
New
Phrase(8, HeaderRight, HeaderFont))
HeaderRightCell.HorizontalAlignment = PdfPCell.ALIGN_RIGHT
HeaderRightCell.Padding = 5
HeaderRightCell.PaddingBottom = 8
HeaderRightCell.BorderWidthLeft = 0
HeaderTable.AddCell(HeaderRightCell)
' cb.SetRGBColorFill(0, 0, 0)
HeaderTable.WriteSelectedRows(0, -1, pageSize.GetLeft(40), pageSize.GetTop(50), cb)
End
If
End
Sub
Public
Overrides
Sub
OnEndPage(
ByVal
writer
As
PdfWriter,
ByVal
document
As
Document)
MyBase
.OnEndPage(writer, document)
Dim
pageN
As
Integer
= writer.PageNumber
Dim
text
As
String
=
"Page "
& pageN
Dim
len
As
Single
= bf.GetWidthPoint(text, 8)
Dim
pageSize
As
Rectangle = document.PageSize
' cb.SetRGBColorFill(100, 100, 100)
cb.BeginText()
cb.SetFontAndSize(bf, 8)
'cb.SetRGBColorFill(50, 50, 200)
cb.SetTextMatrix(pageSize.GetLeft(40), pageSize.GetBottom(30))
cb.ShowText(text)
cb.EndText()
cb.AddTemplate(template1, pageSize.GetLeft(40) + len, pageSize.GetBottom(30))
cb.BeginText()
cb.SetFontAndSize(bf, 8)
cb.ShowTextAligned(PdfContentByte.ALIGN_RIGHT,
"Printed On "
& PrintTime, pageSize.GetRight(40), pageSize.GetBottom(30), 0)
cb.EndText()
End
Sub
Public
Overrides
Sub
OnCloseDocument(
ByVal
writer
As
PdfWriter,
ByVal
document
As
Document)
MyBase
.OnCloseDocument(writer, document)
template.BeginText()
template.SetFontAndSize(bf, 8)
template.SetTextMatrix(0, 0)
template.ShowText(
""
& (writer.PageNumber - 1))
template.EndText()
template1.BeginText()
template1.SetFontAndSize(bf, 8)
template1.SetTextMatrix(0, 0)
template1.ShowText(
""
)
template1.EndText()
End
Sub
End
Class
End
Namespace
Reply
Answers (
0
)
What is ASP.NET page Life Cycle?
How to convert Redable data from Nvarchar data