RotatedTableText.cs
//
// このコードは、DioDocs for PDF のサンプルの一部として提供されています。
// © MESCIUS inc. All rights reserved.
//
using System;
using System.IO;
using System.Drawing;
using System.Numerics;
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 RotatedTableText
{
public int CreatePDF(Stream stream)
{
var doc = new GcPdfDocument();
var p = doc.Pages.Add(new SizeF(doc.PageSize.Height, doc.PageSize.Width));
var g = p.Graphics;
DrawTable(g, g.CanvasSize.Width, g.CanvasSize.Height);
// PDF を保存します。
doc.Save(stream);
return doc.Pages.Count;
}
static void DrawTable(GcGraphics g, float pageWidth, float pageHeight)
{
var host = new LayoutHost();
var view = host.CreateView(pageWidth, pageHeight);
var rt = view.CreateRect();
rt.AnchorTopLeftRight(null, 30, 20, 20);
var ta = new TableRenderer(g,
rt, FixedTableSides.TopLeftRight,
rowCount: 9, columnCount: 7,
gridLineColor: Color.Transparent,
gridLineWidth: 1,
rowMinHeight: 20,
columnMinWidth: 20);
ta.ColumnRects[6].SetStarWidth(1f);
ta.RowRects[8].AppendMinHeight(70);
var fmtNorm = new TextFormat
{
Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "ipag.ttc")),
FontSize = 16f,
FontSizeInGraphicUnits = true
};
var cs = new CellStyle
{
LineWidth = 1,
LineColor = Color.Coral,
LinePaddingAll = 1,
CornerRadius = 5,
FillColor = Color.Snow,
PaddingLeftRight = 10,
PaddingTop = 5,
PaddingBottom = 5,
TextAlignment = TextAlignment.Center,
TextFormat = fmtNorm
};
ta.DefaultCellStyle = cs;
ta.TableFrameStyle = new FrameStyle
{
LineWidth = 1,
LineColor = Color.CornflowerBlue,
LinePaddingAll = -3,
CornerRadius = 5,
FillColor = Color.MistyRose
};
var csFlexW = new CellStyle(cs)
{
FixedWidth = false
};
var cs270 = new CellStyle(cs)
{
RotationAngle = 270
};
var cs270FlexH = new CellStyle(cs)
{
RotationAngle = 270,
ParagraphAlignment = ParagraphAlignment.Center,
FixedWidth = false,
MaxWidth = 120
};
ta.AddCell(0, 1, 1, 3, "長文付きタイトル 1");
ta.AddCell(new CellStyle(cs) { ParagraphAlignment = ParagraphAlignment.Center },
0, 4, 1, 3, "タイトル 2");
ta.AddCell(cs270, 1, 1, 2, 1, "縦のタイトル 1");
ta.AddCell(cs270, 1, 2, 2, 1, "追加テキスト付き縦のタイトル 2");
ta.AddCell(cs270, 1, 3, 2, 1, "縦のタイトル 3");
ta.AddCell(1, 4, 1, 2, "サブタイトル 2.1");
ta.AddCell(cs270FlexH, 2, 4, "縦のサブタイトル 2.1.1");
ta.AddCell(cs270FlexH, 2, 5, "長い長い長いテキストを含む縦のサブタイトル 2.1.2");
ta.AddCell(cs270, 3, 0, 3, 1, "サイドタイトル 1");
ta.AddCell(cs270, 6, 0, 2, 1, "サイドタイトル 2");
for (int r = 3; r < 8; r++)
for (int c = 1; c < 4; c++)
ta.AddCell(csFlexW, r, c, (r * c).ToString());
for (int r = 3; r < 8; r++)
for (int c = 4; c < 6; c++)
ta.AddCell(csFlexW, r, c, $"行 {r} 列 {c}");
ta.AddCell(new CellStyle(cs)
{
RotationAngle = 90,
ParagraphAlignment = ParagraphAlignment.Far,
TextAlignment = TextAlignment.Leading
},
1, 6, 7, 1, "別の向き");
ta.AddCell(new CellStyle(cs)
{
TextAlignment = TextAlignment.Trailing,
ParagraphAlignment = ParagraphAlignment.Far
},
8, 0, 1, 7, "右下詰め");
ta.Render();
}
}
}