SimpleTable.cs
  1. //
  2. // This code is part of Document Solutions for PDF demos.
  3. // Copyright (c) MESCIUS inc. All rights reserved.
  4. //
  5. using System;
  6. using System.IO;
  7. using System.Drawing;
  8. using System.Text;
  9. using System.Data;
  10. using System.Linq;
  11. using System.Collections.Generic;
  12. using GrapeCity.Documents.Pdf;
  13. using GrapeCity.Documents.Text;
  14. using GrapeCity.Documents.Html;
  15.  
  16. namespace DsPdfWeb.Demos
  17. {
  18. // This sample shows how to insert an HTML table into a PDF
  19. // along with other (non-HTML) content.
  20. //
  21. // Please see notes in comments at the top of HelloWorldHtml
  22. // sample code for details on adding DsHtml to your projects.
  23. public class SimpleTable
  24. {
  25. public int CreatePDF(Stream stream)
  26. {
  27. const string TTAG = "___TABLE___";
  28.  
  29. // HTML page template:
  30. const string tableTpl =
  31. "<!DOCTYPE html>" +
  32. "<html>" +
  33. "<head>" +
  34. "<style>" +
  35.  
  36. "html * {" +
  37. " font-family: 'Trebuchet MS', Arial, Helvetica, sans-serif !important;" +
  38. "}" +
  39.  
  40. "h1 {" +
  41. " color: #fcf3cf;" +
  42. " background-color: #2471a3;" +
  43. " text-align: center;" +
  44. " padding: 6px;" +
  45. " margin-bottom: 0px;" +
  46. "}" +
  47.  
  48. "table {" +
  49. " border-bottom: 1px solid #ddd;" +
  50. "}" +
  51.  
  52. "thead {display: table-header-group;}" +
  53.  
  54. "#employees {" +
  55. " font-family: 'Trebuchet MS', Arial, Helvetica, sans-serif;" +
  56. " border-collapse: collapse;" +
  57. " width: 100%;" +
  58. "}" +
  59.  
  60. "#employees td, #employees th {" +
  61. " border: 0px solid #ddd;" +
  62. " padding: 8px;" +
  63. "}" +
  64.  
  65. "#employees tr:nth-child(even){background-color: #d4e6f1;}" +
  66.  
  67. "#employees tr:hover {background-color: #ddd;}" +
  68.  
  69. "#employees th {" +
  70. " padding-top: 12px;" +
  71. " padding-bottom: 12px;" +
  72. " text-align: left;" +
  73. " background-color: #2980b9;" +
  74. " color: white;" +
  75. "}" +
  76. "</style>" +
  77. "</head>" +
  78. "<body>" +
  79.  
  80. TTAG +
  81.  
  82. "</body>" +
  83. "</html>";
  84.  
  85. const string tableHead = "<h1>Employees</h1>";
  86.  
  87. const string tableFmt =
  88. "<table id='employees'>" +
  89. " <thead>" +
  90. " <th>Name</th>" +
  91. " <th>Address</th>" +
  92. " <th>Country</th>" +
  93. " </thead>" +
  94. "{0}" +
  95. "</table>";
  96.  
  97. const string dataRowFmt =
  98. " <tr>" +
  99. " <td>{0}</td>" +
  100. " <td>{1}</td>" +
  101. " <td>{2}</td>" +
  102. " </tr>";
  103.  
  104. // Create a new PDF document:
  105. var doc = new GcPdfDocument();
  106. // Add a page:
  107. var page = doc.NewPage();
  108. // Get page graphics:
  109. var g = page.Graphics;
  110.  
  111. var nrc = Common.Util.AddNote(
  112. "Here we build an HTML table with data fetched from an XML database, " +
  113. "and insert it into the current PDF page. " +
  114. "A footer is added below the table based on the rendered table size " +
  115. "returned by the GcPdfGraphics.DrawHtml() method.",
  116. page);
  117.  
  118. // Get employees data from the sample NorthWind database:
  119. using (var ds = new DataSet())
  120. {
  121. ds.ReadXml(Path.Combine("Resources", "data", "DsNWind.xml"));
  122. DataTable dtEmps = ds.Tables["Employees"];
  123. var emps =
  124. from emp in dtEmps.Select()
  125. orderby emp["LastName"]
  126. select new
  127. {
  128. Name = emp["LastName"] + ", " + emp["FirstName"],
  129. Address = emp["Address"],
  130. Country = emp["Country"]
  131. };
  132.  
  133. // Build the HTML table:
  134. var sb = new StringBuilder();
  135. sb.AppendLine(tableHead);
  136. foreach (var emp in emps)
  137. sb.AppendFormat(dataRowFmt, emp.Name, emp.Address, emp.Country);
  138.  
  139. var html = tableTpl.Replace(TTAG, string.Format(tableFmt, sb.ToString()));
  140.  
  141. // Create an instance of GcHtmlBrowser that is used to render HTML:
  142. using var browser = Common.Util.NewHtmlBrowser();
  143.  
  144. // Render HTML.
  145. // The return value indicates whether anything has been rendered.
  146. // The output parameter size returns the actual size of the rendered content:
  147. var ok = g.DrawHtml(browser, html, nrc.Left, nrc.Bottom + 36,
  148. new HtmlToPdfFormat(false) { MaxPageWidth = nrc.Width / 72 },
  149. out SizeF size);
  150.  
  151. Common.Util.AddNote(
  152. "This text is added below the HTML table. Its position is determined " +
  153. "by the rendered size returned by GcPdfGraphics.DrawHtml().",
  154. page,
  155. new RectangleF(nrc.Left, nrc.Bottom + size.Height + 72, nrc.Width, int.MaxValue));
  156. }
  157. // Done.
  158. doc.Save(stream);
  159. return doc.Pages.Count;
  160. }
  161. }
  162. }
  163.