OddEvenRows.cs
// 
// このコードは、DioDocs for PDF のサンプルの一部として提供されています。
// © 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
{
    // このサンプルでは、GrapeCity.Documents.Drawing.TableRenderer クラスなどを使用して、
    // 奇数行と偶数行で異なる背景色を設定したテーブルを描画する方法を紹介しています。
    public class OddEvenRows
    {
        public int CreatePDF(Stream stream)
        {
            var doc = new GcPdfDocument();
            var p = doc.Pages.Add();
            var g = p.Graphics;

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

            // PDF を保存します。
            doc.Save(stream);
            return doc.Pages.Count;
        }

        // テーブルのデータソースです。
        static readonly Team[] _teams = new Team[]
        {
            new Team("イングランド"),
            new Team("フランス"),
            new Team("アイルランド"),
            new Team("イタリア"),
            new Team("スコットランド"),
            new Team("ウェールズ"),
        };

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

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

            var ta = new TableRenderer(g,
                rt, FixedTableSides.TopRight,
                rowCount: _teams.Length + 2,
                columnCount: 11,
                gridLineColor: Color.FromArgb(173, 223, 252),
                gridLineWidth: 1,
                rowMinHeight: 10,
                columnMinWidth: 10);

            var fmt = new TextFormat
            {
                Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "ipag.ttc")),
                FontSize = 18,
                FontSizeInGraphicUnits = true
            };
            var cs = new CellStyle
            {
                TextFormat = fmt,
                FixedWidth = false,
                PaddingAll = 4
            };
            var csHeaderH = new CellStyle(cs)
            {
                TextFormat = new TextFormat(fmt)
                {
                    ForeColor = Color.White,
                    FontBold = true
                },
                FillColor = Color.FromArgb(17, 93, 140),
                TextAlignment = TextAlignment.Center,
                ParagraphAlignment = ParagraphAlignment.Center,
            };
            var csHeaderV = new CellStyle(csHeaderH)
            {
                RotationAngle = 270,
                TextAlignment = TextAlignment.Leading,
                PaddingLeft = 3
            };
            var csNumber = new CellStyle(cs)
            {
                TextAlignment = TextAlignment.Center
            };
            var csNation = new CellStyle(cs)
            {
                TextFormat = new TextFormat(fmt)
                {
                    ForeColor = Color.FromArgb(50, 123, 197)
                },
            };

            // ヘッダーにセルを追加します。
            ta.AddCell(csHeaderV, 0, 0, 2, 1, "順位");
            ta.AddCell(csHeaderH, 0, 1, 2, 1, "国");
            ta.AddCell(csHeaderH, 0, 2, 1, 4, "試合");
            ta.AddCell(csHeaderV, 1, 2, "試合数");
            ta.AddCell(csHeaderV, 1, 3, "勝数");
            ta.AddCell(csHeaderV, 1, 4, "引分数");
            ta.AddCell(csHeaderV, 1, 5, "敗数");
            ta.AddCell(csHeaderH, 0, 6, 1, 4, "点");
            ta.AddCell(csHeaderV, 1, 6, "得点");
            ta.AddCell(csHeaderV, 1, 7, "失点");
            ta.AddCell(csHeaderV, 1, 8, "得失差");
            ta.AddCell(csHeaderV, 1, 9, "トライ");
            ta.AddCell(csHeaderH, 0, 10, 2, 1, "総合点");

            // データセルを追加します。
            for (int i = 0; i < _teams.Length; i++)
            {
                int rowIndex = i + 2;
                var team = _teams[i];
                ta.AddCell(csNumber, rowIndex, 0, $"{i + 1}");
                ta.AddCell(csNation, rowIndex, 1, team.Nation);
                ta.AddCell(csNumber, rowIndex, 2, $"{team.Played}");
                ta.AddCell(csNumber, rowIndex, 3, $"{team.Won}");
                ta.AddCell(csNumber, rowIndex, 4, $"{team.Drawn}");
                ta.AddCell(csNumber, rowIndex, 5, $"{team.Lost}");
                ta.AddCell(csNumber, rowIndex, 6, $"{team.For}");
                ta.AddCell(csNumber, rowIndex, 7, $"{team.Against}");
                ta.AddCell(csNumber, rowIndex, 8, $"{team.Diff}");
                ta.AddCell(csNumber, rowIndex, 9, $"{team.Tries}");
                ta.AddCell(csNumber, rowIndex, 10, $"{team.TablePoints}");
            }

            // 奇数行の背景を変更します。
            ta.DefaultCellStyle = new CellStyle
            {
                Background = true,
                FillColor = Color.FromArgb(238, 238, 238)
            };
            for (int i = 0; i < _teams.Length; i += 2)
            {
                ta.AddCell(i + 2, 0, 1, 11);
            }

            ta.Render();
        }

        // サンプルデータを格納するためのクラスです。
        class Team
        {
            static readonly Random _rnd = Common.Util.NewRandom();

            public string Nation;
            public int Played, Won, Drawn, Lost;
            public int For, Against, Diff, Tries;
            public int TablePoints;

            internal Team(string nation)
            {
                Nation = nation;
                Played = _rnd.Next(0, 50);
                Won = _rnd.Next(0, 50);
                Drawn = _rnd.Next(0, 50);
                Lost = _rnd.Next(0, 50);
                For = _rnd.Next(0, 50);
                Against = _rnd.Next(0, 50);
                Diff = _rnd.Next(0, 50);
                Tries = _rnd.Next(0, 50);
                TablePoints = _rnd.Next(0, 150);
            }
        }
    }
}