ProductList.vb
'' 完毕:
Imports System.IO
Imports System.Drawing
Imports System.Text
Imports System.Data
Imports System.Linq
Imports System.Collections.Generic
Imports GrapeCity.Documents.Pdf
Imports GrapeCity.Documents.Text
Imports GrapeCity.Documents.Html

'' 此示例展示了如何呈现报告(产品列表)
'' 来自标准 NWind 示例数据库)使用 HTML 字符串
'' 作为模板。通过循环数据记录来创建报告
'' 并从表格行模板构建生成的 HTML,其中填充
'' 实际数据。然后将生成的 HTML 字符串传递给 GcHtmlRenderer
'' 创建 PDF。
'' 
'' 请参阅HelloWorldHtml顶部评论中的注释
'' 有关将 GcDocs.Html 添加到项目的详细信息的示例代码。
Public Class ProductList
    Sub CreatePDF(ByVal stream As Stream)
        Const TTAG = "___TABLE___"

        '' HTML 页面模板:
        Const tableTpl =
            "<!DOCTYPE html>" +
            "<html>" +
            "<head>" +
            "<style>" +
            "" +
            "html * {" +
            "  font-family: 'Trebuchet MS', Arial, Helvetica, sans-serif !important;" +
            "}" +
            "" +
            "h1 {" +
            "  color: #1a5276;" +
            "  background-color: #d2b4de;" +
            "  text-align: center;" +
            "  padding: 6px;" +
            "}" +
            "" +
            "thead {display: table-header-group;}" +
            "" +
            "#products {" +
            "  font-family: 'Trebuchet MS', Arial, Helvetica, sans-serif;" +
            "  border-collapse: collapse;" +
            "  width: 100%;" +
            "}" +
            "" +
            "#products td, #products th {" +
            "  border: 1px solid #ddd;" +
            "  padding: 8px;" +
            "}" +
            "" +
            "#products tr:nth-child(even){background-color: #f2f2f2;}" +
            "" +
            "#products tr:hover {background-color: #ddd;}" +
            "" +
            "#products th {" +
            "  padding-top: 12px;" +
            "  padding-bottom: 12px;" +
            "  text-align: left;" +
            "  background-color: #a569bd;" +
            "  color: white;" +
            "}" +
            "</style>" +
            "</head>" +
            "<body>" +
            "" +
            TTAG +
            "" +
            "</body>" +
            "</html>"

        Const tableHead = "<h1>Product Price List</h1>"

        Const tableFmt =
            "<table id='products'>" +
            "  <thead>" +
            "    <th>Product ID</th>" +
            "    <th>Description</th>" +
            "    <th>Supplier</th>" +
            "    <th>Quantity Per Unit</th>" +
            "    <th>Unit Price</th>" +
            "  </thead>" +
            "{0}" +
            "</table>"

        Const dataRowFmt =
            "  <tr>" +
            "    <td>{0}</td>" +
            "    <td>{1}</td>" +
            "    <td>{2}</td>" +
            "    <td>{3}</td>" +
            "    <td align='right'>{4:C}</td>" +
            "  </tr>"

        Using ds = New DataSet()
            ds.ReadXml(Path.Combine("Resources", "data", "DsNWind.xml"))

            Dim dtProds = ds.Tables("Products")
            Dim dtSupps = ds.Tables("Suppliers")

            Dim products =
            From prod In dtProds.Select()
            Join supp In dtSupps.Select()
            On prod("SupplierID") Equals supp("SupplierID")
            Order By prod("ProductName")
            Select New With {
                .ProductID = prod("ProductID"),
                .ProductName = prod("ProductName"),
                .Supplier = supp("CompanyName"),
                .QuantityPerUnit = prod("QuantityPerUnit"),
                .UnitPrice = prod("UnitPrice")
            }

            Dim sb = New StringBuilder()
            sb.AppendLine(tableHead)
            For Each prod In products
                sb.AppendFormat(dataRowFmt, prod.ProductID, prod.ProductName, prod.Supplier, prod.QuantityPerUnit, prod.UnitPrice)
            Next

            Dim html = tableTpl.Replace(TTAG, String.Format(tableFmt, sb.ToString()))
            Dim tmp = Path.GetTempFileName()
            '' 创建一个用于呈现 HTML 的 GcHtmlBrowser 实例:
            Using browser = Util.NewHtmlBrowser()
                '' PdfOptions 允许您提供 HTML 到 PDF 转换的选项:
                Dim pdfOptions = New PdfOptions() With {
                    .Margins = New PdfMargins(0.2F, 1, 0.2F, 1),
                    .DisplayHeaderFooter = True,
                    .HeaderTemplate = "<div style='color:#1a5276; font-size:12px; width:1000px; margin-left:0.2in; margin-right:0.2in'>" +
                        "<span style='float:left;'>Product Price List</span>" +
                        "<span style='float:right'>Page <span class='pageNumber'></span> of <span class='totalPages'></span></span>" +
                        "</div>",
                    .FooterTemplate = "<div style='color: #1a5276; font-size:12em; width:1000px; margin-left:0.2in; margin-right:0.2in;'>" +
                        "<span>(c) MESCIUS inc. All Rights Reserved.</span>" +
                        "<span style='float:right'>Generated on <span class='date'></span></span></div>"
                }
                '' 将源网页渲染到临时文件:
                Using htmlPage = browser.NewPage(html)
                    htmlPage.SaveAsPdf(tmp, pdfOptions)
                End Using
            End Using
            '' 将创建的 PDF 从临时文件复制到目标流:
            Using ts = File.OpenRead(tmp)
                ts.CopyTo(stream)
            End Using
            '' 清理:
            File.Delete(tmp)
        End Using
        '' 完毕:
    End Sub
End Class