Skip to main content

How To: Create New Character Style

The following example illustrates how to create a character style in code.

The following code snippet creates a new character style, specifies style settings, adds it to the document style collection and applies the style to the specified range.

View Example

richEditControl.Document.LoadDocument("Grimm.docx");

// Create a new style
CharacterStyle cstyle = richEditControl.Document.CharacterStyles.CreateNew();


// Specify style parameters
cstyle.Name = "MyCStyle";
cstyle.Parent = richEditControl.Document.CharacterStyles["Default Paragraph Font"];
cstyle.ForeColor = System.Drawing.Color.DarkOrange;
cstyle.Strikeout = StrikeoutType.Double;
cstyle.FontName = "Verdana";

// Add style to the collection
document.CharacterStyles.Add(cstyle);

// Obtain the second document paragraph
DocumentRange myRange = richEditControl.Document.Paragraphs[0].Range;

// Apply created style to the paragraph
CharacterProperties charProps =
    richEditControl.Document.BeginUpdateCharacters(myRange);
charProps.Style = cstyle;
richEditControl.Document.EndUpdateCharacters(charProps);