'''' This code is part of Document Solutions for PDF .NET demos.'' Copyright (c) MESCIUS inc. All rights reserved.''ImportsSystem.IOImportsSystem.DrawingImportsSystem.TextImportsSystem.DataImportsSystem.LinqImportsSystem.Collections.GenericImportsGrapeCity.Documents.Pdf '' 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.PublicClassSimpleTableFunctionCreatePDF(ByVal stream AsStream) AsIntegerConstTTAG = "___TABLE___" '' HTML page template:ConsttableTpl ="<!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>" ConsttableHead = "<h1>Employees</h1>" ConsttableFmt ="<table id='employees'>" +" <thead>" +" <th>Name</th>" +" <th>Address</th>" +" <th>Country</th>" +" </thead>" +"{0}" +"</table>" ConstdataRowFmt =" <tr>" +" <td>{0}</td>" +" <td>{1}</td>" +" <td>{2}</td>" +" </tr>" '' Create a new PDF document:Dim doc = NewGcPdfDocument()'' 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 = NewDataSet() ds.ReadXml(Path.Combine("Resources", "data", "DsNWind.xml"))Dim dtEmps = ds.Tables("Employees")Dim emps =From emp In dtEmps.Select()OrderBy emp("LastName")SelectNewWith { .Name = emp("LastName") + ", " + emp("FirstName"), .Address = emp("Address"), .Country = emp("Country") } '' Build the HTML table:Dim sb = NewStringBuilder() sb.AppendLine(tableHead)ForEach 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 AsSizeF'' 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,NewHtmlToPdfFormat(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,NewRectangleF(nrc.Left, nrc.Bottom + size.Height + 72, nrc.Width, Integer.MaxValue))EndUsingEndUsing'' Done. doc.Save(stream)Return doc.Pages.CountEndFunctionEndClass