TableNoSides.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 simple table
    // with a grid that has no left and right borders,
    // using the GrapeCity.Documents.Drawing.TableRenderer and related classes.
    public class TableNoSides
    {
        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.AnchorTopLeftRight(null, 30, 20, 20);

            var ta = new TableRenderer(g,
                rt, FixedTableSides.TopLeftRight,
                rowCount: 17, 
                columnCount: 4,
                gridLineColor: Color.SlateBlue,
                gridLineWidth: 2,
                rowMinHeight: 20);

            var columns = ta.ColumnRects;
            columns[0].SetWidth(40);
            columns[1].SetStarWidth(150);
            columns[2].SetStarWidth(250);
            columns[3].SetStarWidth(250);

            ta.SetVerticalGridLineWidth(0, 0);
            ta.SetVerticalGridLineWidth(4, 0);

            ta.SetHorizontalGridLineWidth(0, 5);
            ta.SetHorizontalGridLineWidth(1, 5);
            ta.SetHorizontalGridLineWidth(17, 5);

            var fmt1 = new TextFormat
            {
                Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "calibri.ttf")),
                FontSize = 16f,
                FontSizeInGraphicUnits = true
            };
            var cs = new CellStyle
            {
                PaddingTop = 2,
                PaddingBottom = 5,
                PaddingLeftRight = 10,
                TextFormat = fmt1,
                TextAlignment = TextAlignment.Trailing
            };
            var csBold = new CellStyle(cs)
            {
                TextAlignment = TextAlignment.Center,
                TextFormat = new TextFormat(fmt1)
                {
                    FontBold = true
                }
            };
            var csText = new CellStyle(cs)
            {
                TextAlignment = TextAlignment.Leading,
            };

            ta.AddCell(csBold, 0, 0, "#");
            ta.AddCell(csBold, 0, 1, "Name");
            ta.AddCell(csBold, 0, 2, "Value");
            ta.AddCell(csBold, 0, 3, "Comment");
            for (int i = 1; i <= 16; i++)
            {
                ta.AddCell(cs, i, 0, $"{i}");
                ta.AddCell(csText, i, 1, $"Math.Sqrt({i})");
                ta.AddCell(cs, i, 2, Math.Sqrt(i).ToString());
                ta.AddCell(csText, i, 3, $"Square root of {i}");
            }

            ta.Render();
        }
    }
}