DataTableView.cs
//
// このコードは、DioDocs for PDF のサンプルの一部として提供されています。
// Copyright (c) GrapeCity inc. All rights reserved.
//
using System;
using System.IO;
using System.Drawing;
using System.Text;
using System.Data;
using GrapeCity.Documents.Pdf;
using GrapeCity.Documents.Text;
using GrapeCity.Documents.Html;
namespace GcPdfWeb.Samples
{
// このサンプルは、HTMLの文字列をレンダリングする方法を示しています。
//
// GcHtmlをプロジェクトに追加する方法の詳細については、 HelloWorldHtml
// サンプルコードのトップにあるコメント欄の注意事項をご覧ください。
public class DataTableView
{
public void CreatePDF(Stream stream)
{
const string TTAG = "___TABLE___";
// HTMLページのテンプレート
var tableTpl =
"<!DOCTYPE html>" +
"<html>" +
"<head>" +
"<style>" +
"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:nth-child(even){background-color: #f2f2f2;}" +
"#products tr:hover {background-color: #ddd;}" +
"#products th {" +
" padding-top: 12px;" +
" padding-bottom: 12px;" +
" text-align: left;" +
" background-color: #3377ff;" +
" color: white;" +
"}" +
"</style>" +
"</head>" +
"<body>" +
TTAG +
"</body>" +
"</html>";
var tableFmt =
"<table id='products'>" +
" <thead>" +
" <th>商品 ID</th>" +
" <th>商品名</th>" +
" <th>仕入れ先 ID</th>" +
" <th>数量</th>" +
" <th>単価</th>" +
" </thead>" +
"{0}" +
"</table>";
var dataRowFmt =
" <tr>" +
" <td>{0}</td>" +
" <td>{1}</td>" +
" <td>{2}</td>" +
" <td>{3}</td>" +
" <td align='right'>{4:C}</td>" +
" </tr>";
using (var ds = new DataSet())
{
ds.ReadXml(Path.Combine("Resources", "data", "GcNWind.xml"));
DataTable dtProds = ds.Tables["Products"];
DataRowCollection prods = dtProds.Rows;
var sb = new StringBuilder();
for (int i = 0; i < prods.Count; ++i)
{
var prod = prods[i];
// "ProductID" "ProductName" (Description) "SupplierID" (Supplier) "QuantityPerUnit" "UnitPrice"
sb.AppendFormat(dataRowFmt, prod["ProductID"], prod["ProductName"], prod["SupplierID"], prod["QuantityPerUnit"], prod["UnitPrice"]);
}
var html = tableTpl.Replace(TTAG, string.Format(tableFmt, sb.ToString()));
var tmp = Path.GetTempFileName();
using (var re = new GcHtmlRenderer(html))
{
// PdfSettingsは、HTMLをPDFに変換するためのオプションを提供することができます。
var pdfSettings = new PdfSettings()
{
// このデモのみに使用されるデフォルト
PageWidth = 8.5f,
// このデモのみに使用されるデフォルト
PageHeight = 11f,
// ページの余白 (デフォルトは余白なし)
Margins = new Margins(0.2f, 1, 0.2f, 1),
// 必要に応じてページを印刷するようにしてください。
IgnoreCSSPageSize = true,
// ランドスケープを使用して長いコード行が収まるようにします。
// Landscape = true,
// 必要に応じて、スケーリングをデフォルトの1から変更することができます。
// Scale = 0.8f,
// ヘッダー/フッターを有効にします。
DisplayHeaderFooter = true,
HeaderTemplate = "<div style='font-size:20px;width:1000px;margin-left:0.2in;margin-right:0.2in'>" +
"<span style='float:left;font-size:'>Product Price List</span>" +
"<span style='float:right'>Page <span class='pageNumber'></span> of <span class='totalPages'></span>" +
"</div>",
FooterTemplate = "<div style='font-size:12em;width:1000px;margin-left:0.2in;margin-right:0.2in;'>" +
"<span style='background-color:lightblue;'>(c) GrapeCity, Inc. All Rights Reserved. <span class='date'></span></div>"
};
// 元となるWebページを一時ファイルにレンダリングします。
re.RenderToPdf(tmp, pdfSettings);
}
// 作成したPDFを一時ファイルから対象のストリームにコピーします。
using (var ts = File.OpenRead(tmp))
ts.CopyTo(stream);
// 一時ファイルを削除
File.Delete(tmp);
}
// PDF ドキュメントを保存します。
}
}
}