RotatedTable.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 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
{
    // This example shows how to draw a rotated table using an angle constraint.
    // An angle constraint is similar to rotation with a transformation matrix,
    // but unlike transformation, using angle constraint makes it easier to add
    // other visual elements to the view.
    public class RotatedTable
    {
        public int CreatePDF(Stream stream)
        {
            var doc = new GcPdfDocument();
            var p = doc.NewPage();
            var g = p.Graphics;

            // Changing the default graphics resolution (72dpi for GcPdfGraphics)
            // allows us to scale a document without loss of fidelity:
            g.Resolution = 96;
            DrawTable(g, g.CanvasSize.Width, g.CanvasSize.Height);

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

        class ItemLine
        {
            public ItemLine(string text, double sales, double marginNumber, double marginPercent, int totalCustomers)
            {
                Text = text;
                Sales = sales;
                MarginNumber = marginNumber;
                MarginPercent = marginPercent;
                TotalCustomers = totalCustomers;
            }
            public string Text { get; }
            public double Sales { get; }
            public double MarginNumber { get; }
            public double MarginPercent { get; }
            public int TotalCustomers { get; }
        }

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

            // Note that the table rectangle is rotated using an AngleConstraint.
            // We don't need a separate View with a transformation matrix.

            var rt = view.CreateRect();
            rt.SetAngle(null, 270);
            rt.SetTop(null, AnchorParam.Left, marginX);
            rt.SetRight(null, AnchorParam.Top, -marginY);

            var ta = new TableRenderer(g,
                rt, FixedTableSides.TopRight,
                rowCount: 9,
                columnCount: 5,
                gridLineColor: Color.Black,
                gridLineWidth: 1,
                paddingLeft: 20,
                paddingRight: 20,
                paddingBottom: 20,
                paddingTop: 12);

            var lgb = new LinearGradientBrush(Color.FromArgb(240, 234, 249), new PointF(0, 0), Color.FromArgb(201, 181, 232), new PointF(0, 1));
            ta.TableFrameStyle = new FrameStyle
            {
                LineColor = Color.FromArgb(126, 96, 160),
                LineWidth = 1,
                FillBrush = lgb
            };

            var columns = ta.ColumnRects;
            columns[0].SetWidth(80);
            columns[1].SetWidth(125);
            columns[2].SetWidth(110);
            columns[3].SetWidth(80);
            columns[4].SetWidth(80);

            var fmt = new TextFormat
            {
                Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "calibri.ttf")),
                FontSize = 16,
                FontSizeInGraphicUnits = true,
                FontFeatures = new FontFeature[] { new FontFeature(FeatureTag.liga, false) }
            };
            var fmtHeader = new TextFormat(fmt)
            {
                ForeColor = Color.White,
                FontBold = true
            };

            var csTitle = new CellStyle
            {
                Background = true,
                TextFormat = new TextFormat(fmt)
                {
                    FontBold = true,
                    FontSize = 20
                },
                TextAlignment = TextAlignment.Center,
                PaddingBottom = 8
            };

            ta.AddCell(csTitle, 0, 0, 1, 5, "Class Schedule");

            var csHeader = new CellStyle
            {
                TextFormat = fmtHeader,
                TextAlignment = TextAlignment.Center,
                PaddingTop = 5,
                PaddingBottom = 6
            };
            ta.AddCell(csHeader, 1, 0, "LESSON");
            ta.AddCell(csHeader, 1, 1, "TOPIC");
            ta.AddCell(csHeader, 1, 2, "ASSIGNMENT");
            ta.AddCell(csHeader, 1, 3, "POINTS");
            ta.AddCell(csHeader, 1, 4, "DUE");

            var csCenter = new CellStyle(csHeader)
            {
                TextFormat = fmt,
                PaddingLeftRight = 6
            };
            var csLeft = new CellStyle(csCenter)
            {
                TextAlignment = TextAlignment.Leading
            };

            ta.AddCell(csCenter, 2, 0, 2, 1, "1");
            ta.AddCell(csLeft, 2, 1, 2, 1, "What is Distance Learning?");
            ta.AddCell(csLeft, 2, 2, "Wiki #1");
            ta.AddCell(csCenter, 2, 3, "10");
            ta.AddCell(csCenter, 2, 4, "March 10");
            ta.AddCell(csLeft, 3, 2, "Presentation");
            ta.AddCell(csCenter, 3, 3, "20");
            ta.AddCell(csCenter, 3, 4);

            ta.AddCell(csCenter, 4, 0, "2");
            ta.AddCell(csLeft, 4, 1, "History & Theories");
            ta.AddCell(csLeft, 4, 2, "Brief Paper");
            ta.AddCell(csCenter, 4, 3, "20");
            ta.AddCell(csCenter, 4, 4, "March 24");

            ta.AddCell(csCenter, 5, 0, 1, 5, "Spring Break");

            ta.AddCell(csCenter, 6, 0, 2, 1, "3");
            ta.AddCell(csLeft, 6, 1, 2, 1, "Distance Learners");
            ta.AddCell(csLeft, 6, 2, "Discussion #1");
            ta.AddCell(csCenter, 6, 3, "10");
            ta.AddCell(csCenter, 6, 4, "April 7");
            ta.AddCell(csLeft, 7, 2, "Group Project");
            ta.AddCell(csCenter, 7, 3, "50");
            ta.AddCell(csCenter, 7, 4, "April 14");

            ta.AddCell(csCenter, 8, 0, "4");
            ta.AddCell(csLeft, 8, 1, "Media Selection");
            ta.AddCell(csLeft, 8, 2, "Blog #1");
            ta.AddCell(csCenter, 8, 3, "10");
            ta.AddCell(csCenter, 8, 4, "April 21");

            ta.AddCell(new CellStyle
            {
                Background = true,
                FillColor = Color.FromArgb(79, 129, 189)
            }, 1, 0, 1, 5);

            ta.AddCell(new CellStyle
            {
                Background = true,
                FillColor = Color.FromArgb(208, 216, 232)
            }, 2, 0, 7, 5);

            var bkHighlight = new CellStyle
            {
                Background = true,
                FillColor = Color.FromArgb(233, 237, 244)
            };
            ta.AddCell(bkHighlight, 3, 2, 1, 3);
            ta.AddCell(bkHighlight, 5, 0, 1, 5);
            ta.AddCell(bkHighlight, 7, 2, 1, 3);

            ta.ApplyCellConstraints();

            g.Transform = Matrix3x2.CreateScale(1.8f);
            ta.Render(g);
        }
    }
}