TableCellSpans.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 cells spanning multiple rows and columns,
    // using the GrapeCity.Documents.Drawing.TableRenderer and related classes.
    public class TableCellSpans
    {
        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: 5, 
                columnCount: 4,
                gridLineColor: Color.CornflowerBlue,
                gridLineWidth: 5,
                rowMinHeight: 50,
                columnMinWidth: 120);

            // We set a "star" (weighted) width for the third column so that the table
            // can expand since both the left and right sides of the table are fixed.
            ta.ColumnRects[2].SetStarWidth(1f);

            ta.DefaultCellStyle = new CellStyle
            {
                LineWidth = 1,
                LineColor = Color.Coral,
                LinePaddingAll = 2,
                PaddingAll = 5,
                TextFormat = new TextFormat
                {
                    Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "segoeui.ttf")),
                    FontSizeInGraphicUnits = true,
                    FontSize = 16,
                }
            };

            // AddCell()'s first 4 arguments are rowIndex, columnIndex, rowSpan, columnSpan:
            ta.AddCell(0, 0, 1, 1, "1 2 3 4 5 6 7 8 9 0.");
            ta.AddCell(0, 1, 2, 2, "1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 " +
                    "1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 " +
                    "4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0.");
            ta.AddCell(2, 2, 3, 1, "1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 " +
                    "1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 " +
                    "4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 " +
                    "7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0.");
            ta.AddCell(1, 3, 3, 1, "1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 " +
                    "1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0.");
            ta.AddCell(4, 1, 1, 1, "1 2 3 4 5.");
            ta.AddCell(3, 0, 2, 1);

            ta.AddMissingCells();

            ta.Render();
        }
    }
}