Working with Text
Extract Text from OneNote Document
One of the tasks that developers need to perform is extracting text from a OneNote document. Aspose.Note for Java allows developers to extract text from a OneNote document in various ways.
Aspose.Note for Java offers the Document class that represents a OneNote file. The Document class exposes the GetText method that can be called to extract text from a OneNote document.
This article shows how to:
Extracting All Text from OneNote Document
This example works as follows:
- Create an object of the Document class.
- Call the stream-based code to extract text.
- Display text on the output screen.
The following example shows how to extract all text from a OneNote document.
1// The path to the documents directory.
2String dataDir = Utils.getSharedDataDir(ExtractingAllText.class) + "text\\";
3
4// Load the document into Aspose.Note.
5Document oneFile = new Document(dataDir + "Sample1.one");
6
7// Retrieve text
8List<RichText> textNodes = (List<RichText>) oneFile.getChildNodes(RichText.class);
9 StringBuilder text = new StringBuilder();
10 for (RichText richText : textNodes) {
11 text = text.append(richText.getText().toString());
12 }
13
14// Print text on the output screen
15System.out.println(text);
Extracting Text from a Specified Page of a OneNote Document
This example works as follows:
- Create an object of the Document class.
- Filter out a list of page nodes.
- Retrieve the page node by index.
- Call the stream-based code to extract text.
- Display the text on the output screen.
The following example shows how to extract text from a specified page in a OneNote document.
1// The path to the documents directory.
2String dataDir = Utils.getSharedDataDir(ExtractingTextFromAPage.class) + "text\\";
3
4// Load the document into Aspose.Note.
5Document oneFile = new Document(dataDir + "Sample1.one");
6
7// Get list of page nodes
8List<Node> nodes = oneFile.getChildNodes(Node.class);
9
10if (nodes.size() > 0 && nodes.get(0).getNodeType() == NodeType.Page)
11{
12 Page page = (Page)nodes.get(0);
13 // Retrieve text
14 List<RichText> textNodes = (List<RichText>) page.getChildNodes(RichText.class);
15 StringBuilder text = new StringBuilder();
16 for (RichText richText : textNodes) {
17 text = text.append(richText.getText().toString());
18 }
19
20 // Print text on the output screen
21 System.out.println(text);
22}
Replace Text in Pages of a OneNote Document and Save as PDF
Aspose.Note for Java supports finding and then replacing text within a OneNote document.
Aspose.Note for Java offers the Document class that represents a OneNote file.
This article shows how to:
Replace Text on All Pages
The following example shows how to replace text on all pages.
1String dataDir = Utils.getSharedDataDir(ReplaceTextonAllPages.class) + "text/";
2
3Map<String, String> replacements = new HashMap<String, String>();
4replacements.put("2. Get organized", "New Text Here");
5
6// Load the document into Aspose.Note.
7LoadOptions options = new LoadOptions();
8Document oneFile = new Document(dataDir + "Sample1.one", options);
9
10// Get all RichText nodes
11List<RichText> textNodes = (List<RichText>) oneFile.getChildNodes(RichText.class);// <RichText.class>();
12
13// Traverse all nodes and compare text against the key text
14for (RichText richText : textNodes) {
15 for (String key : replacements.keySet()) {
16 if (richText != null && richText.getText().contains(key)) {
17 // Replace text of a shape
18 richText.setText(richText.getText().replace(key, replacements.get(key)));
19 }
20 }
21}
22
23// Save to any supported file format
24oneFile.save(dataDir + "ReplaceTextonAllPages_out.pdf", SaveFormat.Pdf);
Replace Text on a Particular Page
The following example shows how to replace text on a particular page.
1String dataDir = Utils.getSharedDataDir(ReplaceTextonParticularPage.class) + "text/";
2
3Map<String, String> replacements = new HashMap<String, String>();
4replacements.put("2. Get organized", "New Text Here");
5
6// Load the document into Aspose.Note.
7Document oneFile = new Document(dataDir + "Sample1.one", new LoadOptions());
8
9List<Page> pageNodes = (List<Page>) oneFile.getChildNodes(Page.class);
10
11// Get all RichText nodes
12List<RichText> textNodes = (List<RichText>) pageNodes.get(0).getChildNodes(RichText.class);
13
14for (RichText richText : textNodes) {
15 for (String key : replacements.keySet()) {
16 if (richText != null && richText.getText().contains(key)) {
17 // Replace text of a shape
18 richText.setText(richText.getText().replace(key, replacements.get(key)));
19 }
20 }
21}
22
23// Save to any supported file format
24oneFile.save(dataDir + "ReplaceTextonParticularPage_out.pdf", SaveFormat.Pdf);
Retrieve Bullet or Number List Properties
Aspose.Note for Java provides comprehensive support for Microsoft OneNote lists. Developers can access a number of list properties, such as font name, numbering format, and many others.
Retrieving Properties
Aspose.Note for .NET offers the OutlineElement class that represents an outline inside a OneNote document. The OutlineElement class exposes the NumberList property that can be called to extract bullet or numbered list properties. The following example shows how to extract bullet or numbered list properties from a OneNote document.
Output Screen
1String dataDir = Utils.getSharedDataDir(GetListProperties.class) + "text/";
2
3// Load the document into Aspose.Note
4Document oneFile = new Document(dataDir + "Sample1.one");
5
6// Retrieve a collection nodes of the outline element
7List<OutlineElement> nodes = oneFile.getChildNodes(OutlineElement.class);
8
9// Iterate through each node
10for (OutlineElement node : nodes) {
11 if (node.getNumberList() != null) {
12 NumberList list = node.getNumberList();
13 // Retrieve font name
14 System.out.println("Font Name: " + list.getFont());
15 // Retrieve font length
16 System.out.println("Font Length: " + list.getFont());
17 // Retrieve font size
18 System.out.println("Font Size: " + list.getFontSize());
19 // Retrieve font color
20 System.out.println("Font Color: " + list.getFontColor());
21 // Retrieve format
22 System.out.println("Font format: " + list.getFormat());
23 // Check bold
24 System.out.println("Is bold: " + list.isBold());
25 // Check italic
26 System.out.println("Is italic: " + list.isItalic());
27
28 System.out.println();
29 }
30}
Apply Bullets on the Text
Arranging texts in bullet form is a very common approach. Aspose.Note for Java API allows developers to arrange text items in bullets.
Aspose.Note for Java offers the [Document]( https://reference.aspose.com/note/java/com.Aspose.Note.Document Class) class that represents a OneNote file. Writing text to a OneNote document page involves creating an OutlineElement, which offers NumberList property to define the bullet.
Please see a brief description to add and apply bullets on the text in OneNote document using the Aspose.Note APIs:
- Create an instance of the Document class that represents a OneNote document.
- Initialize three objects of Page class and set their levels.
- Initialize the objects of Outline, OutlineElement and RichText classes. Setting the NumberList property of the OutlineElement class adds bullets.
- The ParagraphStyle class defines the text formatting.
- Generate the OneNote document by calling the Save method of the Document object.
The following example shows how to apply a bullet on the OneNote document.
1String dataDir = Utils.getSharedDataDir(CreateBulletedList.class) + "text/";
2
3// create an object of the Document class
4Document doc = new Document();
5
6// initialize Page class object
7Page page = new Page(doc);
8
9// initialize Outline class object
10Outline outline = new Outline(doc);
11
12// initialize TextStyle class object and set formatting properties
13ParagraphStyle defaultStyle = new ParagraphStyle();
14defaultStyle.setFontColor(Color.black);
15defaultStyle.setFontName("Arial");
16defaultStyle.setFontSize(10);
17
18// initialize OutlineElement class objects and apply bullets
19OutlineElement outlineElem1 = new OutlineElement(doc);
20outlineElem1.setNumberList(new NumberList("*", "Arial", 10));
21
22// initialize RichText class object and apply text style
23RichText text1 = new RichText(doc);
24text1.setText("First");
25text1.setParagraphStyle(defaultStyle);
26outlineElem1.appendChildLast(text1);
27
28OutlineElement outlineElem2 = new OutlineElement(doc);
29outlineElem2.setNumberList(new NumberList("*", "Arial", 10));
30
31RichText text2 = new RichText(doc);
32text2.setText("Second");
33text2.setParagraphStyle(defaultStyle);
34outlineElem2.appendChildLast(text2);
35
36OutlineElement outlineElem3 = new OutlineElement(doc);
37outlineElem3.setNumberList(new NumberList("*", "Arial", 10));
38RichText text3 = new RichText(doc);
39text3.setText("Third");
40text3.setParagraphStyle(defaultStyle);
41outlineElem3.appendChildLast(text3);
42
43// add outline elements
44outline.appendChildLast(outlineElem1);
45outline.appendChildLast(outlineElem2);
46outline.appendChildLast(outlineElem3);
47
48// add Outline node
49page.appendChildLast(outline);
50
51// add Page node
52doc.appendChildLast(page);
53
54// save the document
55doc.save(dataDir + "CreateBulletedList_out.pdf");
Apply Numbering on the Text
Arranging texts in numbering form is a very common approach. Aspose.Note for Java API allows developers to arrange text items in numbering.
Aspose.Note for Java offers the Document class that represents a OneNote file. Writing text to a OneNote document page involves creating an OutlineElement, which offers NumberList property to define the bullet.
Please see a brief description to add and apply bullets on the text in OneNote document using the Aspose.Note APIs:
- Create an instance of the Document class that represents a OneNote document.
- Initialize three objects of Page class and set their levels.
- Initialize the objects of Outline, OutlineElement and RichText classes. Setting the NumberList property of the OutlineElement class adds bullets.
- The ParagraphStyle class defines the text formatting.
- Generate the OneNote document by calling the Save method of the Document object.
The following example shows how to apply a bullet on the OneNote document.
1String dataDir = Utils.getSharedDataDir(CreateNumberedList.class) + "text/";
2
3// create an object of the Document class
4Document doc = new Document();
5
6// initialize Page class object
7Page page = new Page(doc);
8
9// initialize Outline class object
10Outline outline = new Outline(doc);
11
12// initialize TextStyle class object and set formatting properties
13ParagraphStyle defaultStyle = new ParagraphStyle();
14defaultStyle.setFontColor(Color.black);
15defaultStyle.setFontName("Arial");
16defaultStyle.setFontSize(10);
17
18// initialize OutlineElement class objects and apply numbering
19// numbers in the same outline are automatically incremented.
20OutlineElement outlineElem1 = new OutlineElement(doc);
21
22outlineElem1.setNumberList(new NumberList("{0})", NumberFormat.DecimalNumbers, "Arial", 10));
23
24RichText text1 = new RichText(doc);
25text1.setText("First");
26text1.setParagraphStyle(defaultStyle);
27outlineElem1.appendChildLast(text1);
28
29OutlineElement outlineElem2 = new OutlineElement(doc);
30outlineElem2.setNumberList(new NumberList("{0})", NumberFormat.DecimalNumbers, "Arial", 10));
31RichText text2 = new RichText(doc);
32text2.setText("Second");
33text2.setParagraphStyle(defaultStyle);
34outlineElem2.appendChildLast(text2);
35
36OutlineElement outlineElem3 = new OutlineElement(doc);
37outlineElem3.setNumberList(new NumberList("{0})", NumberFormat.DecimalNumbers, "Arial", 10));
38RichText text3 = new RichText(doc);
39text3.setText("Third");
40text3.setParagraphStyle(defaultStyle);
41outlineElem3.appendChildLast(text3);
42
43// add outline elements
44outline.appendChildLast(outlineElem1);
45outline.appendChildLast(outlineElem2);
46outline.appendChildLast(outlineElem3);
47
48// add Outline node
49page.appendChildLast(outline);
50
51// add Page node
52doc.appendChildLast(page);
53
54// save the document
55doc.save(dataDir + "CreateNumberedList_out.pdf");
Insert Chinese Number List in the OneNote Document
Aspose.Note for Java API supports inserting the Chinese number list in the OneNote document.
Aspose.Note for Java offers the Document class that represents a OneNote file. Inserting the Chinese number list to a OneNote document page involves creating an OutlineElement, which offers NumberList property to define the bullet.
Please see a brief description to add and apply bullets on the text in OneNote document using the Aspose.Note APIs:
- Create an instance of the Document class that represents a OneNote document.
- Initialize three objects of Page class and set their levels.
- Initialize the objects of Outline, OutlineElement and RichText classes. Setting the NumberList property with Chinese counting format of the OutlineElement class adds bullets.
- The ParagraphStyle class defines the text formatting.
- Generate the OneNote document by calling the Save method of the Document object.
The following example shows how to apply a bullet on the OneNote document.
1String dataDir = Utils.getSharedDataDir(CreateChineseNumberedList.class) + "text/";
2
3// create an object of the Document class
4Document doc = new Document();
5
6// initialize Page class object
7Page page = new Page(doc);
8
9// initialize Outline class object
10Outline outline = new Outline(doc);
11
12// initialize TextStyle class object and set formatting properties
13ParagraphStyle defaultStyle = new ParagraphStyle();
14defaultStyle.setFontColor(Color.black);
15defaultStyle.setFontName("Arial");
16defaultStyle.setFontSize(10);
17
18// initialize OutlineElement class objects and apply numbering
19// numbers in the same outline are automatically incremented.
20OutlineElement outlineElem1 = new OutlineElement(doc);
21
22outlineElem1.setNumberList(new NumberList("{0})", NumberFormat.ChineseCounting, "Arial", 10));
23
24RichText text1 = new RichText(doc);
25text1.setText("First");
26text1.setParagraphStyle(defaultStyle);
27outlineElem1.appendChildLast(text1);
28
29OutlineElement outlineElem2 = new OutlineElement(doc);
30outlineElem2.setNumberList(new NumberList("{0})", NumberFormat.ChineseCounting, "Arial", 10));
31RichText text2 = new RichText(doc);
32text2.setText("Second");
33text2.setParagraphStyle(defaultStyle);
34outlineElem2.appendChildLast(text2);
35
36OutlineElement outlineElem3 = new OutlineElement(doc);
37outlineElem3.setNumberList(new NumberList("{0})", NumberFormat.ChineseCounting, "Arial", 10));
38RichText text3 = new RichText(doc);
39text3.setText("Third");
40text3.setParagraphStyle(defaultStyle);
41outlineElem3.appendChildLast(text3);
42
43// add outline elements
44outline.appendChildLast(outlineElem1);
45outline.appendChildLast(outlineElem2);
46outline.appendChildLast(outlineElem3);
47
48// add Outline node
49page.appendChildLast(outline);
50
51// add Page node
52doc.appendChildLast(page);
53
54// save the document
55doc.save(dataDir + "CreateChineseNumberedList_out.pdf");
Creating Page Title in MS OneNote Style
Aspose.Note API allows creating a page tile in default MS OneNote Style. The DefaultMsOneNoteTitleParagraphStyle property of the ParagraphStyle class can be used to set the page title for this purpose.
Setting Page Title in Microsoft OneNote Style
1String dataDir = Utils.getSharedDataDir(SettingPageTitleinMicrosoftOneNoteStyle.class) + "text/";
2
3// initialize new Document
4Document doc = new Document(dataDir + "Sample1.one");
5// initialize new Page
6Page page = new Page(doc);
7
8// title text
9RichText titleText = new RichText(doc);
10titleText.setText("Title text.");
11titleText.setParagraphStyle(ParagraphStyle.getDefault());
12
13// title date
14RichText titleDate = new RichText(doc);
15titleDate.setText("2011,11,11");
16titleDate.setParagraphStyle(ParagraphStyle.getDefault());
17
18// title time
19RichText titleTime = new RichText(doc);
20titleTime.setText("12:34");
21titleTime.setParagraphStyle(ParagraphStyle.getDefault());
22
23Title title = new Title(doc);
24title.setTitleText(titleText);
25title.setTitleDate(titleDate);
26title.setTitleTime(titleTime);
27page.setTitle(title);
28
29// append page node
30doc.appendChildLast(page);