PlainTable.cs
// 完毕:
using System;
using System.IO;
using System.Drawing;
using System.Numerics;
using System.Collections.Generic;
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 plain table with random data,
    // using the GrapeCity.Documents.Drawing.TableRenderer and related classes.
    public class PlainTable
    {
        public int CreatePDF(Stream stream)
        {
            var doc = new GcPdfDocument();
            var p = doc.NewPage();
            p.Size = new SizeF(p.Size.Height, p.Size.Width);
            var g = p.Graphics;

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

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

        class Record
        {
            public Record(string name, int take1, int take2, int take3)
            {
                Name = name;
                Take1 = take1;
                Take2 = take2;
                Take3 = take3;
            }
            public string Name { get; }
            public int Take1 { get; }
            public int Take2 { get; }
            public int Take3 { get; }
        }

        static void DrawTable(GcGraphics g, float pageWidth, float pageHeight)
        {
            var rnd = Common.Util.NewRandom();
            var records = new List<Record>();
            for (int i = 0; i < 10; ++i)
                records.Add(new Record(Common.Util.LoremIpsum(1, 1, 1, 1, 4), rnd.Next(100), rnd.Next(100), rnd.Next(100)));

            var host = new LayoutHost();
            var view = host.CreateView(pageWidth, pageHeight);

            var rt = view.CreateRect();
            rt.AnchorTopLeft(null, 36, 36);

            var ta = new TableRenderer(g,
                rt, FixedTableSides.TopLeft,
                rowCount: records.Count + 1,
                columnCount: 4,
                gridLineColor: Color.DarkGray,
                gridLineWidth: 1,
                rowMinHeight: 10);

            var cs = new CellStyle
            {
                PaddingTopBottom = 3,
                PaddingLeftRight = 4,
                FixedWidth = false,
                TextFormat = new TextFormat
                {
                    Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "calibri.ttf")),
                    FontSize = 18,
                    FontSizeInGraphicUnits = true
                }
            };
            var csHeader = new CellStyle(cs)
            {
                PaddingRight = 9,
                ParagraphAlignment = ParagraphAlignment.Center,
                TextAlignment = TextAlignment.Center,
            };
            var csName = new CellStyle(cs)
            {
                MaxWidth = pageWidth / 2,
            };
            var csNumber = new CellStyle(cs)
            {
                TextAlignment = TextAlignment.Trailing
            };

            ta.DefaultCellStyle = csHeader;
            ta.AddCell(0, 0, "Name");
            ta.AddCell(0, 1, "Take 1\nThe Good");
            ta.AddCell(0, 2, "Take 2\nThe Bad");
            ta.AddCell(0, 3, "Take 3\nThe Ugly");

            ta.DefaultCellStyle = csNumber;
            for (int i = 0; i < records.Count; i++)
            {
                var p = records[i];
                int r = i + 1;
                ta.AddCell(csName, r, 0, p.Name);
                ta.AddCell(r, 1, p.Take1.ToString());
                ta.AddCell(r, 2, p.Take2.ToString());
                ta.AddCell(r, 3, p.Take3.ToString());
            }

            ta.ApplyCellConstraints();
            ta.Render();
        }
    }
}