Skip to main content
A newer version of this page is available. .

Text Formatting

  • 7 minutes to read

RichEditDocumentServer provides two ways to format document text.

  • Direct Formatting
    Allows you to change individual formatting attributes for a given range.

  • Document Styles
    Styles can change multiple attributes at the same time. Styles created in code are automatically available to end users. If you change the style settings, all linked document fragments will be updated automatically.

Both approaches described above are available in API.

Direct Formatting

Use the following properties to set the default character and paragraph formatting for a loaded or newly created document.

The code snippet below illustrates how to apply default formatting to a loaded document.

private void RichEditDocumentServer1_DocumentLoaded(object sender, EventArgs e)
{
    //Set the default font, size and forecolor
    richEditDocumentServer1.Document.DefaultCharacterProperties.FontName = "Arial";
    richEditDocumentServer1.Document.DefaultCharacterProperties.FontSize = 16;
    richEditDocumentServer1.Document.DefaultCharacterProperties.ForeColor = Color.Red;

    //Specify the default alignment
    richEditDocumentServer1.Document.DefaultParagraphProperties.Alignment = ParagraphAlignment.Center;
    richEditDocumentServer1.Document.AppendText("Document created at " + DateTime.Now.ToLongTimeString());
}

Use both character and paragraph formatting to modify the document title as shown in the image below.

TitleFormatted

Format Characters

The following members allow you to change character formatting for a given range.

Member Description
SubDocument.BeginUpdateCharacters Initiates the update session and provides access to CharacterProperties for the specified range.
CharacterProperties Holds the character formatting options.
SubDocument.EndUpdateCharacters Finalizes the character formatting update.

The code sample below uses this API to modify the text color and the font type.

//The target range is the first paragraph
DocumentRange range = document.Paragraphs[0].Range;

//Provide access to the character properties
CharacterProperties titleFormatting = document.BeginUpdateCharacters(range);

//Set the character size, font name and color
titleFormatting.FontSize = 20;
titleFormatting.FontName = "Helvetica";
titleFormatting.ForeColor = Color.DarkBlue;

document.EndUpdateCharacters(titleFormatting); 

Format Paragraphs

Use the following API to change the title’s alignment, left indent or any other paragraph option.

Member Description
SubDocument.BeginUpdateParagraphs Initiates the update session and provides access to the ParagraphProperties for the specified range.
ParagraphProperties Specifies the paragraph formatting properties.
SubDocument.EndUpdateParagraphs Finalizes the character formatting update.
//The target range is the first paragraph
DocumentRange titleParagraph = document.Paragraphs[0].Range;

//Provide access to the paragraph options 
ParagraphProperties titleParagraphFormatting = document.BeginUpdateParagraphs(titleParagraph);

//Set the paragraph alignment
titleParagraphFormatting.Alignment = ParagraphAlignment.Center;

//Set left indent at 0.5".
//Default unit is 1/300 of an inch (a document unit).
titleParagraphFormatting.LeftIndent = Units.InchesToDocumentsF(0.5f);
titleParagraphFormatting.SpacingAfter = Units.InchesToDocumentsF(0.3f);

//Set tab stop at 1.5"
TabInfoCollection tbiColl = titleParagraphFormatting.BeginUpdateTabs(true);
TabInfo tbi = new TabInfo();
tbi.Alignment = TabAlignmentType.Center;
tbi.Position = Units.InchesToDocumentsF(1.5f);
tbiColl.Add(tbi);
titleParagraphFormatting.EndUpdateTabs(tbiColl);

document.EndUpdateParagraphs(titleParagraphFormatting);

Document Styles

Word Processing Document API supports paragraph and character document styles. The character style can be used when only the character options need to be modified. Use the paragraph style to change both character (a font type, size, color, etc.) and paragraph (alignment, spacing before and after, etc.) attributes as in the current example.

The image below demonstrates chapter titles modified using the paragraph style.

IMAGE

RichEditDocumentServer doesn’t have any predefined document styles. Use members from the table below to create a new document style.

Member

Description

SubDocument.BeginUpdate

Opens the document for editing.

Document.CharacterStyles

Document.ParagraphStyles

Provides access to the CharacterStyleCollection

Provides access to the ParagraphStyleCollection.

CharacterStyleCollection.CreateNew

ParagraphStyleCollection.CreateNew

Creates a new CharacterStyle object.

Creates a new ParagraphStyle object.

CharacterStyle.Parent

ParagraphStyle.Parent

Specifies the base style for the created instance.

