PlainTable.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 System.Collections.Generic;
using GrapeCity.Documents.Drawing;
using GrapeCity.Documents.Text;
using GrapeCity.Documents.Imaging;
using GrapeCity.Documents.Layout.Composition;
using GCTEXT = GrapeCity.Documents.Text;
using GCDRAW = GrapeCity.Documents.Drawing;

namespace DsImagingWeb.Demos
{
    // This example shows how to draw a plain table with random data,
    // using the GrapeCity.Documents.Drawing.TableRenderer and related classes.
    public class PlainTable
    {
        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;
        }

        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)));

            // The Surface object helps with calculating layout
            // of the table and drawing the table on a graphics.
            var surf = new Surface();
            var view = surf.CreateView(pageWidth, pageHeight);
            var visual = view.CreateVisual();

            // The table is always contained within a LayoutRect which is passed to the TableRenderer constructor.
            var rt = visual.LayoutRect;
            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", "calibri.ttf")),
                    FontSize = 21,
                    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, "Name");
            ta.AddCell(0, 1, "Take 1\nThe Good");
            ta.AddCell(0, 2, "Take 2\nThe Bad");
            ta.AddCell(0, 3, "Take 3\nThe Ugly");

            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());
            }

            // Applying layout to the table cells must precede drawing the Surface.
            ta.ApplyCellConstraints();

            visual.Draw = (g, v) =>
            {
                // Move from Visual transform to Surface transform before drawing the table.
                g.Transform = v.Layer.Surface.BaseTransform;
                ta.Render();
            };

            surf.Render(g);
        }
    }
}