TableNoSides.cs
//
// This code is part of Document Solutions for PDF demos.
// Copyright (c) MESCIUS inc. All rights reserved.
//
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 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 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.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 = 16,
                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();
        }
    }
}