ProductList.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 render a report (the list of products
  16. '' from the standard NWind sample database) using an HTML string
  17. '' as a template. The report is created by looping over the data records
  18. '' and building up the resulting HTML from table row template filled with
  19. '' the actual data. The generated HTML string is then passed to GcHtmlRenderer
  20. '' to create the PDF.
  21. ''
  22. '' Please see notes in comments at the top of HelloWorldHtml
  23. '' sample code for details on adding DsHtml to your projects.
  24. Public Class ProductList
  25. Sub CreatePDF(ByVal stream As Stream)
  26. Const TTAG = "___TABLE___"
  27.  
  28. '' HTML page template:
  29. Const tableTpl =
  30. "<!DOCTYPE html>" +
  31. "<html>" +
  32. "<head>" +
  33. "<style>" +
  34. "" +
  35. "html * {" +
  36. " font-family: 'Trebuchet MS', Arial, Helvetica, sans-serif !important;" +
  37. "}" +
  38. "" +
  39. "h1 {" +
  40. " color: #1a5276;" +
  41. " background-color: #d2b4de;" +
  42. " text-align: center;" +
  43. " padding: 6px;" +
  44. "}" +
  45. "" +
  46. "thead {display: table-header-group;}" +
  47. "" +
  48. "#products {" +
  49. " font-family: 'Trebuchet MS', Arial, Helvetica, sans-serif;" +
  50. " border-collapse: collapse;" +
  51. " width: 100%;" +
  52. "}" +
  53. "" +
  54. "#products td, #products th {" +
  55. " border: 1px solid #ddd;" +
  56. " padding: 8px;" +
  57. "}" +
  58. "" +
  59. "#products tr:nth-child(even){background-color: #f2f2f2;}" +
  60. "" +
  61. "#products tr:hover {background-color: #ddd;}" +
  62. "" +
  63. "#products th {" +
  64. " padding-top: 12px;" +
  65. " padding-bottom: 12px;" +
  66. " text-align: left;" +
  67. " background-color: #a569bd;" +
  68. " color: white;" +
  69. "}" +
  70. "</style>" +
  71. "</head>" +
  72. "<body>" +
  73. "" +
  74. TTAG +
  75. "" +
  76. "</body>" +
  77. "</html>"
  78.  
  79. Const tableHead = "<h1>Product Price List</h1>"
  80.  
  81. Const tableFmt =
  82. "<table id='products'>" +
  83. " <thead>" +
  84. " <th>Product ID</th>" +
  85. " <th>Description</th>" +
  86. " <th>Supplier</th>" +
  87. " <th>Quantity Per Unit</th>" +
  88. " <th>Unit Price</th>" +
  89. " </thead>" +
  90. "{0}" +
  91. "</table>"
  92.  
  93. Const dataRowFmt =
  94. " <tr>" +
  95. " <td>{0}</td>" +
  96. " <td>{1}</td>" +
  97. " <td>{2}</td>" +
  98. " <td>{3}</td>" +
  99. " <td align='right'>{4:C}</td>" +
  100. " </tr>"
  101.  
  102. Using ds = New DataSet()
  103. ds.ReadXml(Path.Combine("Resources", "data", "DsNWind.xml"))
  104.  
  105. Dim dtProds = ds.Tables("Products")
  106. Dim dtSupps = ds.Tables("Suppliers")
  107.  
  108. Dim products =
  109. From prod In dtProds.Select()
  110. Join supp In dtSupps.Select()
  111. On prod("SupplierID") Equals supp("SupplierID")
  112. Order By prod("ProductName")
  113. Select New With {
  114. .ProductID = prod("ProductID"),
  115. .ProductName = prod("ProductName"),
  116. .Supplier = supp("CompanyName"),
  117. .QuantityPerUnit = prod("QuantityPerUnit"),
  118. .UnitPrice = prod("UnitPrice")
  119. }
  120.  
  121. Dim sb = New StringBuilder()
  122. sb.AppendLine(tableHead)
  123. For Each prod In products
  124. sb.AppendFormat(dataRowFmt, prod.ProductID, prod.ProductName, prod.Supplier, prod.QuantityPerUnit, prod.UnitPrice)
  125. Next
  126.  
  127. Dim html = tableTpl.Replace(TTAG, String.Format(tableFmt, sb.ToString()))
  128. Dim tmp = Path.GetTempFileName()
  129. '' Create an instance of GcHtmlBrowser that is used to render HTML:
  130. Using browser = Util.NewHtmlBrowser()
  131. '' PdfSettings specifies options for HTML to PDF conversion:
  132. Dim pdfOptions = New PdfOptions() With {
  133. .Margins = New PdfMargins(0.2F, 1, 0.2F, 1),
  134. .DisplayHeaderFooter = True,
  135. .HeaderTemplate = "<div style='color:#1a5276; font-size:12px; width:1000px; margin-left:0.2in; margin-right:0.2in'>" +
  136. "<span style='float:left;'>Product Price List</span>" +
  137. "<span style='float:right'>Page <span class='pageNumber'></span> of <span class='totalPages'></span></span>" +
  138. "</div>",
  139. .FooterTemplate = "<div style='color: #1a5276; font-size:12em; width:1000px; margin-left:0.2in; margin-right:0.2in;'>" +
  140. "<span>(c) MESCIUS inc. All Rights Reserved.</span>" +
  141. "<span style='float:right'>Generated on <span class='date'></span></span></div>"
  142. }
  143. '' Render the source Web page to the temporary file:
  144. Using htmlPage = browser.NewPage(html)
  145. htmlPage.SaveAsPdf(tmp, pdfOptions)
  146. End Using
  147. End Using
  148. '' Copy the created PDF from the temp file to target stream:
  149. Using ts = File.OpenRead(tmp)
  150. ts.CopyTo(stream)
  151. End Using
  152. '' Clean up:
  153. File.Delete(tmp)
  154. End Using
  155. '' Done.
  156. End Sub
  157. End Class
  158.