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

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