SubSuperScript.vb
''
'' This code is part of GrapeCity Documents for PDF samples.
'' Copyright (c) GrapeCity, Inc. All rights reserved.
''
Imports System.IO
Imports System.Drawing
Imports System.Text.RegularExpressions
Imports GrapeCity.Documents.Drawing
Imports GrapeCity.Documents.Pdf
Imports GrapeCity.Documents.Text

'' This sample shows how to render subscript And superscript text.
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(
            "Demo of the TextFormat.Subscript and TextFormat.Superscript properties. " +
            "We draw a random 'lorem ipsum' paragraph, rendering all instances of 'lorem' as subscript, " +
            "and all instances of 'ipsum' as superscript.",
            page)

        '' Get a random 'lorem ipsum' paragraph:
        Dim para = Util.LoremIpsum(1, 18, 20, 20, 20)

        '' Split the paragraph into 'lorem', 'ipsum' and everything else:
        Const subs = "lorem"
        Const super = "ipsum"
        Dim frags = Regex.Split(para, $"({subs})|({super})")

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

        '' Create text formats for subscript And superscript:
        Dim tfSub = New TextFormat() With {.Font = fnt, .FontSize = 12, .Subscript = True}
        Dim tfSuper = New TextFormat(tfSub) With {.Subscript = False, .Superscript = True}

        '' Add text to a TextLayout using special formats for 'lorem' and 'ipsum':
        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

        '' Set layout properties And render the text
        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)

        '' Done
        doc.Save(stream)
    End Sub
End Class