RedNegativesTable.cs
// 
// このコードは、DioDocs for PDF のサンプルの一部として提供されています。
// © MESCIUS inc. All rights reserved.
// 
using System;
using System.IO;
using System.Drawing;
using System.Numerics;
using System.Collections.Generic;
using GrapeCity.Documents.Pdf;
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 DsPdfWeb.Demos
{
    // このサンプルでは、GrapeCity.Documents.Drawing.TableRenderer クラスなどを使用して、
    // 負の値を赤字に設定し、奇数行と偶数行で異なる背景色を設定した、
    // 数値データを含む単純なテーブルを描画する方法を紹介しています。
    public class RedNegativesTable
    {
        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 ItemLine
        {
            public ItemLine(string text, int sales, int marginNumber, double marginPercent, int totalCustomers)
            {
                Text = text;
                Sales = sales;
                MarginNumber = marginNumber;
                MarginPercent = marginPercent;
                TotalCustomers = totalCustomers;
            }
            public string Text { get; }
            public int Sales { get; }
            public int MarginNumber { get; }
            public double MarginPercent { get; }
            public int TotalCustomers { get; }
        }

        static void DrawTable(GcGraphics g, float pageWidth, float pageHeight)
        {
            var items = new ItemLine[]
            {
                new ItemLine("洗濯機・乾燥機", 158000, 19682, 12.46, 247),
                new ItemLine("プロジェクター・スクリーン", 97300, 21126, 21.72, 382),
                new ItemLine("冷蔵庫", 95300, -35057, -36.77, 331),
                new ItemLine("ノートPC", 66800, 18117, 27.11, 301),
                new ItemLine("コーヒーメーカー", 47600, -1115, -2.34, 251),
                new ItemLine("照明器具", 47100, 34031, 72.28, 548),
                new ItemLine("給湯器", 44300, 9459, 21.34, 152),
                new ItemLine("モニター", 42400, -77, -0.18, 327),
                new ItemLine("電子レンジ", 31900, 1430, 4.48, 375),
                new ItemLine("デスクトップPC", 26600, 2322, 8.72, 184),
                new ItemLine("エアコン", 25300, 13782, 54.54, 216),
                new ItemLine("プリンター・スキャナー", 21900, 59, 0.27, 379),
                new ItemLine("コンピュータ周辺機器", 12800, -1823, -14.27, 654),
                new ItemLine("扇風機", 8300, -3864, -46.35, 287)
            };

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

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

            var ta = new TableRenderer(g,
                rt, FixedTableSides.TopLeft,
                rowCount: items.Length + 1,
                columnCount: 5,
                gridLineColor: Color.Transparent,
                gridLineWidth: 0);

            var columns = ta.ColumnRects;
            columns[0].SetWidth(200);
            columns[1].SetWidth(120);
            columns[2].SetWidth(120);
            columns[3].SetWidth(120);
            columns[4].SetWidth(120);

            var fmt = new TextFormat
            {
                Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "ipag.ttc")),
                ForeColor = Color.FromArgb(38, 38, 38),
                FontSize = 14,
                FontSizeInGraphicUnits = true,
            };
            var fmtSmall = new TextFormat(fmt)
            {
                FontSize = 11
            };

            var csCaption = new CellStyle
            {
                TextAlignment = TextAlignment.Trailing,
                ParagraphAlignment = ParagraphAlignment.Center,
                TextFormat = fmt,
                PaddingBottom = 10,
                PaddingRight = 4
            };
            ta.AddCell(new CellStyle(csCaption)
            {
                CustomDraw = (g, tc) =>
                {
                    float y = tc.Height * 0.5f;
                    float x = 10;
                    var pen = new GCDRAW.Pen(Color.DimGray, 1);
                    g.DrawLine(new PointF(x, y - 7), new PointF(x, y + 7), pen);
                    g.DrawLine(new PointF(x - 3, y + 3), new PointF(x, y + 7), pen);
                    g.DrawLine(new PointF(x + 3, y + 3), new PointF(x, y + 7), pen);
                },
                CreateTextLayout = (g, cs, data) =>
                {
                    var tl = g.CreateTextLayout();
                    tl.AppendLine("売上高", fmt);
                    tl.Append("(千円)", fmtSmall);
                    return tl;
                }
            }, 0, 1, null);
            ta.AddCell(new CellStyle(csCaption)
            {
                CreateTextLayout = (g, cs, data) =>
                {
                    var tl = g.CreateTextLayout();
                    tl.AppendLine("マージン", fmt);
                    tl.Append("(千円)", fmtSmall);
                    return tl;
                }
            }, 0, 2, null);
            ta.AddCell(csCaption, 0, 3, "マージン\n(%)");
            ta.AddCell(csCaption, 0, 4, "総顧客数");

            var csName = new CellStyle
            {
                TextFormat = fmt,
                PaddingTopBottom = 5,
                PaddingLeft = 4
            };
            var csNum = new CellStyle
            {
                TextFormat = fmt,
                TextAlignment = TextAlignment.Trailing,
                PaddingTopBottom = 5,
                PaddingRight = 4
            };
            var csNumNegative = new CellStyle(csNum)
            {
                TextFormat = new TextFormat(fmt)
                {
                    ForeColor = Color.Red
                }
            };
            var csBand = new CellStyle
            {
                FillColor = Color.FromArgb(244, 241, 250),
                Background = true
            };

            var ci = new System.Globalization.CultureInfo("ja-JP");

            for (int i = 0; i < items.Length; i++)
            {
                var item = items[i];
                int rowIndex = i + 1;

                if ((i & 1) != 0)
                {
                    ta.AddCell(csBand, rowIndex, 0, 1, 5);
                }
                ta.AddCell(csName, rowIndex, 0, item.Text);
                ta.AddCell(csNum, rowIndex, 1, item.Sales.ToString("c0", ci));
                var n = item.MarginNumber;
                ta.AddCell(n >= 0 ? csNum : csNumNegative, rowIndex, 2, n.ToString("c0"));
                var p = item.MarginPercent;
                ta.AddCell(p >= 0.0 ? csNum : csNumNegative, rowIndex, 3, p.ToString("n2"));
                ta.AddCell(csNum, rowIndex, 4, item.TotalCustomers.ToString("n0"));
            }

            ta.SetVerticalGridLineWidth(1, 2);
            ta.SetHorizontalGridLineWidth(1, 2);

            var pen = new GCDRAW.Pen(Color.FromArgb(159, 146, 213), 2)
            {
                DashStyle = DashStyle.Solid,
                LineCap = PenLineCap.Round
            };
            ta.AddCell(new CellStyle
            {
                Background = true,
                Borders = FrameBorders.LeftBorder | FrameBorders.TopBorder,
                Pen = pen,
                LinePaddingLeft = -2,
                LinePaddingTop = -2,
            }, 1, 1, items.Length, 4);

            ta.Render();
        }
    }
}