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.

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:
- Value is retrieved from the element's direct formatting.
- Value is retrieved from the element's style.
- Value is retrieved from the ancestor element's direct formatting of the same formatting type.
- Value is retrieved from the ancestor element's style of the same style type.
- Value is retrieved from the document's default formatting.
Want more?
Like it?
Published: December 13, 2018 | Modified: December 16, 2019 | Author: Josip Kremenic