Styles

Styles provide a way to format your document in a consistent way so that when you change formatting options on a style, all document elements referencing that style will be changed.

Following sample demonstrates how to create character and paragraph styles and assign them to paragraph and runs.

Screenshot

Styles Screenshot

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


1using System;
2using System.Globalization;
3using GemBox.Document;
4
5class Sample
6{
7    [STAThread]
8    static void Main(string[] args)
9    {
10        // If using Professional version, put your serial key below.
11        ComponentInfo.SetLicense("FREE-LIMITED-KEY");
12
13        DocumentModel document = new DocumentModel();
14
15        // Built-in styles can be created using Style.CreateStyle() method.
16        ParagraphStyle title = (ParagraphStyle)Style.CreateStyle(StyleTemplateType.Title, document);
17
18        // We can also create our own (custom) styles.
19        CharacterStyle emphasis = new CharacterStyle("Emphasis");
20        emphasis.CharacterFormat.Italic = true;
21
22        // First add style to the document, then use it.
23        document.Styles.Add(title);
24        document.Styles.Add(emphasis);
25
26        document.Sections.Add(
27            new Section(document,
28                new Paragraph(document, "Title (Title style)")
29                {
30                    ParagraphFormat = new ParagraphFormat()
31                    {
32                        Style = title
33                    }
34                },
35                new Paragraph(document,
36                    new Run(document, "Text is written using Strong style.")
37                    {
38                        CharacterFormat = new CharacterFormat()
39                        {
40                            // Or we can use utility method.
41                            Style = (CharacterStyle)document.Styles.GetOrAdd(StyleTemplateType.Strong)
42                        }
43                    },
44                    new SpecialCharacter(document, SpecialCharacterType.LineBreak),
45                    new Run(document, "Text is written using Emphasis style.")
46                    {
47                        CharacterFormat = new CharacterFormat()
48                        {
49                            Style = emphasis
50                        }
51                    })));
52
53        document.Save("Styles.docx");
54    }
55}
1Imports System
2Imports System.Globalization
3Imports GemBox.Document
4
5Module Samples
6
7    Sub Main()
8
9        ' If using Professional version, put your serial key below.
10        ComponentInfo.SetLicense("FREE-LIMITED-KEY")
11
12        Dim document As DocumentModel = New DocumentModel
13
14        ' Built-in styles can be created using Style.CreateStyle() method.
15        Dim title = DirectCast(Style.CreateStyle(StyleTemplateType.Title, document), ParagraphStyle)
16
17        ' We can also create our own (custom) styles.
18        Dim emphasis As New CharacterStyle("Emphasis")
19        emphasis.CharacterFormat.Italic = True
20
21        ' First add style to the document, then use it.
22        document.Styles.Add(title)
23        document.Styles.Add(emphasis)
24
25        document.Sections.Add(
26            New Section(document,
27                New Paragraph(document, "Title (Title style)") With {
28                    .ParagraphFormat = New ParagraphFormat() With {
29                        .Style = title
30                    }
31                },
32                New Paragraph(document,
33                    New Run(document, "Text is written using Strong style.") With {
34                        .CharacterFormat = New CharacterFormat() With {
35                            .Style = document.Styles.GetOrAdd(StyleTemplateType.Strong)
36                        }
37                    },
38                    New SpecialCharacter(document, SpecialCharacterType.LineBreak),
39                    New Run(document, "Text is written using Emphasis style.") With {
40                        .CharacterFormat = New CharacterFormat() With {
41                            .Style = emphasis
42                        }
43                    })))
44
45        document.Save("Styles.docx")
46
47    End Sub
48
49End Module

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