GemBox.Document is a .NET component that enables you to read, write, edit, convert, and print document files from your .NET applications using one simple API.

Styles

Styles provide a way to format your document in a consistent way so that when you change formatting options on a style, all document elements referencing that style will be changed.

Following example demonstrates how to create character and paragraph styles and assign them to paragraph and runs.

Screenshot
Styles Screenshot

See the full code below, use Run Example to execute.

using System;
using System.Globalization;
using GemBox.Document;

class Sample
{
    [STAThread]
    static void Main(string[] args)
    {
        // If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        DocumentModel document = new DocumentModel();

        // Built-in styles can be created using Style.CreateStyle() method.
        ParagraphStyle title = (ParagraphStyle)Style.CreateStyle(StyleTemplateType.Title, document);

        // We can also create our own (custom) styles.
        CharacterStyle emphasis = new CharacterStyle("Emphasis");
        emphasis.CharacterFormat.Italic = true;

        // First add style to the document, then use it.
        document.Styles.Add(title);
        document.Styles.Add(emphasis);

        document.Sections.Add(
            new Section(document,
                new Paragraph(document, "Title (Title style)")
                {
                    ParagraphFormat = new ParagraphFormat()
                    {
                        Style = title
                    }
                },
                new Paragraph(document,
                    new Run(document, "Text is written using Strong style.")
                    {
                        CharacterFormat = new CharacterFormat()
                        {
                            // Or we can use utility method.
                            Style = (CharacterStyle)document.Styles.GetOrAdd(StyleTemplateType.Strong)
                        }
                    },
                    new SpecialCharacter(document, SpecialCharacterType.LineBreak),
                    new Run(document, "Text is written using Emphasis style.")
                    {
                        CharacterFormat = new CharacterFormat()
                        {
                            Style = emphasis
                        }
                    })));

        document.Save("Styles.docx");
    }
}
Imports System
Imports System.Globalization
Imports GemBox.Document

Module Samples

    Sub Main()

        ' If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY")

        Dim document As DocumentModel = New DocumentModel

        ' Built-in styles can be created using Style.CreateStyle() method.
        Dim title = DirectCast(Style.CreateStyle(StyleTemplateType.Title, document), ParagraphStyle)

        ' We can also create our own (custom) styles.
        Dim emphasis As New CharacterStyle("Emphasis")
        emphasis.CharacterFormat.Italic = True

        ' First add style to the document, then use it.
        document.Styles.Add(title)
        document.Styles.Add(emphasis)

        document.Sections.Add(
            New Section(document,
                New Paragraph(document, "Title (Title style)") With {
                    .ParagraphFormat = New ParagraphFormat() With {
                        .Style = title
                    }
                },
                New Paragraph(document,
                    New Run(document, "Text is written using Strong style.") With {
                        .CharacterFormat = New CharacterFormat() With {
                            .Style = document.Styles.GetOrAdd(StyleTemplateType.Strong)
                        }
                    },
                    New SpecialCharacter(document, SpecialCharacterType.LineBreak),
                    New Run(document, "Text is written using Emphasis style.") With {
                        .CharacterFormat = New CharacterFormat() With {
                            .Style = emphasis
                        }
                    })))

        document.Save("Styles.docx")

    End Sub

End Module

Check next sample.