DrawRotatedText.cs
// 完毕:
using System;
using System.IO;
using System.Drawing;
using GrapeCity.Documents.Pdf;
using GrapeCity.Documents.Text;
using GrapeCity.Documents.Drawing;
using GCTEXT = GrapeCity.Documents.Text;
using GCDRAW = GrapeCity.Documents.Drawing;

namespace DsPdfWeb.Demos.Basics
{
    // This demo illustrates the results of using the GcGraphics.DrawRotatedText() method
    // with different combinations of arguments.
    // The DrawRotatedText() method draws text rotated within a specified rectangle
    // similar to how rotated text is drawn in MS Excel cells without borders.
    // See also the DrawSlantedText demo.
    public class DrawRotatedText
    {
        public int CreatePDF(Stream stream)
        {
            var doc = new GcPdfDocument();
            var page = doc.Pages.Add();
            var g = page.Graphics;
            // Set up some values to manage layout:
            var gap = g.Resolution;
            var w = page.Size.Width / 3;
            var h = w;

            // Draw text at different negative angles:
            Draw(g, q0(), angle: -90, false, RotatedTextAlignment.BottomLeft, TextAlignment.Leading);
            Draw(g, q1(), angle: -60, false, RotatedTextAlignment.BottomLeft, TextAlignment.Leading);
            Draw(g, q2(), angle: -45, false, RotatedTextAlignment.BottomLeft, TextAlignment.Leading);
            Draw(g, q3(), angle: -30, false, RotatedTextAlignment.BottomLeft, TextAlignment.Leading);
            // Draw text at different positive angles:
            page = doc.Pages.Add();
            g = page.Graphics;
            Draw(g, q0(), angle: -90, false, RotatedTextAlignment.TopRight, TextAlignment.Leading);
            Draw(g, q1(), angle: -60, false, RotatedTextAlignment.TopRight, TextAlignment.Leading);
            Draw(g, q2(), angle: -45, false, RotatedTextAlignment.TopRight, TextAlignment.Leading);
            Draw(g, q3(), angle: -30, false, RotatedTextAlignment.TopRight, TextAlignment.Leading);
            // Draw text using different vertical stacking:
            page = doc.Pages.Add();
            g = page.Graphics;
            Draw(g, q0(), angle: -30, verticalStacking: true, RotatedTextAlignment.TopRight, TextAlignment.Trailing);
            Draw(g, q1(), angle: -30, verticalStacking: false, RotatedTextAlignment.TopRight, TextAlignment.Trailing);
            Draw(g, q2(), angle: -30, verticalStacking: false, RotatedTextAlignment.BottomLeft, TextAlignment.Leading);
            Draw(g, q3(), angle: -30, verticalStacking: true, RotatedTextAlignment.BottomLeft, TextAlignment.Leading);
            // RotatedTextAlignment affects the location of text (red) within the target rectangle (green):
            page = doc.Pages.Add();
            g = page.Graphics;
            Draw(g, q0(), angle: -30, verticalStacking: true, RotatedTextAlignment.TopRight, TextAlignment.Trailing);
            Draw(g, q1(), angle: 60, verticalStacking: false, RotatedTextAlignment.BottomRight, TextAlignment.Trailing);
            Draw(g, q2(), angle: 30, verticalStacking: true, RotatedTextAlignment.TopLeft, TextAlignment.Leading);
            Draw(g, q3(), angle: -60, verticalStacking: false, RotatedTextAlignment.BottomLeft, TextAlignment.Leading);
            // Draw vertically stacked text using different TextAlignment values:
            page = doc.Pages.Add();
            g = page.Graphics;
            Draw(g, q0(), angle: 30, verticalStacking: true, RotatedTextAlignment.TopLeft, TextAlignment.Leading);
            Draw(g, q1(), angle: 30, verticalStacking: true, RotatedTextAlignment.TopLeft, TextAlignment.Trailing);
            Draw(g, q2(), angle: 30, verticalStacking: true, RotatedTextAlignment.TopLeft, TextAlignment.Center);
            Draw(g, q3(), angle: 30, verticalStacking: true, RotatedTextAlignment.TopLeft, TextAlignment.Distributed);
            // Draw horizontally stacked text using different TextAlignment values:
            page = doc.Pages.Add();
            g = page.Graphics;
            Draw(g, q0(), angle: -70, verticalStacking: false, RotatedTextAlignment.BottomLeft, TextAlignment.Leading);
            Draw(g, q1(), angle: -70, verticalStacking: false, RotatedTextAlignment.BottomLeft, TextAlignment.Trailing);
            Draw(g, q2(), angle: -70, verticalStacking: false, RotatedTextAlignment.BottomLeft, TextAlignment.Center);
            Draw(g, q3(), angle: -70, verticalStacking: false, RotatedTextAlignment.BottomLeft, TextAlignment.Distributed);

            // 完毕:
            doc.Save(stream);
            return doc.Pages.Count;

            RectangleF q0()
            {
                return new RectangleF(gap, gap, w, h);
            }
            RectangleF q1()
            {
                return new RectangleF(gap + w + gap, gap, w, h);
            }
            RectangleF q2()
            {
                return new RectangleF(gap, gap + h + gap, w, h);
            }
            RectangleF q3()
            {
                return new RectangleF(gap + w + gap, gap + h + gap, w, h);
            }
        }

        static void Draw(GcGraphics g, RectangleF rect, int angle, bool verticalStacking,
            RotatedTextAlignment rotatedAlign, TextAlignment textAlign)
        {
            // Draw a legend stating the current DrawRotatedText arguments' values:
            var tlLegend = g.CreateTextLayout();
            tlLegend.DefaultFormat.FontName = "Calibri";
            tlLegend.DefaultFormat.FontSize = 9;
            tlLegend.AppendLine($"Rotation angle: {angle}°");
            tlLegend.AppendLine($"Text alignment: {textAlign}");
            tlLegend.AppendLine($"Rotated text alignment: {rotatedAlign}");
            tlLegend.AppendLine($"Is vertical stacking: {verticalStacking}");
            g.DrawTextLayout(tlLegend, rect.Location);

            // The target rectangle for the DrawRotatedText call:
            var d = tlLegend.ContentHeight + 12;
            rect.Y += d;
            rect.Height -= d;

            // Text layout to draw:
            var tl = g.CreateTextLayout();
            tl.DefaultFormat.FontName = "Calibri";
            tl.DefaultFormat.FontSize = 12;
            tl.Append("The quick brown fox jumps over the lazy dog. ");
            tl.Append("The quick brown fox jumps over the lazy dog.");
            tl.TextAlignment = textAlign;

            // Outline the target rectangle in green:
            g.DrawRectangle(rect, new GCDRAW::Pen(Color.PaleGreen, 3));
            // Outline the actual text rectangle in red:
            var tlCopy = tl.Clone(true);
            var tlRect = g.MeasureRotatedText(tlCopy, angle, verticalStacking, rect, rotatedAlign);
            g.DrawRectangle(tlRect, new GCDRAW::Pen(Color.Red, 1));

            // Draw rotated text:
            g.DrawRotatedText(tl, angle, verticalStacking, rect, rotatedAlign);
        }
    }
}