TextJustifyRules.cs
// 
// このコードは、DioDocs for PDF のサンプルの一部として提供されています。
// © MESCIUS inc. All rights reserved.
// 
using System;
using System.Drawing;
using System.IO;
using GrapeCity.Documents.Pdf;
using GrapeCity.Documents.Text;
using GCTEXT = GrapeCity.Documents.Text;
using GCDRAW = GrapeCity.Documents.Drawing;

namespace DsPdfWeb.Demos.Basics
{
    // TBD:

    public class TextJustifyRules
    {
        public int CreatePDF(Stream stream)
        {
            var arialbd = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "arialbd.ttf"));
            var arialuni = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "arialuni.ttf"));

            var doc = new GcPdfDocument();
            var page = doc.NewPage();
            var g = page.Graphics;

            var tl = g.CreateTextLayout();
            //tl.LineBreakingRules = LineBreakingRules.Unicode;
            //tl.WordBoundaryRules = WordBoundaryRules.Unicode;
            //tl.TextExtensionStrategy = TextExtensionStrategy.Default;
            tl.TextAlignment = TextAlignment.Distributed;
            tl.JustifiedSpaceExtension = 0f;
            tl.JustifiedTextExtension = 20f;

            var tf = new TextFormat { FontSize = 26f, Font = arialuni };
            var tfInfo = new TextFormat { FontSize = 11f, Font = arialbd };

            float marginx = 120, marginy = 36;
            tl.MaxWidth = page.Size.Width - marginx * 2;
            var text = "abcdefg!1010101010abc;999999本列島で使され99 555";

            float DrawText(TextLayout tl, float y)
            {
                var pt = new PointF(marginx, y + 20);
                tl.Append(text, tf);
                tl.PerformLayout(true);
                var rc = new RectangleF(pt, new SizeF(tl.ContentWidth, tl.ContentHeight));
                g.FillRectangle(rc, Color.PaleGoldenrod);
                g.DrawString($"LineBreakingRules.{tl.LineBreakingRules}, TextExtensionStrategy.{tl.TextExtensionStrategy}:",
                    tfInfo, new PointF(marginx / 2f, y));
                g.DrawTextLayout(tl, pt);
                tl.Clear();
                return rc.Bottom + 16;
            }

            float y = marginy, dy = marginy * 3.5f;
            y = DrawText(tl, y);
            tl.TextExtensionStrategy = TextExtensionStrategy.EastAsianExcel;
            y = DrawText(tl, y);
            tl.TextExtensionStrategy = TextExtensionStrategy.Excel;
            y = DrawText(tl, y);
            tl.LineBreakingRules = LineBreakingRules.Simplified;
            tl.WordBoundaryRules = WordBoundaryRules.Simplified;
            tl.TextExtensionStrategy = TextExtensionStrategy.Default;
            y = DrawText(tl, y);
            tl.TextExtensionStrategy = TextExtensionStrategy.EastAsianExcel;
            y = DrawText(tl, y);
            tl.TextExtensionStrategy = TextExtensionStrategy.Excel;
            y = DrawText(tl, y);

            // PDF ドキュメントを保存します。
            doc.Save(stream);
            return doc.Pages.Count;
        }
    }
}