PlainTable.cs
//
// このコードは、DioDocs for PDF のサンプルの一部として提供されています。
// © MESCIUS inc. All rights reserved.
//
using System;
using System.IO;
using System.Drawing;
using System.Numerics;
using System.Collections.Generic;
using GrapeCity.Documents.Pdf;
using GrapeCity.Documents.Text;
using GrapeCity.Documents.Common;
using GrapeCity.Documents.Drawing;
using GrapeCity.Documents.Layout;
using GCTEXT = GrapeCity.Documents.Text;
using GCDRAW = GrapeCity.Documents.Drawing;
namespace DsPdfWeb.Demos
{
// このサンプルでは、GrapeCity.Documents.Drawing.TableRenderer クラスなどを使用して、
// 単純なテーブルを描画する方法を紹介しています。
public class PlainTable
{
public int CreatePDF(Stream stream)
{
var doc = new GcPdfDocument();
var p = doc.NewPage();
p.Size = new SizeF(p.Size.Height, p.Size.Width);
var g = p.Graphics;
DrawTable(g, g.CanvasSize.Width, g.CanvasSize.Height);
// PDF を保存します。
doc.Save(stream);
return doc.Pages.Count;
}
class Record
{
public Record(string name, string light, string water, string cold)
{
Name = name;
Light = light;
Water = water;
Cold = cold;
}
public string Name { get; }
public string Light { get; }
public string Water { get; }
public string Cold { get; }
}
static void DrawTable(GcGraphics g, float pageWidth, float pageHeight)
{
var records = new List<Record>
{
new Record("モンステラ", "明るい日陰", "週に1〜2回", "やや弱い"),
new Record("サンスベリア", "明るい場所", "2週間に1回", "強い"),
new Record("ポトス", "日陰でも可", "週に1回", "普通"),
new Record("パキラ", "明るい日陰", "週に1〜2回", "普通"),
new Record("テーブルヤシ", "半日陰", "週に2回", "普通"),
new Record("ガジュマル", "明るい場所", "週に1回", "強い"),
new Record("ドラセナ", "明るい場所", "週に1回", "普通"),
new Record("アグラオネマ", "薄暗い場所", "週に1回", "やや弱い"),
new Record("オリヅルラン", "半日陰", "週に2回", "普通"),
new Record("ゴムの木", "明るい場所", "週に1回", "強い")
};
var host = new LayoutHost();
var view = host.CreateView(pageWidth, pageHeight);
var rt = view.CreateRect();
rt.AnchorTopLeft(null, 36, 36);
var ta = new TableRenderer(g,
rt, FixedTableSides.TopLeft,
rowCount: records.Count + 1,
columnCount: 4,
gridLineColor: Color.DarkGray,
gridLineWidth: 1,
rowMinHeight: 10);
var cs = new CellStyle
{
PaddingTopBottom = 3,
PaddingLeftRight = 4,
FixedWidth = false,
TextFormat = new TextFormat
{
Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "ipag.ttc")),
FontSize = 18,
FontSizeInGraphicUnits = true
}
};
var csHeader = new CellStyle(cs)
{
PaddingRight = 9,
ParagraphAlignment = ParagraphAlignment.Center,
TextAlignment = TextAlignment.Center,
};
var csName = new CellStyle(cs)
{
MaxWidth = pageWidth / 2,
};
var csNumber = new CellStyle(cs)
{
TextAlignment = TextAlignment.Trailing
};
ta.DefaultCellStyle = csHeader;
ta.AddCell(0, 0, "名称");
ta.AddCell(0, 1, "日照条件");
ta.AddCell(0, 2, "水やり頻度");
ta.AddCell(0, 3, "耐寒性");
ta.DefaultCellStyle = csNumber;
for (int i = 0; i < records.Count; i++)
{
var p = records[i];
int r = i + 1;
ta.AddCell(csName, r, 0, p.Name);
ta.AddCell(r, 1, p.Light.ToString());
ta.AddCell(r, 2, p.Water.ToString());
ta.AddCell(r, 3, p.Cold.ToString());
}
ta.ApplyCellConstraints();
ta.Render();
}
}
}