Work, Extract or Replace Text in OneNote Document using C# API

Extract Text from OneNote Document

One of the tasks that developers need to perform is extracting text from an OneNote document. Aspose.Note for .NET allows developers to extract text from OneNote document in various ways.

Aspose.Note for .NET offers the Document class that represents the OneNote file. The simple LINQ can be used to extract text from the OneNote document.

Extracting All Text from OneNote Document

This example works as follows:

  1. Create an object of the Document class.
  2. Call the LINQ-based Code to extract text
  3. Display text on the output screen.

The following code example demonstrates how to extract all text from the OneNote document.

 1// The path to the documents directory.
 2string dataDir = RunExamples.GetDataDir_Text();
 3
 4// Load the document into Aspose.Note.
 5Document oneFile = new Document(dataDir + "Aspose.one");
 6
 7// Retrieve text
 8string text = string.Join(Environment.NewLine, oneFile.GetChildNodes<RichText>().Select(e => e.Text)) + Environment.NewLine;
 9
10// Print text on the output screen
11Console.WriteLine(text);

Extracting Text from a Specified Page of OneNote Document

This example works as follows:

  1. Create an object of the Document class.
  2. Filter out a list of page nodes.
  3. Retrieve the page node by index.
  4. Call the LINQ-based Code to extract text
  5. Display the text on the output screen.

The following code example demonstrates how to extract text from a specified page in OneNote document.

 1// The path to the documents directory.
 2string dataDir = RunExamples.GetDataDir_Text();
 3
 4// Load the document into Aspose.Note.
 5Document oneFile = new Document(dataDir + "Aspose.one");
 6
 7// Get list of page nodes
 8IList<Node> nodes = oneFile.GetChildNodes<Node>();
 9
10if (nodes.Count > 0 && nodes[0].NodeType == NodeType.Page)
11{
12    Page page = (Page)nodes[0];
13    // Retrieve text
14    string text = string.Join(Environment.NewLine, page.GetChildNodes<RichText>().Select(e => e.Text)) + Environment.NewLine;
15    // Print text on the output screen
16    Console.WriteLine(text);
17}

Replace Text in Pages of OneNote Document

Aspose.Note for .NET supports finding and then replacing text within OneNote document. Aspose.Note for .NET offers the Document class that represents the OneNote file.

Replace Text on All Pages

The following code example demonstrates how to replace text on all pages.

 1// The path to the documents directory.
 2string dataDir = RunExamples.GetDataDir_Text();
 3
 4Dictionary<string, string> replacements = new Dictionary<string, string>();
 5replacements.Add("Some task here", "New Text Here");
 6
 7// Load the document into Aspose.Note.
 8Document oneFile = new Document(dataDir + "Aspose.one");
 9
10// Get all RichText nodes
11IList<RichText> textNodes = oneFile.GetChildNodes<RichText>();
12
13foreach (RichText richText in textNodes)
14{
15    foreach (KeyValuePair<string, string> kvp in replacements)
16    {
17        if (richText != null && richText.Text.Contains(kvp.Key))
18        {
19            // Replace text of a shape
20            richText.Text = richText.Text.Replace(kvp.Key, kvp.Value);
21        }
22    }
23}
24
25dataDir = dataDir + "ReplaceTextOnAllPages_out.pdf";
26
27// Save to any supported file format
28oneFile.Save(dataDir, SaveFormat.Pdf);

Replace Text on a Particular Page

The following code example demonstrates how to replace text on a particular page.

 1// The path to the documents directory.
 2string dataDir = RunExamples.GetDataDir_Text();
 3
 4Dictionary<string, string> replacements = new Dictionary<string, string>();
 5replacements.Add("voice over", "voice over new text");
 6
 7// Load the document into Aspose.Note.
 8Document oneFile = new Document(dataDir + "Aspose.one");
 9
10IList<Page> pageNodes = oneFile.GetChildNodes<Page>();
11
12
13// Get all RichText nodes
14IList<RichText> textNodes = pageNodes[0].GetChildNodes<RichText>();
15
16foreach (RichText richText in textNodes)
17{
18    foreach (KeyValuePair<string, string> kvp in replacements)
19    {
20        if (richText != null && richText.Text.Contains(kvp.Key))
21        {
22            // Replace text of a shape
23            richText.Text = richText.Text.Replace(kvp.Key, kvp.Value);
24        }
25    }
26}
27dataDir = dataDir + "ReplaceTextOnParticularPage_out.pdf";
28// Save to any supported file format
29oneFile.Save(dataDir, SaveFormat.Pdf);

Retrieve Bullet or Number List Properties

Aspose.Note for .NET 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.

 1string dataDir = RunExamples.GetDataDir_Text();
 2
 3// Load the document into Aspose.Note.
 4Document oneFile = new Document(dataDir + "ApplyNumberingOnText.one");
 5
 6// Retrieve a collection nodes of the outline element
 7IList<OutlineElement> nodes = oneFile.GetChildNodes<OutlineElement>();
 8
 9// Iterate through each node
