CharacterStyles.vb
''
'' This code is part of GrapeCity Documents for Word samples.
'' Copyright (c) GrapeCity, Inc. All rights reserved.
''
Imports System.Drawing
Imports GrapeCity.Documents.Word

'' This sample demonstrates how to create, specify and apply
'' character styles.
'' It creates a paragraph of text, splits it into two runs,
'' then creates And applies two different character styles
'' to the two runs.
'' The part creating And splitting the paragraph Is based
'' on the FormatChars sample.
Public Class CharacterStyles
    Function CreateDocx() As GcWordDocument
        '' Get a sample paragraph of text:
        Dim lorem = LoremIpsumPar()
        '' The code below is similar to FormatChars code that creates and splits a paragraph:
        Dim doc = New GcWordDocument()
        Dim p = doc.Body.Sections.First.GetRange().Paragraphs.Add(lorem)
        Dim r = p.GetRange()
        Dim run = r.Runs.First
        Dim text = run.GetRange().Texts.First
        Dim tIpsum = text.Split(lorem.Length / 2)
        Dim rIpsum = run.Split(tIpsum, InsertLocation.Before)
        '' We now have two runs, create and apply different styles to each:
        ''
        '' Create a new char style "Lorem" for the first half:
        Dim sLorem = doc.Styles.Add("Lorem", StyleType.Character)
        sLorem.Font.Name = "Times New Roman"
        sLorem.Font.Size = 16
        sLorem.Font.Bold = True
        sLorem.Font.Italic = True
        sLorem.Font.Underline = Underline.Thick
        '' 'text' was split into 2 halves, but the head can be still
        '' accessed via the 'text' variable. We set its style to the
        '' newly created style (the style of the 2nd half remains default):
        text.ParentRun.Style = sLorem
        '' Create a new char style "Ipsum" for the 2nd half:
        Dim sIpsum = doc.Styles.Add("Ipsum", StyleType.Character)
        sIpsum.Font.Name = "Gabriola"
        sIpsum.Font.Size = 18
        sIpsum.Font.Color.RGB = Color.BlueViolet
        rIpsum.Style = sIpsum
        '' Done:
        Return doc
    End Function
End Class