CharacterStyles.cs
//
// This code is part of Document Solutions for Word demos.
// Copyright (c) MESCIUS inc. All rights reserved.
//
using System;
using System.IO;
using System.Drawing;
using GrapeCity.Documents.Word;
using Range = GrapeCity.Documents.Word.Range;

namespace DsWordWeb.Demos
{
    // 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 CharacterFormatting sample.
    public class CharacterStyles
    {
        public GcWordDocument CreateDocx()
        {
            // Get a sample paragraph of text:
            var lorem = Util.LoremIpsumPar();
            // The code below is similar to FormatChars code that creates and splits a paragraph:
            GcWordDocument doc = new GcWordDocument();
            Paragraph p = doc.Body.Paragraphs.Add(lorem);
            Range r = p.GetRange();
            Run run = r.Runs.First;
            Text text = run.GetRange().Texts.First;
            Text tIpsum = text.Split(lorem.Length / 2);
            Run 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:
            Style 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:
            Style 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;
        }
    }
}