Character Formatting

GemBox.Document supports a variety of text formatting options like font name, size and color; on / off properties like bold, italic, underline, strikethrough, subscript, superscript and many others.

Following sample shows all available character formatting options.

Screenshot

Character Formatting 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        document.Sections.Add(
16            new Section(document,
17                new Paragraph(document,
18                    new Run(document, "All caps: "),
19                    new Run(document, "Capital letters")
20                    {
21                        CharacterFormat = new CharacterFormat()
22                        {
23                            AllCaps = true,
24                        }
25                    },
26                    new SpecialCharacter(document, SpecialCharacterType.LineBreak),
27                    new Run(document, "Bold: "),
28                    new Run(document, "Bold text")
29                    {
30                        CharacterFormat = new CharacterFormat()
31                        {
32                            Bold = true
33                        }
34                    },
35                    new SpecialCharacter(document, SpecialCharacterType.LineBreak),
36                    new Run(document, "Border: "),
37                    new Run(document, "Some text")
38                    {
39                        CharacterFormat = new CharacterFormat()
40                        {
41                            Border = new SingleBorder(BorderStyle.Single, Color.Black, 1)
42                        }
43                    },
44                    new SpecialCharacter(document, SpecialCharacterType.LineBreak),
45                    new Run(document, "Double strikethrough: "),
46                    new Run(document, "Some text")
47                    {
48                        CharacterFormat = new CharacterFormat()
49                        {
50                            DoubleStrikethrough = true
51                        }
52                    },
53                    new SpecialCharacter(document, SpecialCharacterType.LineBreak),
54                    new Run(document, "Font color: "),
55                    new Run(document, "Blue text")
56                    {
57                        CharacterFormat = new CharacterFormat()
58                        {
59                            FontColor = Color.Blue
60                        }
61                    },
62                    new SpecialCharacter(document, SpecialCharacterType.LineBreak),
63                    new Run(document, "Font name: "),
64                    new Run(document, "Arial Black")
65                    {
66                        CharacterFormat = new CharacterFormat()
67                        {
68                            FontName = "Arial Black"
69                        }
70                    },
71                    new SpecialCharacter(document, SpecialCharacterType.LineBreak),
72                    new Run(document, "Hidden: "),
73                    new Run(document, "Hidden text")
74                    {
75                        CharacterFormat = new CharacterFormat()
76                        {
77                            Hidden = true
78                        }
79                    },
80                    new SpecialCharacter(document, SpecialCharacterType.LineBreak),
81                    new Run(document, "Text highlight color: "),
82                    new Run(document, "Yellow background color")
83                    {
84                        CharacterFormat = new CharacterFormat()
85                        {
86                            HighlightColor = Color.Yellow
87                        }
88                    },
89                    new SpecialCharacter(document, SpecialCharacterType.LineBreak),
90                    new Run(document, "Italic: "),
91                    new Run(document, "Italic text")
92                    {
93                        CharacterFormat = new CharacterFormat()
94                        {
95                            Italic = true
96                        }
97                    },
98                    new SpecialCharacter(document, SpecialCharacterType.LineBreak),
99                    new Run(document, "Kerning: "),
100                    new Run(document, "Kerning is 15 points")
101                    {
102                        CharacterFormat = new CharacterFormat()
103                        {
104                            Kerning = 15
105                        }
106                    },
107                    new SpecialCharacter(document, SpecialCharacterType.LineBreak),
108                    new Run(document, "Position: "),
109                    new Run(document, "Position is 3 points")
110                    {
111                        CharacterFormat = new CharacterFormat()
112                        {
113                            Position = 3
114                        }
115                    },
116                    new SpecialCharacter(document, SpecialCharacterType.LineBreak),
117                    new Run(document, "Scale: "),
118                    new Run(document, "Scale is 125%")
119                    {
120                        CharacterFormat = new CharacterFormat()
121                        {
122                            Scaling = 125
123                        }
124                    },
125                    new SpecialCharacter(document, SpecialCharacterType.LineBreak),
126                    new Run(document, "Font size: "),
127                    new Run(document, "Font size is 14 points")
128                    {
129                        CharacterFormat = new CharacterFormat()
130                        {
131                            Size = 14
132                        }
133                    },
134                    new SpecialCharacter(document, SpecialCharacterType.LineBreak),
135                    new Run(document, "Small caps: "),
136                    new Run(document, "Some text")
137                    {
138                        CharacterFormat = new CharacterFormat()
139                        {
140                            SmallCaps = true
141                        }
142                    },
143                    new SpecialCharacter(document, SpecialCharacterType.LineBreak),
144                    new Run(document, "Spacing: "),
145                    new Run(document, "Spacing is 1 point")
146                    {
147                        CharacterFormat = new CharacterFormat()
148                        {
149                            Spacing = 1
150                        }
151                    },
152                    new SpecialCharacter(document, SpecialCharacterType.LineBreak),
153                    new Run(document, "Strikethrough: "),
154                    new Run(document, "Some text")
155                    {
156                        CharacterFormat = new CharacterFormat()
157                        {
158                            Strikethrough = true
159                        }
160                    },
161                    new SpecialCharacter(document, SpecialCharacterType.LineBreak),
162                    new Run(document, "Subscript: "),
163                    new Run(document, "Some text")
164                    {
165                        CharacterFormat = new CharacterFormat()
166                        {
167                            Subscript = true
168                        }
169                    },
170                    new SpecialCharacter(document, SpecialCharacterType.LineBreak),
171                    new Run(document, "Superscript: "),
172                    new Run(document, "Some text")
173                    {
174                        CharacterFormat = new CharacterFormat()
175                        {
176                            Superscript = true
177                        }
178                    },
179                    new SpecialCharacter(document, SpecialCharacterType.LineBreak),
180                    new Run(document, "Underline color: "),
181                    new Run(document, "Underline color is blue")
182                    {
183                        CharacterFormat = new CharacterFormat()
184                        {
185                            UnderlineColor = Color.Blue,
186                            UnderlineStyle = UnderlineType.Single
187                        }
188                    },
189                    new SpecialCharacter(document, SpecialCharacterType.LineBreak),
190                    new Run(document, "Underline style: "),
191                    new Run(document, "Underline style is double")
192                    {
193                        CharacterFormat = new CharacterFormat()
194                        {
195                            UnderlineStyle = UnderlineType.Double
196                        }
197                    },
198                    new SpecialCharacter(document, SpecialCharacterType.LineBreak),
199                    new Run(document, "Language: "),
200                    new Field(document, FieldType.Date)
201                    {
202                        CharacterFormat = new CharacterFormat()
203                        {
204                            Language = CultureInfo.GetCultureInfo("de-DE")
205                        }
206                    })));
207
208        document.Save("Character Formatting.docx");
209    }
210}
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        document.Sections.Add(
15            New Section(document,
16                New Paragraph(document,
17                    New Run(document, "All caps: "),
18                    New Run(document, "Capital letters") With {
19                        .CharacterFormat = New CharacterFormat() With {
20                            .AllCaps = True
21                        }
22                    },
23                    New SpecialCharacter(document, SpecialCharacterType.LineBreak),
24                    New Run(document, "Bold: "),
25                    New Run(document, "Bold text") With {
26                            .CharacterFormat = New CharacterFormat() With {
27                                .Bold = True
28                            }
29                        },
30                    New SpecialCharacter(document, SpecialCharacterType.LineBreak),
31                    New Run(document, "Border: "),
32                    New Run(document, "Some text") With {
33                            .CharacterFormat = New CharacterFormat() With {
34                                .Border = New SingleBorder(BorderStyle.Single, Color.Black, 1)
35                            }
36                        },
37                    New SpecialCharacter(document, SpecialCharacterType.LineBreak),
38                    New Run(document, "Double strikethrough: "),
39                    New Run(document, "Some text") With {
40                            .CharacterFormat = New CharacterFormat() With {
41                                .DoubleStrikethrough = True
42                            }
43                        },
44                    New SpecialCharacter(document, SpecialCharacterType.LineBreak),
45                    New Run(document, "Font color: "),
46                    New Run(document, "Blue text") With {
47                            .CharacterFormat = New CharacterFormat() With {
48                                .FontColor = Color.Blue
49                            }
50                        },
51                    New SpecialCharacter(document, SpecialCharacterType.LineBreak),
52                    New Run(document, "Font name: "),
53                    New Run(document, "Arial Black") With {
54                            .CharacterFormat = New CharacterFormat() With {
55                                .FontName = "Arial Black"
56                            }
57                        },
58                    New SpecialCharacter(document, SpecialCharacterType.LineBreak),
59                    New Run(document, "Hidden: "),
60                    New Run(document, "Hidden text") With {
61                            .CharacterFormat = New CharacterFormat() With {
62                                .Hidden = True
63                            }
64                        },
65                    New SpecialCharacter(document, SpecialCharacterType.LineBreak),
66                    New Run(document, "Text highlight color: "),
67                    New Run(document, "Yellow background color") With {
68                            .CharacterFormat = New CharacterFormat() With {
69                                .HighlightColor = Color.Yellow
70                            }
71                        },
72                    New SpecialCharacter(document, SpecialCharacterType.LineBreak),
73                    New Run(document, "Italic: "),
74                    New Run(document, "Italic text") With {
75                            .CharacterFormat = New CharacterFormat() With {
76                                .Italic = True
77                            }
78                        },
79                    New SpecialCharacter(document, SpecialCharacterType.LineBreak),
80                    New Run(document, "Kerning: "),
81                    New Run(document, "Kerning is 15 points") With {
82                            .CharacterFormat = New CharacterFormat() With {
83                                .Kerning = 15
84                            }
85                        },
86                    New SpecialCharacter(document, SpecialCharacterType.LineBreak),
87                    New Run(document, "Position: "),
88                    New Run(document, "Position is 3 points") With {
89                            .CharacterFormat = New CharacterFormat() With {
90                                .Position = 3
91                            }
92                        },
93                    New SpecialCharacter(document, SpecialCharacterType.LineBreak),
94                    New Run(document, "Scale: "),
95                    New Run(document, "Scale is 125%") With {
96                            .CharacterFormat = New CharacterFormat() With {
97                                .Scaling = 125
98                            }
99                        },
100                    New SpecialCharacter(document, SpecialCharacterType.LineBreak),
101                    New Run(document, "Font size: "),
102                    New Run(document, "Font size is 14 points") With {
103                            .CharacterFormat = New CharacterFormat() With {
104                                .Size = 14
105                            }
106                        },
107                    New SpecialCharacter(document, SpecialCharacterType.LineBreak),
108                    New Run(document, "Small caps: "),
109                    New Run(document, "Some text") With {
110                            .CharacterFormat = New CharacterFormat() With {
111                                .SmallCaps = True
112                            }
113                        },
114                    New SpecialCharacter(document, SpecialCharacterType.LineBreak),
115                    New Run(document, "Spacing: "),
116                    New Run(document, "Spacing is 1 point") With {
117                            .CharacterFormat = New CharacterFormat() With {
118                                .Spacing = 1
119                            }
120                        },
121                    New SpecialCharacter(document, SpecialCharacterType.LineBreak),
122                    New Run(document, "Strikethrough: "),
123                    New Run(document, "Some text") With {
124                            .CharacterFormat = New CharacterFormat() With {
125                                .Strikethrough = True
126                            }
127                        },
128                    New SpecialCharacter(document, SpecialCharacterType.LineBreak),
129                    New Run(document, "Subscript: "),
130                    New Run(document, "Some text") With {
131                            .CharacterFormat = New CharacterFormat() With {
132                                .Subscript = True
133                            }
134                        },
135                    New SpecialCharacter(document, SpecialCharacterType.LineBreak),
136                    New Run(document, "Superscript: "),
137                    New Run(document, "Some text") With {
138                            .CharacterFormat = New CharacterFormat() With {
139                                .Superscript = True
140                            }
141                        },
142                    New SpecialCharacter(document, SpecialCharacterType.LineBreak),
143                    New Run(document, "Underline color: "),
144                    New Run(document, "Underline color is blue") With {
145                            .CharacterFormat = New CharacterFormat() With {
146                                .UnderlineColor = Color.Blue,
147                                .UnderlineStyle = UnderlineType.Single
148                            }
149                        },
150                    New SpecialCharacter(document, SpecialCharacterType.LineBreak),
151                    New Run(document, "Underline style: "),
152                    New Run(document, "Underline style is double") With {
153                            .CharacterFormat = New CharacterFormat() With {
154                                .UnderlineStyle = UnderlineType.Double
155                            }
156                        },
157                     New SpecialCharacter(document, SpecialCharacterType.LineBreak),
158                     New Run(document, "Language: "),
159                     New Field(document, FieldType.Date) With {
160                            .CharacterFormat = New CharacterFormat() With {
161                                .Language = CultureInfo.GetCultureInfo("de-DE")
162                            }
163                        })))
164
165        document.Save("Character Formatting.docx")
166
167    End Sub
168
169End Module

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