Style Resolution

Style resolution is a process of resolving a document element's formatting values that may come from a variety of sources, like from a direct formatting or style, parent element's direct formatting or style, document's default formatting, etc.

GemBox.Document calculates or resolves formatting properties, so that you have the same formatting for any element as when it's rendered by a document processing application like Microsoft Word.

The following example shows how the font size formatting property is resolved in Word documents.

Word document with elements using direct formatting and style
Screenshot of Word document different elements formatting
using System.Linq;
using GemBox.Document;

class Program
{
    static void Main()
    {
        // If using 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 System.Linq
Imports GemBox.Document

Module Program

    Sub Main()

        ' If using 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

The following steps describe how the values of a document element's formatting properties are resolved, the value is taken from the first occurrence of it in the chain of style and formatting inheritance:

  1. Value is retrieved from the element's direct formatting.
  2. Value is retrieved from the element's style.
  3. Value is retrieved from the ancestor element's direct formatting of the same formatting type.
  4. Value is retrieved from the ancestor element's style of the same style type.
  5. Value is retrieved from the document's default formatting.

Want more?

Next example GitHub

Check the next example or select an example from the menu. You can also download our examples from the GitHub.


Like it?

Download Buy

If you want to try the GemBox.Document yourself, you can download the free version. It delivers the same performance and set of features as the professional version, but with some operations limited. To remove the limitation, you need to purchase a license.