Create or Work with OneNote Notebook in C# API
Creating and Saving a Notebook
The Notebook class exposed by the API lets you create a notebook from scratch and save it as OneNote Notebook (.onetoc2). OneNote notebook can be created using the default constructor of Notebook class. This article shows creating and saving a Notebook document.
1// The path to the documents directory.
2string dataDir = RunExamples.GetDataDir_NoteBook();
3
4var notebook = new Notebook();
5
6dataDir = dataDir + "test_out.onetoc2";
7
8// Save the Notebook
9notebook.Save(dataDir);
Saving Notebook to Stream
1// The path to the documents directory.
2string dataDir = RunExamples.GetDataDir_NoteBook();
3var notebook = new Notebook();
4
5MemoryStream stream = new MemoryStream();
6notebook.Save(stream);
7if (notebook.Count > 0)
8{
9 var childDocument0 = notebook[0] as Document;
10
11 MemoryStream childStream = new MemoryStream();
12 childDocument0.Save(childStream);
13
14 var childDocument1 = notebook[1] as Document;
15 childDocument0.Save(dataDir + "SaveNotebookToStream_out.one");
16}
Support of Password Protected Documents Writing as Part of .onetoc2 Notebook
1// The path to the documents directory.
2string dataDir = RunExamples.GetDataDir_NoteBook();
3
4var notebook = new Notebook(dataDir + "notebook2.onetoc2", new NotebookLoadOptions() { DeferredLoading = false });
5
6notebook.Save(dataDir + "notebook_out.onetoc2", new NotebookOneSaveOptions() { DeferredSaving = true });
7
8if (notebook.Count > 0)
9{
10 var childDocument0 = notebook[0] as Document;
11
12 childDocument0.Save(dataDir + "Not Locked_out.one");
13
14 var childDocument1 = notebook[1] as Document;
15
16 childDocument1.Save(dataDir + "Locked Pass1_out.one", new OneSaveOptions() { DocumentPassword = "pass" });
17
18 var childDocument2 = notebook[2] as Document;
19
20 childDocument2.Save(dataDir + "Locked Pass2_out.one", new OneSaveOptions() { DocumentPassword = "pass2" });
21}
Reading Notebook File
The .onetoc2 file format represents the saved table of contents for the OneNote document. The Notebook class can be used to read this type of Microsoft .one file format and parse it further. The following code sample shows the usage of this class for use.
Loading Notebook File
1// The path to the documents directory.
2string dataDir = RunExamples.GetDataDir_LoadingAndSaving();
3string fileName = "Open Notebook.onetoc2";
4if (fileName != "")
5{
6 try
7 {
8 var notebook = new Notebook(dataDir + fileName);
9 foreach (var notebookChildNode in notebook)
10 {
11 Console.WriteLine(notebookChildNode.DisplayName);
12 if (notebookChildNode is Document)
13 {
14 // Do something with child document
15 }
16 else if (notebookChildNode is Notebook)
17 {
18 // Do something with child notebook
19 }
20 }
21 }
22 catch (Exception ex)
23 {
24 Console.WriteLine(ex.Message);
25 }
26}
27else
28{
29 Console.WriteLine("\nPlease enter valid file name.");
30}
Loading Notebook from Stream
1// The path to the documents directory.
2string dataDir = RunExamples.GetDataDir_NoteBook();
3FileStream stream = new FileStream(dataDir + "Notizbuch öffnen.onetoc2", FileMode.Open);
4var notebook = new Notebook(stream);
5
6FileStream childStream = new FileStream(dataDir + "Aspose.one", FileMode.Open);
7notebook.LoadChildDocument(childStream);
8
9notebook.LoadChildDocument(dataDir + "Sample1.one");
Loading Notebook File with LoadOptions
By default, notebook’s child documents are loaded “lazily”, i.e. their loading is postponed until direct access to a specific child. An alternative approach is to load all children at once. The last can be achieved by passing to the Notebook constructor NotebookLoadOptions with InstantLoading flag set to true. Following code snippets demonstrate both approaches.
Loading Notebook with Lazy Loading Option
1string inputFile = "Notizbuch öffnen.onetoc2";
2string dataDir = RunExamples.GetDataDir_NoteBook();
3
4// By default children loading is "lazy".
5Notebook notebook = new Notebook(dataDir + inputFile);
6
7foreach (INotebookChildNode notebookChildNode in notebook) {
8 // Actual loading of the child document happens only here.
9 if (notebookChildNode is Document) {
10 // Do something with child document
11 }
12}
Loading Notebook Instantly
1// By default children loading is "lazy".
2// Therefore for instant loading has took place,
3// it is necessary to set the NotebookLoadOptions.InstantLoading flag.
4NotebookLoadOptions loadOptions = new NotebookLoadOptions();
5loadOptions.InstantLoading = true;
6String inputFile = "Notizbuch öffnen.onetoc2";
7String dataDir = RunExamples.GetDataDir_NoteBook();
8Notebook notebook = new Notebook(dataDir + inputFile, loadOptions);
9
10// All child documents are already loaded.
11foreach (INotebookChildNode notebookChildNode in notebook) {
12 if (notebookChildNode is Document) {
13 // Do something with child document
14 }
15}
Loading Password Protected Documents as a part of .onetoc2 Notebook
A Notebook having password protected .One documents can be loaded with the help of LoadOptions class of the API specifying the password.
1// The path to the documents directory.
2string dataDir = RunExamples.GetDataDir_NoteBook();
3var notebook = new Notebook(dataDir + "test.onetoc2", new NotebookLoadOptions() { DeferredLoading = true });
4
5notebook.LoadChildDocument(dataDir + "Aspose.one");
6notebook.LoadChildDocument(dataDir + "Locked Pass1.one", new LoadOptions() { DocumentPassword = "pass" });
7notebook.LoadChildDocument(dataDir + "Locked Pass2.one", new LoadOptions() { DocumentPassword = "pass2" });
Adding a Child Node to OneNote Notebook
Aspose.Note API lets you add/append a child node at first or last to a OneNote Notebook. The AppendChildLast or AppendChildFirst methods exposed by the Notebook class allows you to achieve this.
1// The path to the documents directory.
2string dataDir = RunExamples.GetDataDir_NoteBook();
3
4// Load a OneNote Notebook
5var notebook = new Notebook(dataDir + "Notizbuch �ffnen.onetoc2");
6
7// Append a new child to the Notebook
8notebook.AppendChild(new Document(dataDir + "Neuer Abschnitt 1.one"));
9
10dataDir = dataDir + "AddChildNode_out.onetoc2";
11
12// Save the Notebook
13notebook.Save(dataDir);
Remove a Child Node From OneNote Notebook
Aspose.Note API provides the capability to remove a child node from OneNote notebook. The RemoveChild method exposed by the API’s Notebook class lets you remove a child node that is meant to be removed from the Notebook.
1// The path to the documents directory.
2string dataDir = RunExamples.GetDataDir_NoteBook();
3
4// Load a OneNote Notebook
5var notebook = new Notebook(dataDir + "test.onetoc2");
6
7// Traverse through its child nodes for searching the desired child item
8foreach (var child in new List<INotebookChildNode>(notebook))
9{
10 if (child.DisplayName == "Remove Me")
11 {
12 // Remove the Child Item from the Notebook
13 notebook.RemoveChild(child);
14 }
15}
16
17dataDir = dataDir + "RemoveChildNode_out.onetoc2";
18
19// Save the Notebook
20notebook.Save(dataDir);
Converting Notebook Document to Image
Aspose.Note API provides the capability to convert Notebook documents (.onetoc2) to image. The API can export the document to image directly or we can also specify additional save options using the NotebookImageSaveOptions class.
Exporting Notebook to Image
1// The path to the documents directory.
2string dataDir = RunExamples.GetDataDir_NoteBook();
3
4// Load a OneNote Notebook
5var notebook = new Notebook(dataDir + "Notizbuch �ffnen.onetoc2");
6
7dataDir = dataDir + "ConvertToImage_out.png";
8
9// Save the Notebook
10notebook.Save(dataDir);
Exporting Notebook to Image with Options
1// The path to the documents directory.
2string dataDir = RunExamples.GetDataDir_NoteBook();
3
4// Load a OneNote Notebook
5var notebook = new Notebook(dataDir + "Notizbuch �ffnen.onetoc2");
6
7var notebookSaveOptions = new NotebookImageSaveOptions(SaveFormat.Png);
8
9var documentSaveOptions = notebookSaveOptions.DocumentSaveOptions;
10
11documentSaveOptions.Resolution = 400;
12
13dataDir = dataDir + "ConvertToImageWithOptions_out.png";
14
15// Save the Notebook
16notebook.Save(dataDir, notebookSaveOptions);
Export to Image as a Flattened Notebook
1// The path to the documents directory.
2string dataDir = RunExamples.GetDataDir_NoteBook();
3
4// Load a OneNote Notebook
5var notebook = new Notebook(dataDir + "Notizbuch öffnen.onetoc2");
6
7var notebookSaveOptions = new NotebookImageSaveOptions(SaveFormat.Png);
8
9var documentSaveOptions = notebookSaveOptions.DocumentSaveOptions;
10
11documentSaveOptions.Resolution = 400;
12notebookSaveOptions.Flatten = true;
13
14dataDir = dataDir + "ConvertToImageAsFlattenedNotebook_out.png";
15
16// Save the Notebook
17notebook.Save(dataDir, notebookSaveOptions);
Converting Notebook Document to PDF
Aspose.Note API provides the capability to convert Notebook documents (.onetoc2) to PDF format. The API can export the document to PDF directly or we can also specify additional save options using the NotebookPdfSaveOptions class. This article shows all the approaches to export Notebook to PDF.
Please note that you cannot set values against the Application and Producer fields, because of Aspose Ltd. and Aspose.Note for .NET x.x will be displayed against these fields.
Exporting Notebook to PDF
1// The path to the documents directory.
2string dataDir = RunExamples.GetDataDir_NoteBook();
3
4// Load a OneNote Notebook
5var notebook = new Notebook(dataDir + "Notizbuch �ffnen.onetoc2");
6
7dataDir = dataDir + "ConvertToPDF_out.pdf";
8
9// Save the Notebook
10notebook.Save(dataDir);
Exporting Notebook to PDF with Options
1// The path to the documents directory.
2string dataDir = RunExamples.GetDataDir_NoteBook();
3
4// Load a OneNote Notebook
5var notebook = new Notebook(dataDir + "Notizbuch �ffnen.onetoc2");
6
7var notebookSaveOptions = new NotebookPdfSaveOptions();
8
9var documentSaveOptions = notebookSaveOptions.DocumentSaveOptions;
10
11documentSaveOptions.PageSplittingAlgorithm = new KeepSolidObjectsAlgorithm();
12
13dataDir = dataDir + "ConvertToPDF_out.pdf";
14
15// Save the Notebook
16notebook.Save(dataDir, notebookSaveOptions);
Export to PDF as a Flattened Notebook
Aspose.Note API can be used to export a OneNote notebook as a Flattened PDF which exports all children to a single directory.
1// The path to the documents directory.
2string dataDir = RunExamples.GetDataDir_NoteBook();
3
4// Load a OneNote Notebook
5var notebook = new Notebook(dataDir + "Notizbuch �ffnen.onetoc2");
6
7dataDir = dataDir + "ConvertToPDFAsFlattened_out.pdf";
8// Save the Notebook
9notebook.Save(
10 dataDir,
11 new NotebookPdfSaveOptions
12 {
13 Flatten = true
14 });
Retrieve Documents from OneNote Notebook
The following code snippet demonstrates how to get all documents which are presented in the entire MS OneNote notebook including child notebooks.
1string inputFile = "notebook.onetoc2";
2string dataDir = RunExamples.GetDataDir_NoteBook();
3Notebook rootNotebook = new Notebook(dataDir + inputFile);
4
5IList<Document> allDocuments = rootNotebook.GetChildNodes<Document>();
6foreach (Document document in allDocuments) {
7 Console.WriteLine(document.DisplayName);
8}
Read RichText Nodes from Microsoft OneNote Notebook
The following code snippet demonstrates how to print all RichText nodes from MS OneNote notebook:
1string inputFile = "notebook.onetoc2";
2string dataDir = RunExamples.GetDataDir_NoteBook();
3
4Notebook rootNotebook = new Notebook(dataDir + inputFile);
5
6IList<RichText> allRichTextNodes = rootNotebook.GetChildNodes<RichText>();
7foreach (RichText richTextNode in allRichTextNodes) {
8 Console.WriteLine(richTextNode.Text);
9}