SimpleTable.vb
  1. ''
  2. '' This code is part of Document Solutions for PDF demos.
  3. '' Copyright (c) MESCIUS inc. All rights reserved.
  4. ''
  5. Imports System.IO
  6. Imports System.Drawing
  7. Imports System.Text
  8. Imports System.Data
  9. Imports System.Linq
  10. Imports System.Collections.Generic
  11. Imports GrapeCity.Documents.Pdf
  12. Imports GrapeCity.Documents.Text
  13. Imports GrapeCity.Documents.Html
  14.  
  15. '' This sample shows how to insert an HTML table into a PDF
  16. '' along with other (non-HTML) content.
  17. ''
  18. '' Please see notes in comments at the top of HelloWorldHtml
  19. '' sample code for details on adding DsHtml to your projects.
  20. Public Class SimpleTable
  21. Function CreatePDF(ByVal stream As Stream) As Integer
  22. Const TTAG = "___TABLE___"
  23.  
  24. '' HTML page template:
  25. Const tableTpl =
  26. "<!DOCTYPE html>" +
  27. "<html>" +
  28. "<head>" +
  29. "<style>" +
  30. "html * {" +
  31. " font-family: 'Trebuchet MS', Arial, Helvetica, sans-serif !important;" +
  32. "}" +
  33. "h1 {" +
  34. " color: #fcf3cf;" +
  35. " background-color: #2471a3;" +
  36. " text-align: center;" +
  37. " padding: 6px;" +
  38. " margin-bottom: 0px;" +
  39. "}" +
  40. "table {" +
  41. " border-bottom: 1px solid #ddd;" +
  42. "}" +
  43. "thead {display: table-header-group;}" +
  44. "#employees {" +
  45. " font-family: 'Trebuchet MS', Arial, Helvetica, sans-serif;" +
  46. " border-collapse: collapse;" +
  47. " width: 100%;" +
  48. "}" +
  49. "#employees td, #employees th {" +
  50. " border: 0px solid #ddd;" +
  51. " padding: 8px;" +
  52. "}" +
  53. "#employees tr:nth-child(even){background-color: #d4e6f1;}" +
  54. "#employees tr:hover {background-color: #ddd;}" +
  55. "#employees th {" +
  56. " padding-top: 12px;" +
  57. " padding-bottom: 12px;" +
  58. " text-align: left;" +
  59. " background-color: #2980b9;" +
  60. " color: white;" +
  61. "}" +
  62. "</style>" +
  63. "</head>" +
  64. "<body>" +
  65. TTAG +
  66. "</body>" +
  67. "</html>"
  68.  
  69. Const tableHead = "<h1>Employees</h1>"
  70.  
  71. Const tableFmt =
  72. "<table id='employees'>" +
  73. " <thead>" +
  74. " <th>Name</th>" +
  75. " <th>Address</th>" +
  76. " <th>Country</th>" +
  77. " </thead>" +
  78. "{0}" +
  79. "</table>"
  80.  
  81. Const dataRowFmt =
  82. " <tr>" +
  83. " <td>{0}</td>" +
  84. " <td>{1}</td>" +
  85. " <td>{2}</td>" +
  86. " </tr>"
  87.  
  88. '' Create a new PDF document:
  89. Dim doc = New GcPdfDocument()
  90. '' Add a page:
  91. Dim Page = doc.NewPage()
  92. '' Get page graphics:
  93. Dim g = Page.Graphics
  94.  
  95. Dim nrc = Util.AddNote(
  96. "Here we build an HTML table with data fetched from an XML database, " +
  97. "and insert it into the current PDF page. " +
  98. "A footer is added below the table based on the rendered table size " +
  99. "returned by the GcPdfGraphics.DrawHtml() method.",
  100. Page)
  101.  
  102. '' Get employees data from the sample NorthWind database:
  103. Using ds = New DataSet()
  104. ds.ReadXml(Path.Combine("Resources", "data", "DsNWind.xml"))
  105. Dim dtEmps = ds.Tables("Employees")
  106. Dim emps =
  107. From emp In dtEmps.Select()
  108. Order By emp("LastName")
  109. Select New With
  110. {
  111. .Name = emp("LastName") + ", " + emp("FirstName"),
  112. .Address = emp("Address"),
  113. .Country = emp("Country")
  114. }
  115.  
  116. '' Build the HTML table:
  117. Dim sb = New StringBuilder()
  118. sb.AppendLine(tableHead)
  119. For Each emp In emps
  120. sb.AppendFormat(dataRowFmt, emp.Name, emp.Address, emp.Country)
  121. Next
  122.  
  123. Dim html = tableTpl.Replace(TTAG, String.Format(tableFmt, sb.ToString()))
  124.  
  125. '' Create an instance of GcHtmlBrowser that is used to render HTML:
  126. Using browser = Util.NewHtmlBrowser()
  127. Dim size As SizeF
  128. '' Render HTML.
  129. '' The return value indicates whether anything has been rendered.
  130. '' The output parameter size returns the actual size of the rendered content.
  131. Dim ok = g.DrawHtml(
  132. browser, html, nrc.Left, nrc.Bottom + 36,
  133. New HtmlToPdfFormat(False) With {.MaxPageWidth = nrc.Width / 72},
  134. size)
  135.  
  136. Util.AddNote(
  137. "This text is added below the HTML table. Its position is determined " +
  138. "by the rendered size returned by GcPdfGraphics.DrawHtml().",
  139. Page,
  140. New RectangleF(nrc.Left, nrc.Bottom + size.Height + 72, nrc.Width, Integer.MaxValue))
  141. End Using
  142. End Using
  143. '' Done.
  144. doc.Save(stream)
  145. Return doc.Pages.Count
  146. End Function
  147. End Class
  148.