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 sample demonstrates resolving formatting values on a font size formatting property.

Screenshot

Style Resolution Screenshot

See the full code below, use RUN EXAMPLE to execute.


1using System;
2using System.IO;
3using System.Text;
4using GemBox.Document;
5
6class Sample
7{
8    [STAThread]
9    static void Main(string[] args)
10    {
11        // If using Professional version, put your serial key below.
12        ComponentInfo.SetLicense("FREE-LIMITED-KEY");
13
14        DocumentModel document = new DocumentModel();
15
16        // Default font size is 12
17        document.DefaultCharacterFormat.Size = 12;
18
19        CharacterStyle largeSize = new CharacterStyle("Large Font Size")
20        {
21            CharacterFormat = new CharacterFormat()
22            {
23                Size = 24
24            }
25        };
26
27        // Runs with the following style will have font size 24
28        document.Styles.Add(largeSize);
29
30        // Fill section with test data.
31        document.Sections.Add(
32            new Section(document,
33                new Paragraph(document,
34                    new Run(document, "GemBox.Document component")
35                    {
36                        CharacterFormat = new CharacterFormat()
37                        {
38                            // Style
39                            Style = largeSize
40                        }
41                    }),
42                new Paragraph(document,
43                    new Run(document, "GemBox.")
44                    {
45                        CharacterFormat = new CharacterFormat()
46                        {
47                            // Style and direct formatting (direct formatting has precedence over style)
48                            Style = largeSize,
49                            Size = 16
50                        }
51                    },
52                    new Run(document, "Document")
53                    {
54                        CharacterFormat = new CharacterFormat()
55                        {
56                            // Direct formatting
57                            Size = 16
58                        }
59                    },// Default style                
60                    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."))));
61
62        // Fill section with results.
63        Section section = new Section(document);
64        Paragraph para = new Paragraph(document);
65        section.Blocks.Add(para);
66
67        foreach (Run run in document.GetChildElements(true, ElementType.Run))
68        {
69            para.Inlines.Add(
70                new Run(document, "Font size: " + run.CharacterFormat.Size + " points, Text: '" + run.Text + "'"));
71
72            para.Inlines.Add(new SpecialCharacter(document, SpecialCharacterType.LineBreak));
73        }
74
75        document.Sections.Add(section);
76
77        document.Save("Style Resolution.docx");
78    }
79}
1Imports System
2Imports System.IO
3Imports System.Text
4Imports GemBox.Document
5
6Module Samples
7
8    Sub Main()
9
10        ' If using Professional version, put your serial key below.
11        ComponentInfo.SetLicense("FREE-LIMITED-KEY")
12
13        Dim document As DocumentModel = New DocumentModel
14
15        ' Default font size is 12
16        document.DefaultCharacterFormat.Size = 12
17
18        Dim largeSize As New CharacterStyle("Large Font Size") With {
19            .CharacterFormat = New CharacterFormat() With {
20                .Size = 24
21            }
22        }
23
24        ' Runs with the following style will have font size 24
25        document.Styles.Add(largeSize)
26
27        ' Fill section with test data.
28        document.Sections.Add(
29            New Section(document,
30                New Paragraph(document,
31                    New Run(document, "GemBox.Document component") With {
32                        .CharacterFormat = New CharacterFormat() With {
33                            .Style = largeSize
34                        }
35                    }),
36                New Paragraph(document,
37                    New Run(document, "GemBox.") With {
38                        .CharacterFormat = New CharacterFormat() With {
39                            .Style = largeSize,
40                            .Size = 16
41                        }
42                    },
43                    New Run(document, "Document") With {
44                        .CharacterFormat = New CharacterFormat() With {
45                            .Size = 16
46                        }
47                    },
48                    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.")
49                )))
50
51        ' Fill section with results.
52        Dim section As New Section(document)
53        Dim para As New Paragraph(document)
54        section.Blocks.Add(para)
55
56        For Each run As Run In document.GetChildElements(True, ElementType.Run)
57
58            para.Inlines.Add(
59                New Run(document, "Font size: " & run.CharacterFormat.Size & " points, Text: '" & run.Text & "'"))
60
61            para.Inlines.Add(
62                New SpecialCharacter(document, SpecialCharacterType.LineBreak))
63
64        Next
65
66        document.Sections.Add(section)
67
68        document.Save("Style Resolution.docx")
69
70    End Sub
71
72End Module

Check next sample or find out more about GemBox.Document and GemBox Software.