MergeRows.cs
// 完毕:
using System;
using System.IO;
using System.Drawing;
using System.Text;
using System.Data;
using System.Linq;
using System.Collections.Generic;
using GrapeCity.Documents.Pdf;
using GrapeCity.Documents.Text;
using GrapeCity.Documents.Html;
namespace DsPdfWeb.Demos
{
// 此示例展示了如何构建和呈现基于表格的报告
// 按第一列分组,该列的单元格具有相同的
// 价值观合并。
// 该示例使用 HTML 中的 JavaScript 代码来实际
// 合并单元格,演示 JavaScript 的使用
// 将 HTML 渲染为 PDF。
// 请注意,示例限制了行数,以便
// 整个表格可以放在一页上。
//
// 请参阅HelloWorldHtml顶部评论中的注释
// 有关将 GcDocs.Html 添加到项目的详细信息的示例代码。
public class MergeRows
{
public void CreatePDF(Stream stream)
{
const string TTAG = "___TABLE___";
// HTML 页面模板:
const string tableTpl =
"<!DOCTYPE html>" +
"<html>" +
"<head>" +
"<style>" +
"html * {" +
" font-family: 'Trebuchet MS', Arial, Helvetica, sans-serif !important;" +
"}" +
"h1 {" +
" color: #1a5276;" +
" background-color: #d2b4de;" +
" text-align: center;" +
" padding: 6px;" +
"}" +
"thead {display: table-header-group;}" +
"#products {" +
" font-family: 'Trebuchet MS', Arial, Helvetica, sans-serif;" +
" border-collapse: collapse;" +
" width: 100%;" +
"}" +
"#products td, #products th {" +
" border: 1px solid #ddd;" +
" padding: 8px;" +
"}" +
"#products tr:hover {background-color: #ddd;}" +
"#products th {" +
" padding-top: 12px;" +
" padding-bottom: 12px;" +
" text-align: left;" +
" background-color: #a569bd;" +
" color: white;" +
"}" +
"</style>" +
"</head>" +
"<body onload='mergeRows()'>" +
// solution from https://stackoverflow.com/questions/56587070/merge-neighbouring-html-table-cells-with-same-value-using-js
"<script>" +
"function mergeRows() {" +
" const table = document.querySelector('table');" +
" let headerCell = null;" +
" for (let row of table.rows)" +
" {" +
" const firstCell = row.cells[0];" +
" if (headerCell === null || firstCell.innerText !== headerCell.innerText)" +
" {" +
" headerCell = firstCell;" +
" }" +
" else" +
" {" +
" headerCell.rowSpan++;" +
" firstCell.remove();" +
" }" +
" }" +
"}" +
"</script>" +
TTAG +
"</body>" +
"</html>";
const string tableHead = "<h1>Products by Suppliers</h1>";
const string tableFmt =
"<table id='products'>" +
" <thead>" +
" <th>Supplier</th>" +
" <th>Description</th>" +
" <th>Quantity Per Unit</th>" +
" <th>Unit Price</th>" +
" </thead>" +
"{0}" +
"</table>";
const string dataRowFmt =
" <tr>" +
" <td>{0}</td>" +
" <td>{1}</td>" +
" <td>{2}</td>" +
" <td align='right'>{3:C}</td>" +
" </tr>";
using var ds = new DataSet();
ds.ReadXml(Path.Combine("Resources", "data", "DsNWind.xml"));
DataTable dtProds = ds.Tables["Products"];
DataTable dtSupps = ds.Tables["Suppliers"];
var products =
(from prod in dtProds.Select()
join supp in dtSupps.Select()
on prod["SupplierID"] equals supp["SupplierID"]
orderby supp["CompanyName"]
select new
{
ProductName = prod["ProductName"],
Supplier = supp["CompanyName"],
QuantityPerUnit = prod["QuantityPerUnit"],
UnitPrice = prod["UnitPrice"]
}).Take(16);
var sb = new StringBuilder();
sb.AppendLine(tableHead);
foreach (var prod in products)
sb.AppendFormat(dataRowFmt, prod.Supplier, prod.ProductName, prod.QuantityPerUnit, prod.UnitPrice);
var html = tableTpl.Replace(TTAG, string.Format(tableFmt, sb.ToString()));
// PdfOptions 允许您提供 HTML 到 PDF 转换的选项:
var pdfOptions = new PdfOptions()
{
Margins = new PdfMargins(0.2f, 1, 0.2f, 1),
DisplayHeaderFooter = true,
HeaderTemplate = "<div style='color:#1a5276; font-size:12px; width:1000px; margin-left:0.2in; margin-right:0.2in'>" +
"<span style='float:left;'>Product Price List</span>" +
"<span style='float:right'>Page <span class='pageNumber'></span> of <span class='totalPages'></span></span>" +
"</div>",
FooterTemplate = "<div style='color: #1a5276; font-size:12em; width:1000px; margin-left:0.2in; margin-right:0.2in;'>" +
"<span>(c) MESCIUS inc. All Rights Reserved.</span>" +
"<span style='float:right'>Generated on <span class='date'></span></span></div>"
};
// 创建一个用于呈现 HTML 的 GcHtmlBrowser 实例:
using var browser = Common.Util.NewHtmlBrowser();
using var htmlPage = browser.NewPage(html);
var tmp = Path.GetTempFileName();
htmlPage.SaveAsPdf(tmp, pdfOptions);
// 将创建的 PDF 从临时文件复制到目标流:
using (var ts = File.OpenRead(tmp))
ts.CopyTo(stream);
// 清理:
File.Delete(tmp);
// 完毕:
}
}
}