SubSuperScript.vb
''
'' This code is part of Document Solutions for Word demos.
'' Copyright (c) MESCIUS inc. All rights reserved.
''
Imports System.IO
Imports System.Drawing
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text.RegularExpressions
Imports GrapeCity.Documents.Word

'' This sample shows how to render subscript And superscript text.
Public Class SubSuperScript
    Public Function CreateDocx() As GcWordDocument
        Dim doc = New GcWordDocument()
        Dim sec = doc.Body.Sections.First
        Dim para = sec.GetRange().Paragraphs.Add()

        '' Get a random 'lorem ipsum' paragraph:
        Dim text = Util.LoremIpsumPar(18, 20, 20, 20)

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

        '' Create subscript And superscript styles:
        Dim sSub = doc.Styles.Add("My Subscript", StyleType.Character)
        sSub.Font.VerticalPosition = VerticalTextPosition.Subscript
        Dim sSup = doc.Styles.Add("My Superscript", StyleType.Character)
        sSup.Font.VerticalPosition = VerticalTextPosition.Superscript

        '' Add text to a TextLayout using special formats for 'lorem' and 'ipsum':
        For Each frag In frags
            If frag = subs Then
                para.GetRange().Runs.Add(frag, sSub)
            ElseIf frag = super Then
                para.GetRange().Runs.Add(frag, sSup)
            Else
                para.GetRange().Runs.Add(frag)
            End If
        Next

        '' Done
        Return doc
    End Function
End Class