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.

Style Resolution

Style resolution is a process of resolving document element formatting values that may come from a variety of sources: direct formatting, style, parent element formatting and style and default document formatting.

Following example demonstrates resolving formatting values on a font size formatting property.

Screenshot
Style Resolution Screenshot

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

using System;
using System.IO;
using System.Text;
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();

        // Default font size is 12
        document.DefaultCharacterFormat.Size = 12;

        CharacterStyle largeSize = new CharacterStyle("Large Font Size")
        {
            CharacterFormat = new CharacterFormat()
            {
                Size = 24
            }
        };

        // Runs with the following style will have font size 24
        document.Styles.Add(largeSize);

        // Fill section with test data.
        document.Sections.Add(
            new Section(document,
                new Paragraph(document,
                    new Run(document, "GemBox.Document component")
                    {
                        CharacterFormat = new CharacterFormat()
                        {
                            // Style
                            Style = largeSize
                        }
                    }),
                new Paragraph(document,
                    new Run(document, "GemBox.")
                    {
                        CharacterFormat = new CharacterFormat()
                        {
                            // Style and direct formatting (direct formatting has precedence over style)
                            Style = largeSize,
                            Size = 16
                        }
                    },
                    new Run(document, "Document")
                    {
                        CharacterFormat = new CharacterFormat()
                        {
                            // Direct formatting
                            Size = 16
                        }
                    },// Default style                
                    new Run(document, " is a .NET component that enables developers to read, write, convert and print document files (DOCX, DOC, PDF, HTML, XPS, RTF and TXT) from .NET applications in a simple and efficient way."))));

        // Fill section with results.
        Section section = new Section(document);
        Paragraph para = new Paragraph(document);
        section.Blocks.Add(para);

        foreach (Run run in document.GetChildElements(true, ElementType.Run))
        {
            para.Inlines.Add(
                new Run(document, "Font size: " + run.CharacterFormat.Size + " points, Text: '" + run.Text + "'"));

            para.Inlines.Add(new SpecialCharacter(document, SpecialCharacterType.LineBreak));
        }

        document.Sections.Add(section);

        document.Save("Style Resolution.docx");
    }
}
Imports System
Imports System.IO
Imports System.Text
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

        ' Default font size is 12
        document.DefaultCharacterFormat.Size = 12

        Dim largeSize As New CharacterStyle("Large Font Size") With {
            .CharacterFormat = New CharacterFormat() With {
                .Size = 24
            }
        }

        ' Runs with the following style will have font size 24
        document.Styles.Add(largeSize)

        ' Fill section with test data.
        document.Sections.Add(
            New Section(document,
                New Paragraph(document,
                    New Run(document, "GemBox.Document component") With {
                        .CharacterFormat = New CharacterFormat() With {
                            .Style = largeSize
                        }
                    }),
                New Paragraph(document,
                    New Run(document, "GemBox.") With {
                        .CharacterFormat = New CharacterFormat() With {
                            .Style = largeSize,
                            .Size = 16
                        }
                    },
                    New Run(document, "Document") With {
                        .CharacterFormat = New CharacterFormat() With {
                            .Size = 16
                        }
                    },
                    New Run(document, " is a .NET component that enables developers to read, write, convert and print document files (DOCX, DOC, PDF, HTML, XPS, RTF and TXT) from .NET applications in a simple and efficient way.")
                )))

        ' Fill section with results.
        Dim section As New Section(document)
        Dim para As New Paragraph(document)
        section.Blocks.Add(para)

        For Each run As Run In document.GetChildElements(True, ElementType.Run)

            para.Inlines.Add(
                New Run(document, "Font size: " & run.CharacterFormat.Size & " points, Text: '" & run.Text & "'"))

            para.Inlines.Add(
                New SpecialCharacter(document, SpecialCharacterType.LineBreak))

        Next

        document.Sections.Add(section)

        document.Save("Style Resolution.docx")

    End Sub

End Module

Check next sample.