VerticalText.cs
// 完毕:
using System;
using System.IO;
using System.Collections.Generic;
using System.Drawing;
using GrapeCity.Documents.Pdf;
using GrapeCity.Documents.Text;
using GrapeCity.Documents.Drawing;

namespace DsPdfWeb.Demos.Basics
{
    // 演示在 LTR 和 RTL 模式下垂直文本的渲染。
    // 还展示了如何让文本围绕矩形对象流动。
    // 另请参阅JapaneseColumns。
    public class VerticalText
    {
        public int CreatePDF(Stream stream)
        {
            var doc = new GcPdfDocument();
            var page = doc.NewPage();
            // 使用横向:
            page.Landscape = true;
            var g = page.Graphics;
            // 一些日语、英语和阿拉伯语示例文本:
            string text1 = "学校教育の「国語」で教えられる。";
            string text2 = " flow direction. ";
            string text3 = "النص العربي 12 + 34 = 46 مع الأرقام ";
            // 初始化字体缓存并获取所需的字体:
            var fc = new FontCollection();
            fc.RegisterDirectory(Path.Combine("Resources", "Fonts"));
            var fYuMin = fc.FindFamilyName("Yu Mincho");
            var fTimes = fc.FindFamilyName("Times New Roman");
            var fArial = fc.FindFamilyName("Arial");
            // 创建文本格式:
            var tf1 = new TextFormat() { Font = fYuMin };
            var tf2 = new TextFormat() { Font = fTimes };
            var tf3 = new TextFormat() { Font = fArial };
            // 创建 TextLayout 并在其上设置一些选项:
            var tl = g.CreateTextLayout();
            tl.FirstLineIndent = 36;
            tl.TextAlignment = TextAlignment.Justified;
            // 此设置也证明了最后一行的合理性:
            tl.LastLineIsEndOfParagraph = false;
            // 将所有边距设置为 1":
            tl.MarginAll = tl.Resolution;
            tl.MaxWidth = page.Size.Width;
            tl.MaxHeight = page.Size.Height;
            // RTL 布局:
            tl.RightToLeft = false;
            // 构建一个对象列表以使文本流动:
            tl.ObjectRects = new List<ObjectRect>()
            {
                new ObjectRect(540, 100, 120, 160),
                new ObjectRect(100, 290, 170, 100),
                new ObjectRect(500, 350, 170, 100)
            };
            // 在页面上填充相应的矩形,以便我们可以看到它们:
            foreach (var or in tl.ObjectRects)
                g.FillRectangle(or.ToRectangleF(), Color.PaleVioletRed);

            // 将文本添加到布局:
            for (int i = 0; i < 3; i++)
            {
                tl.Append(text1, tf1);
                tl.Append("Horizontal Top To Bottom" + text2, tf2);
                tl.AppendLine(text3, tf3);
            }
            // 执行并绘制第一个布局:
            tl.PerformLayout(true);
            g.DrawTextLayout(tl, PointF.Empty);
            g.FillRectangle(tl.ContentRectangle, Color.FromArgb(20, Color.Red));

            // 创建第二个布局 - 垂直逆时针旋转:
            var t = tl.ContentHeight;
            tl.Clear();
            tl.RotateSidewaysCounterclockwise = true;
            tl.FlowDirection = FlowDirection.VerticalLeftToRight;
            tl.MarginTop += t;
            // 将文本添加到布局:
            for (int i = 0; i < 3; i++)
            {
                tl.Append(text1, tf1);
                tl.Append("Vertical Left To Right" + text2, tf2);
                tl.AppendLine(text3, tf3);
            }
            // 执行并绘制第二个布局:
            tl.PerformLayout(true);
            g.DrawTextLayout(tl, PointF.Empty);
            g.FillRectangle(tl.ContentRectangle, Color.FromArgb(20, Color.Green));

            // 创建第三个布局 - 垂直:
            tl.Clear();
            tl.FlowDirection = FlowDirection.VerticalRightToLeft;
            tl.RotateSidewaysCounterclockwise = false;
            // 将文本添加到布局:
            for (int i = 0; i < 3; i++)
            {
                tl.Append(text1, tf1);
                tl.Append("Vertical Right To Left" + text2, tf2);
                tl.AppendLine(text3, tf3);
            }
            // 执行并绘制第三个布局:
            tl.PerformLayout(true);
            g.DrawTextLayout(tl, PointF.Empty);
            g.FillRectangle(tl.ContentRectangle, Color.FromArgb(20, Color.Blue));
            // 完毕:
            doc.Save(stream);
            return doc.Pages.Count;
        }
    }
}