ParagraphAlign.cs
// 完毕:
using System;
using System.Drawing;
using System.IO;
using GrapeCity.Documents.Pdf;
using GrapeCity.Documents.Text;

namespace DsPdfWeb.Demos.Basics
{
    // 此示例演示了段落对齐选项
    // (水平 LTR 文本的顶部/中心/对齐/底部)。
    public class ParagraphAlign
    {
        public int CreatePDF(Stream stream)
        {
            var doc = new GcPdfDocument();
            var page = doc.NewPage();
            var g = page.Graphics;
            var tl = g.CreateTextLayout();
            tl.DefaultFormat.Font = StandardFonts.Times;
            tl.DefaultFormat.FontSize = 12;
            var borderColor = Color.FromArgb(217, 217, 217);

            var h = (page.Size.Height - 72) / 5;
            var bounds = new RectangleF(36, 36, page.Size.Width - 72, h);

            tl.MaxWidth = bounds.Width;
            tl.MaxHeight = bounds.Height;

            var para = Common.Util.LoremIpsum(1, 5, 5, 10, 12);

            // 1:段落对齐方式.Near
            tl.ParagraphAlignment = ParagraphAlignment.Near;
            tl.Append("ParagraphAlignment.Near: ");
            tl.Append(para);
            tl.PerformLayout(true);
            g.DrawTextLayout(tl, bounds.Location);
            g.DrawRectangle(bounds, borderColor);

            // 2:段落对齐方式.居中
            bounds.Offset(0, h);
            tl.Clear();
            tl.ParagraphAlignment = ParagraphAlignment.Center;
            tl.Append("ParagraphAlignment.Center: ");
            tl.Append(para);
            tl.PerformLayout(true);
            g.DrawTextLayout(tl, bounds.Location);
            g.DrawRectangle(bounds, borderColor);

            // 3:段落对齐方式.Justified
            bounds.Offset(0, h);
            tl.Clear();
            tl.ParagraphAlignment = ParagraphAlignment.Justified;
            tl.Append("ParagraphAlignment.Justified: ");
            tl.Append(para);
            tl.PerformLayout(true);
            g.DrawTextLayout(tl, bounds.Location);
            g.DrawRectangle(bounds, borderColor);

            // 4:段落对齐.分布式
            bounds.Offset(0, h);
            tl.Clear();
            tl.ParagraphAlignment = ParagraphAlignment.Distributed;
            tl.Append("ParagraphAlignment.Distributed: ");
            tl.Append(para);
            tl.PerformLayout(true);
            g.DrawTextLayout(tl, bounds.Location);
            g.DrawRectangle(bounds, borderColor);

            // 5:段落对齐.远
            bounds.Offset(0, h);
            tl.Clear();
            tl.ParagraphAlignment = ParagraphAlignment.Far;
            tl.Append("ParagraphAlignment.Far: ");
            tl.Append(para);
            tl.PerformLayout(true);
            g.DrawTextLayout(tl, bounds.Location);
            g.DrawRectangle(bounds, borderColor);

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