SimpleTable.cs
// 完毕:
using System;
using System.IO;
using System.Drawing;
using System.Text;
using System.Data;
using System.Linq;
using System.Collections.Generic;
using GrapeCity.Documents.Pdf;
using GrapeCity.Documents.Text;
using GrapeCity.Documents.Html;

namespace DsPdfWeb.Demos
{
    // 此示例演示如何将 HTML 表格插入 PDF 中
    // 以及其他(非 HTML)内容。
    // 
    // 请参阅HelloWorldHtml顶部评论中的注释
    // 有关将 GcDocs.Html 添加到项目的详细信息的示例代码。
    public class SimpleTable
    {
        public int CreatePDF(Stream stream)
        {
            const string TTAG = "___TABLE___";

            // HTML 页面模板:
            const string 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 string tableHead = "<h1>Employees</h1>";

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

            const string dataRowFmt =
                "  <tr>" +
                "    <td>{0}</td>" +
                "    <td>{1}</td>" +
                "    <td>{2}</td>" +
                "  </tr>";

            // 创建一个新的 PDF 文档:
            var doc = new GcPdfDocument();
            // 添加页面:
            var page = doc.NewPage();
            // 获取页面图形:
            var g = page.Graphics;

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

            // 从示例 NorthWind 数据库获取员工数据:
            using (var ds = new DataSet())
            {
                ds.ReadXml(Path.Combine("Resources", "data", "DsNWind.xml"));
                DataTable dtEmps = ds.Tables["Employees"];
                var emps =
                    from emp in dtEmps.Select()
                    orderby emp["LastName"]
                    select new
                    {
                        Name = emp["LastName"] + ", " + emp["FirstName"],
                        Address = emp["Address"],
                        Country = emp["Country"]
                    };

                // 构建 HTML 表格:
                var sb = new StringBuilder();
                sb.AppendLine(tableHead);
                foreach (var emp in emps)
                    sb.AppendFormat(dataRowFmt, emp.Name, emp.Address, emp.Country);

                var html = tableTpl.Replace(TTAG, string.Format(tableFmt, sb.ToString()));

                // 创建一个用于呈现 HTML 的 GcHtmlBrowser 实例:
                using var browser = Common.Util.NewHtmlBrowser();

                // 渲染 HTML。
                // 返回值指示是否已渲染任何内容。
                // 输出参数 size 返回渲染内容的实际大小:
                var ok = g.DrawHtml(browser, html, nrc.Left, nrc.Bottom + 36,
                    new HtmlToPdfFormat(false) { MaxPageWidth = nrc.Width / 72 },
                    out SizeF size);

                Common.Util.AddNote(
                    "该文本添加到 HTML 表格下方。",
                    page,
                    new RectangleF(nrc.Left, nrc.Bottom + size.Height + 72, nrc.Width, int.MaxValue));
            }
            // 完毕:
            doc.Save(stream);
            return doc.Pages.Count;
        }
    }
}