//// This code is part of Document Solutions for PDF .NET demos.// Copyright (c) MESCIUS inc. All rights reserved.//usingSystem.IO;usingSystem.Drawing;usingSystem.Text;usingSystem.Data;usingSystem.Linq;usingGrapeCity.Documents.Pdf; namespaceDsPdfWeb.Demos{// 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.publicclassSimpleTable {publicintCreatePDF(Stream stream) {conststringTTAG = "___TABLE___"; // HTML page template:conststringtableTpl ="<!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>"; conststringtableHead = "<h1>Employees</h1>"; conststringtableFmt ="<table id='employees'>" +" <thead>" +" <th>Name</th>" +" <th>Address</th>" +" <th>Country</th>" +" </thead>" +"{0}" +"</table>"; conststringdataRowFmt =" <tr>" +" <td>{0}</td>" +" <td>{1}</td>" +" <td>{2}</td>" +" </tr>"; // Create a new PDF document:var doc = newGcPdfDocument();// Add a page:var page = doc.NewPage();// Get page graphics:var g = page.Graphics; var nrc = Common.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 (var ds = newDataSet()) { ds.ReadXml(Path.Combine("Resources", "data", "DsNWind.xml"));DataTable dtEmps = ds.Tables["Employees"];var emps =from emp in dtEmps.Select()orderby emp["LastName"]selectnew {Name = emp["LastName"] + ", " + emp["FirstName"],Address = emp["Address"],Country = emp["Country"] }; // Build the HTML table:var sb = newStringBuilder(); 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())); // Create an instance of GcHtmlBrowser that is used to render HTML:usingvar browser = Common.Util.NewHtmlBrowser(); // Render HTML.// The return value indicates whether anything has been rendered.// The output parameter size returns the actual size of the rendered content:var ok = g.DrawHtml(browser, html, nrc.Left, nrc.Bottom + 36,newHtmlToPdfFormat(false) { MaxPageWidth = nrc.Width / 72 },outSizeF size); Common.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, int.MaxValue)); }// Done. doc.Save(stream);return doc.Pages.Count; } }}