10foreach (OutlineElement node in nodes)
11{
12    if (node.NumberList != null)
13    {
14        NumberList list = node.NumberList;
15        // Retrieve font name
16        Console.WriteLine("Font Name: " + list.Font);
17        // Retrieve font length
18        Console.WriteLine("Font Length: " + list.Font.Length);
19        // Retrieve font size
20        Console.WriteLine("Font Size: " + list.FontSize);
21        // Retrieve font color
22        Console.WriteLine("Font Color: " + list.FontColor);
23        // Retrieve format
24        Console.WriteLine("Font format: " + list.Format);
25        // Check bold
26        Console.WriteLine("Is bold: " + list.IsBold);
27        // Check italic
28        Console.WriteLine("Is italic: " + list.IsItalic);
29        Console.WriteLine();
30    }
31}

Apply Bullets on the Text

Arranging texts in bullet form is a very common approach. Aspose.Note for .NET APIs allows developers to arrange text items in bullets.

Aspose.Note for .NET 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:

  1. Create an instance of the Document class that represents a OneNote document.
  2. Initialize three objects of Page class and set their levels.
  3. Initialize the objects of Outline, OutlineElement and RichText classes. Setting the NumberList property of the OutlineElement class adds bullets.
  4. The ParagraphStyle class defines the text formatting.
  5. Generate the OneNote document by calling the Save method of the Document object.

The following code example demonstrates how to apply a bullet on the OneNote document.

 1string dataDir = RunExamples.GetDataDir_Text();
 2
 3// Create an object of the Document class
 4Aspose.Note.Document doc = new Aspose.Note.Document();
 5// Initialize Page class object
 6Aspose.Note.Page page = new Aspose.Note.Page(doc);
 7// Initialize Outline class object
 8Outline outline = new Outline(doc);
 9// Initialize TextStyle class object and set formatting properties
10ParagraphStyle defaultStyle = new ParagraphStyle { FontColor = Color.Black, FontName = "Arial", FontSize = 10 };
11
12// Initialize OutlineElement class objects and apply bullets
13OutlineElement outlineElem1 = new OutlineElement(doc) { NumberList = new NumberList("*", "Arial", 10) };
14// Initialize RichText class object and apply text style
15RichText text1 = new RichText(doc) { Text = "First", ParagraphStyle = defaultStyle };
16outlineElem1.AppendChildLast(text1);
17
18OutlineElement outlineElem2 = new OutlineElement(doc) { NumberList = new NumberList("*", "Arial", 10) };
19RichText text2 = new RichText(doc) { Text = "Second", ParagraphStyle = defaultStyle };
20outlineElem2.AppendChildLast(text2);
21
22OutlineElement outlineElem3 = new OutlineElement(doc) { NumberList = new NumberList("*", "Arial", 10) };
23RichText text3 = new RichText(doc) { Text = "Third", ParagraphStyle = defaultStyle };
24outlineElem3.AppendChildLast(text3);
25
26// Add outline elements
27outline.AppendChildLast(outlineElem1);
28outline.AppendChildLast(outlineElem2);
29outline.AppendChildLast(outlineElem3);
30
31// Add Outline node
32page.AppendChildLast(outline);
33// Add Page node
34doc.AppendChildLast(page);
35
36dataDir = dataDir + "ApplyBulletsOnText_out.one"; 
37// Save OneNote document
38doc.Save(dataDir);

Apply Numbering on the Text

Arranging texts in numbering form is a very common approach. Aspose.Note for .NET APIs allows developers to arrange text items in numbering.

Aspose.Note for .NET 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:

  1. Create an instance of the Document class that represents a OneNote document.
  2. Initialize three objects of Page class and set their levels.
  3. Initialize the objects of Outline, OutlineElement and RichText classes. Setting the NumberList property of the OutlineElement class adds bullets.
  4. The ParagraphStyle class defines the text formatting.
  5. Generate the OneNote document by calling the Save method of the Document object.

The following code example demonstrates how to apply a bullet on the OneNote document.

 1string dataDir = RunExamples.GetDataDir_Text();
 2            
 3// Create an object of the Document class
 4Document doc = new Document();
 5            
 6// Initialize Page class object
 7Aspose.Note.Page page = new Aspose.Note.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 { FontColor = Color.Black, FontName = "Arial", FontSize = 10 };
