TableTextAlign.cs
//// This code is part of Document Solutions for PDF .NET demos.// Copyright (c) MESCIUS inc. All rights reserved.//using System.IO;using System.Drawing;using GrapeCity.Documents.Pdf;using GrapeCity.Documents.Text;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 different // alignments of texts and paragraphs in table cells, // using the GrapeCity.Documents.Drawing.TableRenderer and related classes. public class TableTextAlign { public int CreatePDF(Stream stream) { var doc = new GcPdfDocument(); var g = doc.NewPage().Graphics;
DrawTable(g, g.CanvasSize.Width, g.CanvasSize.Height);
// Save the 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.AnchorTopLeft(null, 30, 20);
var ta = new TableRenderer(g, rt, FixedTableSides.TopLeft, rowCount: 4, columnCount: 3, gridLineColor: Color.Black, gridLineWidth: 1, rowMinHeight: 28);
var columns = ta.ColumnRects; columns[0].SetWidth(150); columns[1].SetWidth(200); columns[2].SetWidth(200);
var fmtNorm = new TextFormat { Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "NotoSerif-Regular.ttf")), FontSize = 25, FontSizeInGraphicUnits = true, FontFeatures = new FontFeature[] { new FontFeature(FeatureTag.dlig) } }; var fmtOrange = new TextFormat(fmtNorm) { ForeColor = Color.Orange };
var cs = new CellStyle { PaddingLeftRight = 15, PaddingBottom = 3, TextAlignment = TextAlignment.Center, TextFormat = fmtNorm, CreateTextLayout = (g, cs, data) => { var tl = g.CreateTextLayout(); tl.TextExtensionStrategy = TextExtensionStrategy.Excel; tl.Append((string)data, cs.TextFormat); return tl; } }; ta.DefaultCellStyle = cs;
ta.AddCell(0, 0, "Column 1"); ta.AddCell(0, 1, "Column 2"); ta.AddCell(0, 2, "Column 3");
ta.AddCell(new CellStyle(cs) { ParagraphAlignment = ParagraphAlignment.Far, FillColor = Color.LemonChiffon }, 1, 0, "One-liner."); ta.AddCell(new CellStyle(cs) { ParagraphAlignment = ParagraphAlignment.Center }, 1, 1, "Multi-line and centered text."); ta.AddCell(new CellStyle(cs) { TextAlignment = TextAlignment.Distributed }, 1, 2, "A multi-line piece of text that is distributed within the table cell.");
ta.AddCell(2, 0, "Apple"); ta.AddCell(2, 1, "Banana"); ta.AddCell(new CellStyle(cs) { TextFormat = fmtOrange }, 2, 2, "Orange"); ta.AddCell(3, 0, "Apple"); ta.AddCell(3, 1, "Banana"); ta.AddCell(3, 2, "Orange");
ta.Render(); } }}