Word Styles

The following example shows how to create built-in and custom Word styles using the GemBox.Document component in your C# and VB.NET applications.

using GemBox.Document;

class Program
{
    static void Main()
    {
        // If using the 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.
        var titleStyle = (ParagraphStyle)Style.CreateStyle(StyleTemplateType.Title, document);

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

        // To use styles, we first must add them to the document.
        document.Styles.Add(titleStyle);
        document.Styles.Add(emphasisStyle);

        // Or we can use a utility method to get a built-in style or create and add a new one in a single statement.
        var strongStyle = (CharacterStyle)document.Styles.GetOrAdd(StyleTemplateType.Strong);

        document.Sections.Add(
            new Section(document,
                new Paragraph(document, "Title (Title style)") { ParagraphFormat = { Style = titleStyle } },
                new Paragraph(document,
                    new Run(document, "Text is written using Emphasis style.") { CharacterFormat = { Style = emphasisStyle } },
                    new SpecialCharacter(document, SpecialCharacterType.LineBreak),
                    new Run(document, "Text is written using Strong style.") { CharacterFormat = { Style = strongStyle } })));

        document.Save("Styles.%OutputFileType%");
    }
}
Imports GemBox.Document

Module Program

    Sub Main()

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

        Dim document As New DocumentModel()

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

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

        ' To use styles, we first must add them to the document.
        document.Styles.Add(titleStyle)
        document.Styles.Add(emphasisStyle)

        ' Or we can use a utility method to get a built-in style or create and add a new one in a single statement.
        Dim strongStyle = DirectCast(document.Styles.GetOrAdd(StyleTemplateType.Strong), CharacterStyle)

        document.Sections.Add(
            New Section(document,
                New Paragraph(document, "Title (Title style)") With {.ParagraphFormat = New ParagraphFormat() With {.Style = titleStyle}},
                New Paragraph(document,
                    New Run(document, "Text is written using Emphasis style.") With {.CharacterFormat = New CharacterFormat() With {.Style = emphasisStyle}},
                    New SpecialCharacter(document, SpecialCharacterType.LineBreak),
                    New Run(document, "Text is written using Strong style.") With {.CharacterFormat = New CharacterFormat() With {.Style = strongStyle}})))

        document.Save("Styles.%OutputFileType%")

    End Sub
End Module
Word document with built-in and custom styles
Screenshot of Word file with built-in and custom styles

You can create a new style or modify an existing one to define your desired formatting and apply it consistently throughout your Word document. This approach not only streamlines the formatting process but also reduces the file size compared to using direct formatting for each section.

If you plan to use a template document, consider customizing or creating new styles and reusing them with GemBox.Document so that fewer lines of code are needed to format your content.

There are four styles:

On our Formattings and Styles help page, you can learn more about formatting properties of each style type.

By using styles, you can easily identify different parts of a document and extract those parts for further processing. For instance, you could retrieve only the titles, references, or footnotes from your document by searching for elements that use some targeted style.

Style Resolution

The following example shows how the style resolution in GemBox.Document works by resolving the font size in a Word document.

using GemBox.Document;
using System.Linq;

class Program
{
    static void Main()
    {
        // If using the Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        var document = new DocumentModel();

        // Document's default font size is 8pt.
        document.DefaultCharacterFormat.Size = 8;

        // Style's font size is 24pt.
        var largeFont = new CharacterStyle("Large Font") { CharacterFormat = { Size = 24 } };
        document.Styles.Add(largeFont);

        var section = new Section(document);
        document.Sections.Add(section);

        var paragraph = new Paragraph(document,
            new Run(document, "Large text that has 'Large Font' style.")
            {
                CharacterFormat = { Style = largeFont }
            },
            new SpecialCharacter(document, SpecialCharacterType.LineBreak),
            new Run(document, "Medium text that has both style and direct formatting; direct formatting has precedence over style's formatting.")
            {
                CharacterFormat = { Style = largeFont, Size = 12 }
            },
            new SpecialCharacter(document, SpecialCharacterType.LineBreak),
            new Run(document, "Small text that uses document's default formatting."));

        section.Blocks.Add(paragraph);

        // Write elements resolved font size values.
        foreach (Run run in document.GetChildElements(true, ElementType.Run).ToArray())
            section.Blocks.Add(new Paragraph(document, $"Font size: {run.CharacterFormat.Size} points. Text: {run.Text}"));

        document.Save("Style Resolution.%OutputFileType%");
    }
}
Imports GemBox.Document
Imports System.Linq

Module Program

    Sub Main()

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

        Dim document As New DocumentModel()

        ' Document's default font size is 8pt.
        document.DefaultCharacterFormat.Size = 8

        ' Style's font size is 24pt.
        Dim largeFont As New CharacterStyle("Large Font") With {.CharacterFormat = New CharacterFormat() With {.Size = 24}}
        document.Styles.Add(largeFont)

        Dim section As New Section(document)
        document.Sections.Add(section)

        Dim paragraph As New Paragraph(document,
            New Run(document, "Large text that has 'Large Font' style.") With
            {
                .CharacterFormat = New CharacterFormat() With {.Style = largeFont}
            },
            New SpecialCharacter(document, SpecialCharacterType.LineBreak),
            New Run(document, "Medium text that has both style and direct formatting; direct formatting has precedence over style's formatting.") With
            {
                .CharacterFormat = New CharacterFormat() With {.Style = largeFont, .Size = 12}
            },
            New SpecialCharacter(document, SpecialCharacterType.LineBreak),
            New Run(document, "Small text that uses document's default formatting."))

        section.Blocks.Add(paragraph)

        ' Write elements resolved font size values.
        For Each run As Run In document.GetChildElements(True, ElementType.Run).ToArray()
            section.Blocks.Add(New Paragraph(document, $"Font size: {run.CharacterFormat.Size} points. Text: {run.Text}"))
        Next

        document.Save("Style Resolution.%OutputFileType%")

    End Sub
End Module
Word document with elements using direct formatting and style
Screenshot of Word document different elements formatting

GemBox.Document calculates or resolves formatting properties to match the ones in the document processing application like Microsoft Word as closely as possible.

The following list specifies the order in which the values of a document element's formatting properties are resolved:

  1. Element's direct formatting.
  2. Element's style.
  3. Ancestor element's direct formatting of the same formatting type.
  4. Ancestor element's style of the same style type.
  5. Document's default formatting.

For example, let's say that we are resolving a font size for a run element defined directly on the paragraph level. In that case, the resolution will end in step 3.

See also


Next steps

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. How about testing it today?

Download Buy