RotatedTableText.cs
// 完毕:
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
{
// This example shows how to draw a table with cells containing rotated texts,
// using the GrapeCity.Documents.Drawing.TableRenderer and related classes.
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", "segoeui.ttf")),
FontSize = 16f,
FontSizeInGraphicUnits = true
};
var cs = new CellStyle
{
LineWidth = 1,
LineColor = Color.Coral,
LinePaddingAll = 1,
CornerRadius = 5,
FillColor = Color.Snow,
PaddingLeftRight = 10,
PaddingTop = 2,
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, "Title 1 with a long text");
ta.AddCell(new CellStyle(cs) { ParagraphAlignment = ParagraphAlignment.Center },
0, 4, 1, 3, "Title 2");
ta.AddCell(cs270, 1, 1, 2, 1, "Vertical Title 1");
ta.AddCell(cs270, 1, 2, 2, 1, "Vertical Title 2 with additional text");
ta.AddCell(cs270, 1, 3, 2, 1, "Vertical Title 3");
ta.AddCell(1, 4, 1, 2, "Subtitle 2.1");
ta.AddCell(cs270FlexH, 2, 4, "Vertical Subtitle 2.1.1");
ta.AddCell(cs270FlexH, 2, 5, "Vertical Subtitle 2.1.2 with a long, long, long, and even longer text");
ta.AddCell(cs270, 3, 0, 3, 1, "Side Title 1");
ta.AddCell(cs270, 6, 0, 2, 1, "Side Title 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, $"row {r} column {c}");
ta.AddCell(new CellStyle(cs)
{
RotationAngle = 90,
ParagraphAlignment = ParagraphAlignment.Far,
TextAlignment = TextAlignment.Leading
},
1, 6, 7, 1, "Other Side");
ta.AddCell(new CellStyle(cs)
{
TextAlignment = TextAlignment.Trailing,
ParagraphAlignment = ParagraphAlignment.Far
},
8, 0, 1, 7, "Bottom Side");
ta.Render();
}
}
}