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.
Changing the Font Color, Size & Highlighting the Text
To change the font and color of a rich text node using Aspose.Note, please follow the steps below:
- Load OneNote document to a Document class.
- Access a RichText node whose font and colors are to be changed.
- Access TextStyle.
- Set the text’s font and color.
The output of the code snippet below is shown in the figure.
Change style of the paragraph.
1String dataDir = Utils.getSharedDataDir(ChangeTextStyle.class) + "styles/";
2
3// Load the document into Aspose.Note
4Document document = new Document(dataDir + "Sample1.one");
5
6// Get a particular RichText node
7List<RichText> richTextNodes = document.getChildNodes(RichText.class);
8RichText richText = richTextNodes.get(0);
9
10for (TextStyle style : richText.getStyles()) {
11 // Set font color
12 style.setFontColor(Color.yellow);
13 // Set highlight color
14 style.setHighlight(Color.blue);
15 // Set font size
16 style.setFontSize(20);
17}
18
19document.save(dataDir + "ChangeTextStyle_out.pdf");
20System.out.printf("File saved: %s\n", dataDir + "ChangeTextStyle_out.pdf");
Set default paragraph style settings.
1 String dataDir = Paths.get(Utils.getSharedDataDir(LoadPasswordProtectedOneNoteDoc.class), "styles").toString();
2
3 Document document = new Document();
4 Page page = new Page(document);
5 Outline outline = new Outline(document);
6 OutlineElement outlineElem = new OutlineElement(document);
7
8 StringBuilder builder = new StringBuilder();
9 builder.append("DefaultParagraphFontAndSize");
10 builder.append(System.lineSeparator());
11 builder.append("OnlyDefaultParagraphFont");
12 builder.append(System.lineSeparator());
13 builder.append("OnlyDefaultParagraphFontSize");
14
15 ParagraphStyle defaultStyle = new ParagraphStyle();
16 defaultStyle.setFontName("Courier New");
17 defaultStyle.setFontSize(20);
18
19 RichText text = new RichText(document);
20 text.setText(builder.toString());
21 text.setParagraphStyle(defaultStyle);
22
23 // Font and font size are from text.ParagraphStyle
24 TextStyle style = new TextStyle();
25 style.setRunIndex(27);
26 text.getStyles().addItem(style);
27
28 // Only font is from text.ParagraphStyle
29 style = new TextStyle();
30 style.setFontSize(14);
31 style.setRunIndex(53);
32 text.getStyles().addItem(style);
33
34 // Only font size is from text.ParagraphStyle
35 style = new TextStyle();
36 style.setFontName("Verdana");
37 style.setRunIndex(text.getText().length());
38 text.getStyles().addItem(style);
39
40
41 outlineElem.appendChildLast(text);
42 outline.appendChildLast(outlineElem);
43 page.appendChildLast(outline);
44 document.appendChildLast(page);
45
46 document.save(Paths.get(dataDir, "SetDefaultParagraphStyle.one").toString());
The output file looks like:
Set proofing language for a text.
1 String dataDir = Paths.get(Utils.getSharedDataDir(LoadPasswordProtectedOneNoteDoc.class), "text").toString();
2
3 Document document = new Document();
4 Page page = new Page(document);
5 Outline outline = new Outline(document);
6 OutlineElement outlineElem = new OutlineElement(document);
7
8 RichText text = new RichText(document);
9 text.setText("United States Germany China");
10 text.setParagraphStyle(ParagraphStyle.getDefault());
11
12 // Font and font size are from text.ParagraphStyle
13 TextStyle style = new TextStyle();
14 style.setLanguage(Locale.forLanguageTag("en-US"));
15 style.setRunIndex(13);
16 text.getStyles().addItem(style);
17
18 // Only font is from text.ParagraphStyle
19 style = new TextStyle();
20 style.setLanguage(Locale.forLanguageTag("de-DE"));
21 style.setRunIndex(21);
22 text.getStyles().addItem(style);
23
24 // Only font size is from text.ParagraphStyle
25 style = new TextStyle();
26 style.setLanguage(Locale.forLanguageTag("zh-CN"));
27 style.setRunIndex(text.getText().length());
28 text.getStyles().addItem(style);
29
30
31 outlineElem.appendChildLast(text);
32 outline.appendChildLast(outlineElem);
33 page.appendChildLast(outline);
34 document.appendChildLast(page);
35
36 document.save(Paths.get(dataDir, "SetProofingLanguageForText.one").toString());
The output file looks like: