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

namespace DsPdfWeb.Demos.Basics
{
    // 此示例演示如何对包含软连字符 (0x00AD) 的文本进行连字符。
    // 将在软连字符位置插入分隔符(如果它们存在于文本中)
    // 如果 TextLayout.WrapMode 设置为 WordWrap。提供了两个属性来控制
    // 连字符:
    // - TextLayout.SoftHyphenReplacementCharCode:指定用作替换的字符
    //   用于跨行断词时的软连字符。默认情况下该属性为 0x002D
    //   (Unicode 连字符减号字符)。将此属性设置为 0 会破坏单词,而不会
    //   显示任何可见的连字符。将其设置为 -1 可以防止软时断词
    //   hyphens).- TextLayout.LinesBetweenConsecutiveHyphens: 指定最小非连字符数
    //   以连字符结尾的行之间的连字符行。默认情况下,该属性为零。
    public class Hyphenation
    {
        public int CreatePDF(Stream stream)
        {
            // 在线hypho-o工具
            // 被用来在WordCharWrap的示例文本中插入软连字符:
            var str =
                "Lose noth­ing in your doc­u­ments! Grape­City Doc­u­ments for PDF " +
                "in­cludes text and para­graph format­ting, spe­cial char­ac­ters, " +
                "mul­tiple lan­guages, RTL sup­port, ver­tic­al and ro­tated text " +
                "on all sup­por­ted plat­forms.";
            // 将 HTML 软连字符替换为 Unicode 软连字符:
            str = str.Replace("­", "\u00AD");

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

            var tl = g.CreateTextLayout();
            tl.Append(str);
            tl.DefaultFormat.Font = StandardFonts.Times;
            tl.DefaultFormat.FontSize = 12;
            tl.MaxWidth = 72 * 3;

            // 默认情况下 0x002D(连字符减号)将用作连字符
            // 在软连字符 (0x00AD) 处换行时:
            tl.PerformLayout(true);

            var dy = tl.Lines[0].Height + 72 / 16;
            var rc = new RectangleF(72, 72 + dy, tl.MaxWidth.Value, 72 * 1.4F);

            g.DrawString("Default hyphenation:", tl.DefaultFormat, new PointF(rc.Left, rc.Top - dy));
            g.DrawTextLayout(tl, rc.Location);
            g.DrawRectangle(rc, Color.CornflowerBlue);

            rc.Offset(0, 72 * 2);
            // 这将避免连字符连接两个连续行:
            tl.LinesBetweenConsecutiveHyphens = 1;
            // 更改连字符选项需要 RecalculateGlyphs():
            tl.PerformLayout(true);
            g.DrawString("LinesBetweenConsecutiveHyphens: 1", tl.DefaultFormat, new PointF(rc.Left, rc.Top - dy));
            g.DrawTextLayout(tl, rc.Location);
            g.DrawRectangle(rc, Color.CornflowerBlue);

            rc.Offset(0, 72 * 2);
            // 重置之前的设置:
            tl.LinesBetweenConsecutiveHyphens = 0;
            // 完全防止单词连字符:
            tl.SoftHyphenReplacementCharCode = -1;
            // 更改连字符选项需要 RecalculateGlyphs():
            tl.PerformLayout(true);
            g.DrawString("SoftHyphenReplacementCharCode: -1", tl.DefaultFormat, new PointF(rc.Left, rc.Top - dy));
            g.DrawTextLayout(tl, rc.Location);
            g.DrawRectangle(rc, Color.CornflowerBlue);

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