SimpleTable.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

'' 此示例演示如何将 HTML 表格插入 PDF 中
'' 以及其他(非 HTML)内容。
'' 
'' 请参阅HelloWorldHtml顶部评论中的注释
'' 有关将 GcDocs.Html 添加到项目的详细信息的示例代码。
Public Class SimpleTable
    Function CreatePDF(ByVal stream As Stream) As Integer
        Const TTAG = "___TABLE___"

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

        Const tableHead = "<h1>Employees</h1>"

        Const tableFmt =
            "<table id='employees'>" +
            "  <thead>" +
            "    <th>Name</th>" +
            "    <th>Address</th>" +
            "    <th>Country</th>" +
            "  </thead>" +
            "{0}" +
            "</table>"

        Const dataRowFmt =
            "  <tr>" +
            "    <td>{0}</td>" +
            "    <td>{1}</td>" +
            "    <td>{2}</td>" +
            "  </tr>"

        '' 创建一个新的 PDF 文档:
        Dim doc = New GcPdfDocument()
        '' 添加页面:
        Dim Page = doc.NewPage()
        '' 获取页面图形:
        Dim g = Page.Graphics

        Dim nrc = Util.AddNote(
            "这里我们使用从 XML 数据库获取的数据构建一个 HTML 表," +
            "",
            Page)

        '' 从示例 NorthWind 数据库获取员工数据:
        Using ds = New DataSet()
            ds.ReadXml(Path.Combine("Resources", "data", "DsNWind.xml"))
            Dim dtEmps = ds.Tables("Employees")
            Dim emps =
                From emp In dtEmps.Select()
                Order By emp("LastName")
                Select New With
                    {
                        .Name = emp("LastName") + ", " + emp("FirstName"),
                        .Address = emp("Address"),
                        .Country = emp("Country")
                    }

            '' 构建 HTML 表格:
            Dim sb = New StringBuilder()
            sb.AppendLine(tableHead)
            For Each emp In emps
                sb.AppendFormat(dataRowFmt, emp.Name, emp.Address, emp.Country)
            Next

            Dim html = tableTpl.Replace(TTAG, String.Format(tableFmt, sb.ToString()))

            '' 创建一个用于呈现 HTML 的 GcHtmlBrowser 实例:
            Using browser = Util.NewHtmlBrowser()
                Dim size As SizeF
                '' 渲染 HTML。
                '' 返回值指示是否已渲染任何内容。
                '' 输出参数 size 返回渲染内容的实际大小:
                Dim ok = g.DrawHtml(
                    browser, html, nrc.Left, nrc.Bottom + 36,
                    New HtmlToPdfFormat(False) With {.MaxPageWidth = nrc.Width / 72},
                    size)

                Util.AddNote(
                    "该文本添加到 HTML 表格下方。",
                    Page,
                    New RectangleF(nrc.Left, nrc.Bottom + size.Height + 72, nrc.Width, Integer.MaxValue))
            End Using
        End Using
        '' 完毕:
        doc.Save(stream)
        Return doc.Pages.Count
    End Function
End Class