Working with Text Styles

Change the Font Color, Size and Highlight All the Text of RichText Node

This topic discusses changing the font color, size and highlighting all the text of a RichText node. This feature provides more in-depth control of OneNote to developers. Using this feature, developers can customize the font color, size and highlight text of any desired rich text node.

To change the font and color of a rich text node using Aspose.Note, please follow the steps below:

  1. Load OneNote document to a Document class.
  2. Access a RichText node whose font and colors are to be changed.
  3. Access TextStyle.
  4. Set the text’s font and color.

Change style of the paragraph

 1string dataDir = RunExamples.GetDataDir_Text();
 2
 3// Load the document into Aspose.Note.
 4Document document = new Document(dataDir + "Aspose.one");
 5// Get a particular RichText node
 6IList<RichText> richTextNodes = document.GetChildNodes<RichText>();
 7RichText richText = richTextNodes[0];
 8
 9foreach (TextStyle style in richText.Styles)
10{
11    // Set font color
12    style.FontColor = Color.Yellow;
13    // Set highlight color
14    style.Highlight = Color.Blue;
15    // Set font size
16    style.FontSize = 20;
17}         

Set default paragraph style settings

 1            var document = new Document();
 2            var page = new Page(document);
 3            var outline = new Outline(document);
 4            var outlineElem = new OutlineElement(document);
 5
 6            var text = new RichText(document)
 7                        {
 8                            Text = $"DefaultParagraphFontAndSize{Environment.NewLine}OnlyDefaultParagraphFont{Environment.NewLine}OnlyDefaultParagraphFontSize",
 9                            ParagraphStyle = new ParagraphStyle()
10                                                {
11                                                    FontName = "Courier New",
12                                                    FontSize = 20
13                                                }
14                        };
15
16            // Font and font size are from text.ParagraphStyle
17            text.Styles.Add(new TextStyle()
18                                    {
19                                        RunIndex = 27
20                                    });
21
22            // Only font is from text.ParagraphStyle
23            text.Styles.Add(new TextStyle()
24                                    {
25                                        FontSize = 14,
26                                        RunIndex = 53
27                                    });
28
29            // Only font size is from text.ParagraphStyle
30            text.Styles.Add(new TextStyle()
31                                    {
32                                        FontName = "Verdana",
33                                        RunIndex = text.Text.Length
34                                    });
35
36
37            outlineElem.AppendChildLast(text);
38            outline.AppendChildLast(outlineElem);
39            page.AppendChildLast(outline);
40            document.AppendChildLast(page);
41
42            document.Save(Path.Combine(RunExamples.GetDataDir_Text(), "SetDefaultParagraphStyle.one"));

Set proofing language for a text

 1            var document = new Document();
 2            var page = new Page(document);
 3            var outline = new Outline(document);
 4            var outlineElem = new OutlineElement(document);
 5
 6            var text = new RichText(document) { Text = "United States Germany China", ParagraphStyle = ParagraphStyle.Default };
 7            text.Styles.Add(new TextStyle()
 8                                {
 9                                    Language = CultureInfo.GetCultureInfo("en-US"),
10                                    RunIndex = 13
11                                });
12            text.Styles.Add(new TextStyle()
13                                {
14                                    Language = CultureInfo.GetCultureInfo("de-DE"),
15                                    RunIndex = 21
16                                });
17            text.Styles.Add(new TextStyle()
18                                {
19                                    Language = CultureInfo.GetCultureInfo("zh-CN"),
20                                    RunIndex = text.Text.Length
21                                });
22
23
24            outlineElem.AppendChildLast(text);
25            outline.AppendChildLast(outlineElem);
26            page.AppendChildLast(outline);
27            document.AppendChildLast(page);
28
29            document.Save(Path.Combine(RunExamples.GetDataDir_Text(), "SetProofingLanguageForText.one"));

Apply Dark mode style

The following code example demonstrates how to make OneNote document to look like in Dark mode.

 1// The path to the documents directory.
 2string dataDir = RunExamples.GetDataDir_Text();
 3
 4// Load the document into Aspose.Note.
 5Document doc = new Document(Path.Combine(dataDir, "Aspose.one"));
 6
 7foreach (var page in doc)
 8{
 9    page.BackgroundColor = Color.Black;
10}
11
12foreach (var node in doc.GetChildNodes<RichText>())
13{   
14    var c = node.ParagraphStyle.FontColor;
15    if (c.IsEmpty || Math.Abs(c.R - Color.Black.R) + Math.Abs(c.G - Color.Black.G) + Math.Abs(c.B - Color.Black.B) <= 30)
16    {
17        node.ParagraphStyle.FontColor = Color.White;
18    }
19}
20
21doc.Save(Path.Combine(dataDir, "AsposeDarkTheme.pdf"));
Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.