TableTextAlign.cs
//
// This code is part of Document Solutions for Imaging demos.
// Copyright (c) MESCIUS inc. All rights reserved.
//
using System;
using System.IO;
using System.Drawing;
using System.Numerics;
using GrapeCity.Documents.Drawing;
using GrapeCity.Documents.Text;
using GrapeCity.Documents.Imaging;
using GrapeCity.Documents.Layout;
using GCTEXT = GrapeCity.Documents.Text;
using GCDRAW = GrapeCity.Documents.Drawing;

namespace DsImagingWeb.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 GcBitmap GenerateImage(Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
        {
            var bmp = new GcBitmap(pixelSize.Width, pixelSize.Height, opaque, dpi, dpi);
            using var g = bmp.CreateGraphics(Color.White);
            DrawTable(g, pixelSize.Width, pixelSize.Height);
            return bmp;
        }

        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", "times.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;
                }
            };

            // Setting the default cell style allows us to call the AddCell
            // method without passing the explicit CellStyle object.
            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();
        }
    }
}