14
15// Initialize OutlineElement class objects and apply numbering
16// Numbers in the same outline are automatically incremented.
17OutlineElement outlineElem1 = new OutlineElement(doc) { NumberList = new NumberList("{0})", NumberFormat.DecimalNumbers, "Arial", 10) };
18RichText text1 = new RichText(doc) { Text = "First", ParagraphStyle = defaultStyle };
19outlineElem1.AppendChildLast(text1);
20
21OutlineElement outlineElem2 = new OutlineElement(doc) { NumberList = new NumberList("{0})", NumberFormat.DecimalNumbers, "Arial", 10) };
22RichText text2 = new RichText(doc) { Text = "Second", ParagraphStyle = defaultStyle };
23outlineElem2.AppendChildLast(text2);
24
25OutlineElement outlineElem3 = new OutlineElement(doc) { NumberList = new NumberList("{0})", NumberFormat.DecimalNumbers, "Arial", 10) };
26RichText text3 = new RichText(doc) { Text = "Third", ParagraphStyle = defaultStyle };
27outlineElem3.AppendChildLast(text3);
28
29// Add outline elements
30outline.AppendChildLast(outlineElem1);
31outline.AppendChildLast(outlineElem2);
32outline.AppendChildLast(outlineElem3);
33
34// Add Outline node
35page.AppendChildLast(outline);
36// Add Page node
37doc.AppendChildLast(page);
38
39dataDir = dataDir + "ApplyNumberingOnText_out.one"; 
40// Save OneNote document
41doc.Save(dataDir);

Insert Chinese Number List in the OneNote Document

Aspose.Note for .NET APIs support inserting the Chinese number list in the OneNote document.

Aspose.Note for .NET offers the Document class that represents the OneNote file. Inserting the Chinese number list to the 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:

  1. Create an instance of the Document class that represents the OneNote document.
  2. Initialize three objects of Page class and set their levels.
  3. Initialize the objects of Outline, OutlineElement, and RichText classes. Setting the NumberList property with Chinese counting format of the OutlineElement class adds bullets.
  4. The ParagraphStyle class defines the text formatting.
  5. Generate the OneNote document by calling the Save method of the Document object.

The following code example demonstrates how to apply a bullet on the OneNote document.

 1string dataDir = RunExamples.GetDataDir_Text();
 2
 3// Initialize OneNote document
 4Aspose.Note.Document doc = new Aspose.Note.Document();
 5// Initialize OneNote page
 6Aspose.Note.Page page = new Aspose.Note.Page(doc);
 7Outline outline = new Outline(doc);
 8// Apply text style settings
 9ParagraphStyle defaultStyle = new ParagraphStyle { FontColor = Color.Black, FontName = "Arial", FontSize = 10 };
10
11// Numbers in the same outline are automatically incremented.
12OutlineElement outlineElem1 = new OutlineElement(doc) { NumberList = new NumberList("{0})", NumberFormat.ChineseCounting, "Arial", 10) };
13RichText text1 = new RichText(doc) { Text = "First", ParagraphStyle = defaultStyle };
14outlineElem1.AppendChildLast(text1);
15//------------------------
16OutlineElement outlineElem2 = new OutlineElement(doc) { NumberList = new NumberList("{0})", NumberFormat.ChineseCounting, "Arial", 10) };
17RichText text2 = new RichText(doc) { Text = "Second", ParagraphStyle = defaultStyle };
18outlineElem2.AppendChildLast(text2);
19//------------------------
20OutlineElement outlineElem3 = new OutlineElement(doc) { NumberList = new NumberList("{0})", NumberFormat.ChineseCounting, "Arial", 10) };
21RichText text3 = new RichText(doc) { Text = "Third", ParagraphStyle = defaultStyle };
22outlineElem3.AppendChildLast(text3);
23//------------------------
24outline.AppendChildLast(outlineElem1);
25outline.AppendChildLast(outlineElem2);
26outline.AppendChildLast(outlineElem3);
27page.AppendChildLast(outline);
28doc.AppendChildLast(page);
29
30dataDir = dataDir + "InsertChineseNumberList_out.one"; 
31// Save OneNote document
32doc.Save(dataDir);

Creating Page Title in MS OneNote Style

Aspose.Note for .NET allows creating a page tile in default MS OneNote Style. The DefaultMsOneNoteTitleTextStyle property of the TextStyle class can be used to set the page title for this purpose.

Setting Page Title in Microsoft OneNote Style

 1string dataDir = RunExamples.GetDataDir_Text();
 2string outputPath = dataDir + "CreateTitleMsStyle_out.one";
 3
 4var doc = new Document();
 5var page = new Page(doc);
 6
 7page.Title = new Title(doc)
 8{
 9    TitleText = new RichText(doc)
10    {
11        Text = "Title text.",
12        ParagraphStyle = ParagraphStyle.Default
13    },
14    TitleDate = new RichText(doc)
15    {
16        Text = new DateTime(2011, 11, 11).ToString("D", CultureInfo.InvariantCulture),
17        ParagraphStyle = ParagraphStyle.Default
18    },
19    TitleTime = new RichText(doc)
20    {
21        Text = "12:34",
22        ParagraphStyle = ParagraphStyle.Default
23    }
24};
25
26doc.AppendChildLast(page);
27
28doc.Save(outputPath);
Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.