TimeChartTable.cs
// 完毕:
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 representing a time chart,
    // using the GrapeCity.Documents.Drawing.TableRenderer and related classes.
    public class TimeChartTable
    {
        public int CreatePDF(Stream stream)
        {
            var doc = new GcPdfDocument();
            var p = doc.Pages.Add(new SizeF(doc.PageSize.Height, doc.PageSize.Width));
            var g = p.Graphics;

            DrawTable(g, g.CanvasSize.Width, g.CanvasSize.Height);

            // 保存 PDF:
            doc.Save(stream);
            return doc.Pages.Count;
        }

        class Range
        {
            public Range(double valueLow, double deltaLow, double valueHigh, double deltaHigh)
            {
                ValueLow = valueLow;
                DeltaLow = deltaLow;
                ValueHigh = valueHigh;
                DeltaHigh = deltaHigh;
            }
            public double ValueLow { get; }
            public double DeltaLow { get; } 
            public double ValueHigh { get; } 
            public double DeltaHigh { get; }
        }

        static void DrawTable(GcGraphics g, float pageWidth, float pageHeight)
        {
            var host = new LayoutHost();
            var view = host.CreateView(pageWidth, pageHeight * 0.7f);

            var rt = view.CreateRect();

            // Pad the table rectangle equally from left, right, top and bottom:
            rt.AnchorDeflate(null, 10);

            // All sides of the table are fixed, so we can apply
            // star widths to columns and star heights to rows:
            var ta = new TableRenderer(g,
                rt, FixedTableSides.All,
                rowCount: 10, columnCount: 32,
                gridLineColor: Color.DimGray,
                gridLineWidth: 1);

            var columns = ta.ColumnRects;
            columns[0].SetWidth(120);
            for (int i = 1; i < 32; i++)
            {
                columns[i].SetStarWidth(1f);
            }

            // Table header is a part of the table;
            // the first row (rows[0]) is for the table header:
            var rows = ta.RowRects;
            rows[0].SetHeight(40);
            rows[1].SetHeight(30);
            for (int i = 2; i < 10; i++)
            {
                rows[i].SetStarHeight(1f);
            }

            var fmt = new TextFormat
            {
                Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "segoeui.ttf")),
                FontSize = 16,
                FontSizeInGraphicUnits = true
            };
            var cs = new CellStyle
            {
                TextFormat = fmt,
                TextAlignment = TextAlignment.Center
            };

            var csTitle = new CellStyle(cs)
            {
                Background = true,
                TextFormat = new TextFormat(fmt)
                {
                    FontSize = 16,
                    FontBold = true
                }
            };
            ta.AddCell(csTitle, 0, 0, 1, 32, "Small Business Marketing Plan");

            ta.DefaultCellStyle = new CellStyle(cs)
            {
                ParagraphAlignment = ParagraphAlignment.Center
            };
            for (int i = 1; i < 32; i++)
            {
                ta.AddCell(1, i, i.ToString());
            }

            ta.DefaultCellStyle = new CellStyle(cs)
            {
                TextAlignment = TextAlignment.Leading,
                PaddingLeft = 3
            };
            ta.AddCell(2, 0, "Business Overview");
            ta.AddCell(3, 0, "Market Analysis");
            ta.AddCell(4, 0, "Marketing Strategy");
            ta.AddCell(5, 0, "Operations Plan");
            ta.AddCell(6, 0, "Organization");
            ta.AddCell(7, 0, "Management");
            ta.AddCell(8, 0, "Legal aspects");
            ta.AddCell(9, 0, "Financial Plan");

            // Add merged background cells for color highlighting:
            AddBand(2, 1, 6, Color.FromArgb(247, 202, 171));
            AddBand(3, 2, 7, Color.FromArgb(179, 198, 231));
            AddBand(4, 5, 6, Color.FromArgb(255, 229, 154));
            AddBand(5, 11, 6, Color.FromArgb(45, 116, 182));
            AddBand(6, 17, 6, Color.FromArgb(255, 155, 155));
            AddBand(7, 18, 6, Color.FromArgb(197, 224, 179));
            AddBand(8, 24, 4, Color.FromArgb(3, 174, 80));
            AddBand(9, 28, 4, Color.FromArgb(1, 176, 241));

            void AddBand(int rowIndex, int columnIndex, int columnCount, Color color)
            {
                ta.AddCell(new CellStyle
                {
                    Background = true,
                    FillColor = color
                }, rowIndex, columnIndex, 1, columnCount);
            }

            // We exclude the first row when we add missing cells
            // to avoid drawing grid lines for the table header:
            ta.AddMissingCells(1, 0, 9, 32);

            ta.Render();
        }
    }
}