SimpleTable.vb
- ''
- '' This code is part of Document Solutions for PDF demos.
- '' Copyright (c) MESCIUS 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
-
- '' This sample shows how to insert an HTML table into a PDF
- '' along with other (non-HTML) content.
- ''
- '' Please see notes in comments at the top of HelloWorldHtml
- '' sample code for details on adding DsHtml to your projects.
- Public Class SimpleTable
- Function CreatePDF(ByVal stream As Stream) As Integer
- Const TTAG = "___TABLE___"
-
- '' HTML page template:
- 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>"
-
- '' Create a new PDF document:
- Dim doc = New GcPdfDocument()
- '' Add a page:
- Dim Page = doc.NewPage()
- '' Get page graphics:
- Dim g = Page.Graphics
-
- Dim nrc = Util.AddNote(
- "Here we build an HTML table with data fetched from an XML database, " +
- "and insert it into the current PDF page. " +
- "A footer is added below the table based on the rendered table size " +
- "returned by the GcPdfGraphics.DrawHtml() method.",
- Page)
-
- '' Get employees data from the sample NorthWind database:
- 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")
- }
-
- '' Build the HTML table:
- 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()))
-
- '' Create an instance of GcHtmlBrowser that is used to render HTML:
- Using browser = Util.NewHtmlBrowser()
- Dim size As SizeF
- '' Render HTML.
- '' The return value indicates whether anything has been rendered.
- '' The output parameter size returns the actual size of the rendered content.
- Dim ok = g.DrawHtml(
- browser, html, nrc.Left, nrc.Bottom + 36,
- New HtmlToPdfFormat(False) With {.MaxPageWidth = nrc.Width / 72},
- size)
-
- Util.AddNote(
- "This text is added below the HTML table. Its position is determined " +
- "by the rendered size returned by GcPdfGraphics.DrawHtml().",
- Page,
- New RectangleF(nrc.Left, nrc.Bottom + size.Height + 72, nrc.Width, Integer.MaxValue))
- End Using
- End Using
- '' Done.
- doc.Save(stream)
- Return doc.Pages.Count
- End Function
- End Class
-