Style Resolution in Word Files with C# and VB.NET
Style resolution is a process of resolving a document element's formatting values that may come from a variety of sources, like from direct formatting or style, a parent element's direct formatting or style, the document's default formatting, etc.
GemBox.Document calculates or resolves formatting properties in a way that they match the ones in the document processing application like Microsoft Word as closely as possible.
The following example shows how the font size formatting property is resolved in Word documents in C# and VB.NET.

using System.Linq;
using GemBox.Document;
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 System.Linq
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()
' 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 list specifies the order in which the values of a document element's formatting properties are resolved:
- Element's direct formatting.
- Element's style.
- Ancestor element's direct formatting of the same formatting type.
- Ancestor element's style of the same style type.
- Document's default formatting.
As an example, let's say that we are resolving a font size for a run element, which is defined directly on the paragraph level. In that case the resolution will end on step 3.