SubSuperScript.vb
'' 完毕:
Imports System.IO
Imports System.Drawing
Imports System.Text.RegularExpressions
Imports GrapeCity.Documents.Drawing
Imports GrapeCity.Documents.Pdf
Imports GrapeCity.Documents.Text
Imports GCTEXT = GrapeCity.Documents.Text
Imports GCDRAW = GrapeCity.Documents.Drawing

'' 此示例演示如何呈现下标和上标文本。
Public Class SubSuperScript
    Sub CreatePDF(ByVal stream As Stream)
        Dim doc = New GcPdfDocument()
        Dim page = doc.NewPage()
        Dim g = page.Graphics

        Dim rc = Util.AddNote(
            "TextFormat.Subscript 和 TextFormat.Superscript 属性的演示。" +
            "",
            page)

        '' 获取随机的“传说本身”段落:
        Dim para = Util.LoremIpsum(1, 18, 20, 20, 20)

        '' 将段落拆分为“lorem”、“ipsum”和其他所有内容:
        Const subs = "lorem"
        Const super = "ipsum"
        Dim frags = Regex.Split(para, $"({subs})|({super})")

        Dim fnt = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "times.ttf"))

        '' 创建下标和上标的文本格式:
        Dim tfSub = New TextFormat() With {.Font = fnt, .FontSize = 12, .Subscript = True}
        Dim tfSuper = New TextFormat(tfSub) With {.Subscript = False, .Superscript = True}

        '' 使用“lorem”和“ipsum”的特殊格式将文本添加到 TextLayout:
        Dim tl = g.CreateTextLayout()
        tl.DefaultFormat.Font = fnt
        tl.DefaultFormat.FontSize = 12
        For Each frag In frags
            If (frag = subs) Then
                tl.Append(frag, tfSub)
            ElseIf (frag = super) Then
                tl.Append(frag, tfSuper)
            Else
                tl.Append(frag)
            End If
        Next

        '' 设置布局属性并渲染文本:
        tl.MaxWidth = page.Size.Width
        tl.MaxHeight = page.Size.Height - rc.Height
        tl.MarginAll = 72
        tl.MarginTop = rc.Bottom + 36
        tl.PerformLayout(True)
        g.DrawTextLayout(tl, PointF.Empty)

        '' 完毕:
        doc.Save(stream)
    End Sub
End Class