Dipak V Nakum

Dipak V Nakum

  • NA
  • 39
  • 3k

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.
  1. Response.ContentType = "application/pdf"  
  2. Response.AddHeader("content-disposition""attachment;filename=eVMS_Astral " & Now & ".pdf")  
  3. Response.Cache.SetCacheability(HttpCacheability.NoCache)  
  4. Dim sw As StringWriter = New StringWriter()  
  5. Dim hw As HtmlTextWriter = New HtmlTextWriter(sw)  
  6. Dim gv As GridView = New GridView()  
  7. gv.HeaderStyle.Font.Bold = True  
  8. gv.HeaderStyle.Font.Name = "Arial"  
  9. gv.HeaderStyle.Font.Size = 7  
  10. gv.HeaderStyle.ForeColor = SystemColors.ActiveCaption  
  11. gv.HeaderStyle.BackColor = System.Drawing.Color.FromName("#144282")  
  12. gv.RowStyle.Font.Name = "Arial"  
  13. gv.RowStyle.Font.Size = 7  
  14. Dim ds As Data.DataSet = GetData()  
  15. 'ds.Tables(0).Columns.RemoveAt(0)  
  16. gv.DataSource = ds  
  17. gv.AllowPaging = False  
  18. gv.AllowSorting = False  
  19. gv.GridLines = GridLines.None  
  20. gv.HeaderStyle.ForeColor = System.Drawing.Color.FromName("#144282")  
  21. gv.DataBind()  
  22. gv.RenderControl(hw)  
  23. Dim sr As StringReader = New StringReader(sw.ToString())  
  24. Dim pdfDoc As Document = New Document(PageSize.A4, 15.0F, 15.0F, 45.0F, 45.0F)  
  25. Dim htmlparser As HTMLWorker = New HTMLWorker(pdfDoc)  
  26. Dim pdfWriter As PdfWriter = pdfWriter.GetInstance(pdfDoc, Response.OutputStream)  
  27. 'Dim page As PdfPage = New PdfPage()  
  28. Dim page As myApp.pdfPage = New myApp.pdfPage()  
  29. pdfWriter.PageEvent = page  
  30. page.Title = "Getrag Transmissions India Pvt Ltd - " & Session("ReportTitle") & Space(60) & "Printed By: " & Session("UserName")  
  31. pdfDoc.AddCreationDate()  
  32. pdfDoc.Open()  
  33. htmlparser.Parse(sr)  
  34. pdfDoc.Close()  
  35. Response.Write(pdfDoc)  
  36. Response.End()  
  37. Imports Microsoft.VisualBasic  
  38. Imports System  
  39. Imports iTextSharp.text  
  40. Imports iTextSharp.text.pdf  
  41. Namespace myApp  
  42. Public Class pdfPage  
  43. Inherits iTextSharp.text.pdf.PdfPageEventHelper  
  44. Private cb As PdfContentByte  
  45. Private template As PdfTemplate  
  46. Private template1 As PdfTemplate  
  47. Private bf As BaseFont = Nothing  
  48. Private PrintTime As DateTime = DateTime.Now  
  49. Private _Title As String  
  50. Public Property Title() As String  
  51. Get  
  52. Return _Title  
  53. End Get  
  54. Set(ByVal value As String)  
  55. _Title = value  
  56. End Set  
  57. End Property  
  58. Private _HeaderLeft As String  
  59. Public Property HeaderLeft() As String  
  60. Get  
  61. Return _HeaderLeft  
  62. End Get  
  63. Set(ByVal value As String)  
  64. _HeaderLeft = value  
  65. End Set  
  66. End Property  
  67. Private _HeaderRight As String  
  68. Public Property HeaderRight() As String  
  69. Get  
  70. Return _HeaderRight  
  71. End Get  
  72. Set(ByVal value As String)  
  73. _HeaderRight = value  
  74. End Set  
  75. End Property  
  76. Private _HeaderFont As Font  
  77. Public Property HeaderFont() As Font  
  78. Get  
  79. Return _HeaderFont  
  80. End Get  
  81. Set(ByVal value As Font)  
  82. _HeaderFont = value  
  83. End Set  
  84. End Property  
  85. Private _FooterFont As Font  
  86. Public Property FooterFont() As Font  
  87. Get  
  88. Return _FooterFont  
  89. End Get  
  90. Set(ByVal value As Font)  
  91. _FooterFont = value  
  92. End Set  
  93. End Property  
  94. Public Overrides Sub OnOpenDocument(ByVal writer As PdfWriter, ByVal document As Document)  
  95. Try  
  96. PrintTime = DateTime.Now  
  97. bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED)  
  98. cb = writer.DirectContent  
  99. template = cb.CreateTemplate(50, 50)  
  100. template1 = cb.CreateTemplate(50, 50)  
  101. Catch de As DocumentException  
  102. Catch ioe As System.IO.IOException  
  103. End Try  
  104. End Sub  
  105. Public Overrides Sub OnStartPage(ByVal writer As PdfWriter, ByVal document As Document)  
  106. MyBase.OnStartPage(writer, document)  
  107. Dim pageSize As Rectangle = document.PageSize  
  108. Dim pageN As Integer = writer.PageNumber  
  109. Dim text As String = "Page " & pageN & " of "  
  110. Dim len As Single = bf.GetWidthPoint(text, 8)  
  111. ' cb.SetRGBColorFill(100, 100, 100)  
  112. cb.BeginText()  
  113. cb.SetFontAndSize(bf, 8)  
  114. cb.SetTextMatrix(pageSize.GetRight(80), pageSize.GetTop(30))  
  115. cb.ShowText(text)  
  116. cb.EndText()  
  117. cb.AddTemplate(template, pageSize.GetRight(80) + len, pageSize.GetTop(30))  
  118. If Title <> String.Empty Then  
  119. cb.BeginText()  
  120. cb.SetFontAndSize(bf, 10)  
  121. 'cb.SetRGBColorFill(50, 50, 200)  
  122. cb.SetTextMatrix(pageSize.GetLeft(40), pageSize.GetTop(40))  
  123. cb.ShowText(Title)  
  124. cb.EndText()  
  125. End If  
  126. If HeaderLeft & HeaderRight <> String.Empty Then  
  127. Dim HeaderTable As PdfPTable = New PdfPTable(2)  
  128. HeaderTable.DefaultCell.VerticalAlignment = Element.ALIGN_MIDDLE  
  129. HeaderTable.TotalWidth = pageSize.Width - 80  
  130. HeaderTable.SetWidthPercentage(New Single() {45, 45}, pageSize)  
  131. Dim HeaderLeftCell As PdfPCell = New PdfPCell(New Phrase(8, HeaderLeft, HeaderFont))  
  132. HeaderLeftCell.Padding = 5  
  133. HeaderLeftCell.PaddingBottom = 8  
  134. HeaderLeftCell.BorderWidthRight = 0  
  135. HeaderTable.AddCell(HeaderLeftCell)  
  136. Dim HeaderRightCell As PdfPCell = New PdfPCell(New Phrase(8, HeaderRight, HeaderFont))  
  137. HeaderRightCell.HorizontalAlignment = PdfPCell.ALIGN_RIGHT  
  138. HeaderRightCell.Padding = 5  
  139. HeaderRightCell.PaddingBottom = 8  
  140. HeaderRightCell.BorderWidthLeft = 0  
  141. HeaderTable.AddCell(HeaderRightCell)  
  142. ' cb.SetRGBColorFill(0, 0, 0)  
  143. HeaderTable.WriteSelectedRows(0, -1, pageSize.GetLeft(40), pageSize.GetTop(50), cb)  
  144. End If  
  145. End Sub  
  146. Public Overrides Sub OnEndPage(ByVal writer As PdfWriter, ByVal document As Document)  
  147. MyBase.OnEndPage(writer, document)  
  148. Dim pageN As Integer = writer.PageNumber  
  149. Dim text As String = "Page " & pageN  
  150. Dim len As Single = bf.GetWidthPoint(text, 8)  
  151. Dim pageSize As Rectangle = document.PageSize  
  152. ' cb.SetRGBColorFill(100, 100, 100)  
  153. cb.BeginText()  
  154. cb.SetFontAndSize(bf, 8)  
  155. 'cb.SetRGBColorFill(50, 50, 200)  
  156. cb.SetTextMatrix(pageSize.GetLeft(40), pageSize.GetBottom(30))  
  157. cb.ShowText(text)  
  158. cb.EndText()  
  159. cb.AddTemplate(template1, pageSize.GetLeft(40) + len, pageSize.GetBottom(30))  
  160. cb.BeginText()  
  161. cb.SetFontAndSize(bf, 8)  
  162. cb.ShowTextAligned(PdfContentByte.ALIGN_RIGHT, "Printed On " & PrintTime, pageSize.GetRight(40), pageSize.GetBottom(30), 0)  
  163. cb.EndText()  
  164. End Sub  
  165. Public Overrides Sub OnCloseDocument(ByVal writer As PdfWriter, ByVal document As Document)  
  166. MyBase.OnCloseDocument(writer, document)  
  167. template.BeginText()  
  168. template.SetFontAndSize(bf, 8)  
  169. template.SetTextMatrix(0, 0)  
  170. template.ShowText("" & (writer.PageNumber - 1))  
  171. template.EndText()  
  172. template1.BeginText()  
  173. template1.SetFontAndSize(bf, 8)  
  174. template1.SetTextMatrix(0, 0)  
  175. template1.ShowText("")  
  176. template1.EndText()  
  177. End Sub  
  178. End Class  
  179. End Namespace