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, int take1, int take2, int take3)
{
Name = name;
Take1 = take1;
Take2 = take2;
Take3 = take3;
}
public string Name { get; }
public int Take1 { get; }
public int Take2 { get; }
public int Take3 { get; }
}
static void DrawTable(GcGraphics g, float pageWidth, float pageHeight)
{
var rnd = Common.Util.NewRandom();
var records = new List<Record>();
for (int i = 0; i < 10; ++i)
records.Add(new Record(Common.Util.LoremIpsum(1, 1, 1, 1, 4), rnd.Next(100), rnd.Next(100), rnd.Next(100)));
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, "試験 1");
ta.AddCell(0, 2, "試験 2");
ta.AddCell(0, 3, "試験 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.Take1.ToString());
ta.AddCell(r, 2, p.Take2.ToString());
ta.AddCell(r, 3, p.Take3.ToString());
}
ta.ApplyCellConstraints();
ta.Render();
}
}
}