CharacterStyle.LinkedStyle

ParagraphStyle.LinkedStyle

Allows you to synchronize the character and paragraph styles to create a linked style.

CharacterStyleCollection.Add

ParagraphStyleCollection.Add

Adds the created style to the collection.

Paragraph.Style

Sets the style to the given paragraph.

CharacterProperties.Style

Specifies the style to the given character range.

SubDocument.EndUpdate

Finalizes style creation.

The code sample below demonstrates how to create a new paragraph style and apply it to every chapter.

//Open the document for editing
document.BeginUpdate();

//Create a new paragraph style instance
//and specify the required properties
ParagraphStyle chapterStyle = document.ParagraphStyles.CreateNew();
chapterStyle.Name = "MyTitleStyle";
chapterStyle.ForeColor = Color.SteelBlue;
chapterStyle.FontSize = 16;
chapterStyle.FontName = "Segoe UI Semilight";
chapterStyle.Alignment = ParagraphAlignment.Left;
chapterStyle.SpacingBefore = Units.InchesToDocumentsF(0.2f);
chapterStyle.SpacingAfter = Units.InchesToDocumentsF(0.2f);
chapterStyle.OutlineLevel = 2;

//Add the object to the document collection
document.ParagraphStyles.Add(chapterStyle);

//Finalize the editing
document.EndUpdate();

//Apply the created style to every chapter in the document 
for (int i = 0; i < document.Paragraphs.Count; i++)
{
    string var = document.GetText(document.Paragraphs[i].Range);
    if (var.Contains("Chapter "))
    {
        document.Paragraphs[i].Style = chapterStyle;
    }
}
return;

If the loaded document already has document styles, they are automatically added to the document’s CharacterStyleCollection or ParagraphStyleCollection and you can use them as shown in the code snippet below.

//Apply style to the paragraph
richEditDocumentServer1.Document.Paragraphs[1].Style = richEditDocumentServer1.Document.ParagraphStyles["Heading 2"];

//Apply style to the character range
DocumentRange range = richEditDocumentServer1.Document.Paragraphs[1].Range;
CharacterProperties rangeProperties = richEditDocumentServer1.Document.BeginUpdateCharacters(range);
rangeProperties.Style = richEditDocumentServer1.Document.CharacterStyles["Heading 2"];
richEditDocumentServer1.Document.EndUpdateCharacters(rangeProperties);

Tip

Load an empty document created in Microsoft® Word® using the Document.LoadDocument method in the RichEditDocumentServer.EmptyDocumentCreated event handler to fill the collection of a newly created document with the predefined styles.

Linked Styles

Use a linked style to format the document annotation. A linked style carries both character and paragraph formatting rules, but applies them according to the target object. If the style is applied to the Paragraph instance, both formatting options will be applied. Only character formatting is used for the DocumentRange instance.

The code sample below demonstrates how to synchronize a paragraph and character style to create a linked style.

ParagraphStyle annotationStyle = document.ParagraphStyles["Annotation"];

document.BeginUpdate();

//Create a new paragraph style
//and set the required settings
annotationStyle = document.ParagraphStyles.CreateNew();
annotationStyle.Name = "Annotation";
annotationStyle.Alignment = ParagraphAlignment.Right;
annotationStyle.LineSpacingMultiplier = 1.5f;
annotationStyle.FirstLineIndentType = ParagraphFirstLineIndent.Hanging;
annotationStyle.FirstLineIndent = 3;
document.ParagraphStyles.Add(annotationStyle);

//Create a new character style and link it
//to the custom paragraph style
CharacterStyle annotationCharStyle = document.CharacterStyles.CreateNew();
annotationCharStyle.Name = "AnnotationChar";
document.CharacterStyles.Add(annotationCharStyle);
annotationCharStyle.LinkedStyle = annotationStyle;

//Specify the style options
annotationCharStyle.Italic = true;
annotationCharStyle.FontSize = 12;
annotationCharStyle.FontName = "Segoe UI";
annotationCharStyle.ForeColor = Color.Gray;
document.EndUpdate();

//Apply the created style to the first paragraph of the annotation
document.Paragraphs[1].Style = annotationStyle;

//Apply the linked style to the range of the annotation's second paragraph
CharacterProperties annotationProperties = document.BeginUpdateCharacters(document.Paragraphs[2].Range);
annotationProperties.Style = annotationCharStyle;
document.EndUpdateCharacters(annotationProperties);

The image below illustrates the result of the code execution.

IMAGE

See Also