SimpleTable.vb
'' 
'' このコードは、DioDocs for PDF のサンプルの一部として提供されています。
'' Copyright (c) GrapeCity inc. All rights reserved.
'' 
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 テーブルを他の(非 HTML)コンテンツとともに PDF に
'' 挿入する方法を示します。
'' 
'' GcHtml プロジェクトに追加する方法の詳細については、HelloWorldHtml
'' サンプルコードの上部にあるコメントのメモを参照してください。
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>従業員</h1>"

        Const tableFmt =
            "<table id='employees'>" +
            "  <thead>" +
            "    <th>氏名</th>" +
            "    <th>住所</th>" +
            "    <th>地域</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 テーブルを" +
            "作成し、現在の PDF ページに挿入します。" +
            "GcPdfGraphics.DrawHtml() メソッドによって返されるレンダリングされた" +
            "テーブルサイズに基づいて、フッターがテーブルの下に追加されます。",
            Page)

        '' サンプル NorthWind データベースから従業員データを取得します。
        Using ds = New DataSet()
            ds.ReadXml(Path.Combine("Resources", "data", "GcNWind.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 をレンダリングします。
                '' 戻り値は、何かがレンダリングされたかどうかを示します。
                '' 出力パラメーターのサイズは、レンダリングされたコンテンツの実際のサイズを返します。
                Dim ok = g.DrawHtml(
                    browser, html, nrc.Left, nrc.Bottom + 36,
                    New HtmlToPdfFormat(False) With {.MaxPageWidth = nrc.Width / 72},
                    size)

                Util.AddNote(
                    "このテキストは、HTML テーブルの下に追加されます。 その位置は、" +
                    "GcPdfGraphics.DrawHtml() によって返されるレンダリングサイズによって決まります。",
                    Page,
                    New RectangleF(nrc.Left, nrc.Bottom + size.Height + 72, nrc.Width, Integer.MaxValue))
            End Using
        End Using
        '' PDF ドキュメントを保存します。
        doc.Save(stream)
        Return doc.Pages.Count
    End Function
End Class