Working with OneNote Notebook
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.
Saving Notebook to Stream
1// Load the document into Aspose.Note.
2String dataDir = Utils.getSharedDataDir(SaveNotebooktoStream.class) + "Notebook/";
3
4Notebook notebook = new Notebook();
5
6FileOutputStream stream = new FileOutputStream(dataDir + "output.onetoc2");
7
8notebook.save(stream);
9
10if (notebook.getCount() > 0) {
11 Document childDocument0 = (Document) notebook.get_Item(0);
12
13 OutputStream childStream = new FileOutputStream(dataDir + "childStream.one");
14 childDocument0.save(childStream);
15
16 Document childDocument1 = (Document) notebook.get_Item(1);
17
18 childDocument1.save(dataDir + "child.one");
19}
Support of Password Protected Documents Writing as Part of .onetoc2 Notebook
1// Load the document into Aspose.Note.
2String dataDir = Utils.getSharedDataDir(WritingPasswordProtectedDoc.class) + "Notebook/";
3
4Notebook notebook = new Notebook();
5
6NotebookOneSaveOptions saveOptions = new NotebookOneSaveOptions();
7saveOptions.setDeferredSaving(true);
8notebook.save(dataDir + "Open Notebook.onetoc2", saveOptions);
9
10Document childDocument0 = (Document) notebook.get_Item(0);
11childDocument0.save(dataDir + "Not Locked.one");
12
13Document childDocument1 = (Document) notebook.get_Item(1);
14OneSaveOptions documentSaveOptions1 = new OneSaveOptions();
15documentSaveOptions1.setDocumentPassword("pass1");
16childDocument1.save(dataDir + "Locked Pass1.one", documentSaveOptions1);
17
18Document childDocument2 = (Document) notebook.get_Item(2);
19OneSaveOptions documentSaveOptions2 = new OneSaveOptions();
20documentSaveOptions2.setDocumentPassword("pass2");
21childDocument2.save(dataDir + "Locked Pass2.one", documentSaveOptions2);
Reading Notebook File
The Notebook class exposed by the API lets you read a OneNote Notebook (.onetoc2) file from disc. The NotebookLoadOptions class provides further options to load the file with user-specified options.
Loading Notebook File
1String dataDir = Utils.getSharedDataDir(LoadingNotebook.class) + "Notebook/";
2Notebook notebook = new Notebook(dataDir + "Notizbuch �ffnen.onetoc2");
3
4
5for (INotebookChildNode notebookChildNode : notebook) {
6 System.out.println(notebookChildNode.getDisplayName());
7
8 if (notebookChildNode instanceof Document) {
9 // Do something with child document
10 } else if (notebookChildNode instanceof Notebook) {
11 // Do something with child notebook
12 }
13}
Loading Notebook File from Stream
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 the InstantLoading flag set to true. The following code snippets demonstrate both approaches.
Loading Notebook with Lazy Loading Option
1String dataDir = Utils.getSharedDataDir(LoadingNotebookFilewithLoadOptions.class) + "Notebook/";
2
3// By default children loading is "lazy".
4Notebook notebook = new Notebook(dataDir + "test.onetoc2");
5
6for (INotebookChildNode notebookChildNode : notebook) {
7 // Actual loading of the child document happens only here.
8 if (notebookChildNode instanceof Document) {
9 // Do something with child document
10 }
11}
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.setInstantLoading(true);
6String dataDir = Utils.getSharedDataDir(LoadingNotebookInstantly.class) + "Notebook/";
7Notebook notebook = new Notebook(dataDir + "test.onetoc2", loadOptions);
8
9// All child documents are already loaded.
10for (INotebookChildNode notebookChildNode : notebook) {
11 if (notebookChildNode instanceof Document) {
12 // Do something with child document
13 }
14}
In the current implementation, lazy loading implemented only for OneNote 2010 child documents/notebooks. OneNote Online first-level child documents/notebooks are always loaded instantly.
For instance, if there is a hierarchy structure:
- Root Notebook (2010)
— Section 1 Level 1 (2010)
— Section 2 Level 1 (Online)
— Notebook 1 Level 1 (2010)
—– Section 1 Level 2 (Online)
— Notebook 2 Level 1 (Online)
—– Section 1 Level 2 (Online)
—– Section 2 Level 2 (2010)
Then the only children will be loaded instantly are: Root Notebook -> Section 2 Level 1, Root Notebook -> Notebook 2 Level 1 and Root Notebook -> Notebook 2 Level 1 -> Section 1 Level 2.
Note that “Root Notebook -> Notebook 1 Level 1 -> Section 1 Level 2” is not loaded at the same time as “Root Notebook”; instead it will be loaded instantly while “Root Notebook -> Notebook 1 Level 1” is loading, as it is the first-level child for the mentioned notebook and the last is not Online itself.
Loading Password Protected Documents as a part of .onetoc2 Notebook
A password-protected notebook can be loaded with the help of LoadOptions class of the API specifying the password.
1// Load the document into Aspose.Note.
2String dataDir = Utils.getSharedDataDir(LoadPasswordProtectedDocuments.class) + "Notebook/";
3
4NotebookLoadOptions loadOptions = new NotebookLoadOptions();
5loadOptions.setDeferredLoading(true);
6Notebook notebook = new Notebook(dataDir + "Notizbuch �ffnen.onetoc2", loadOptions);
7
8notebook.loadChildDocument(dataDir + "Neuer Abschnitt 1.one");
9
10LoadOptions documentLoadOptions1 = new LoadOptions();
11documentLoadOptions1.setDocumentPassword("pass");
12notebook.loadChildDocument(dataDir + "Locked Pass1.one", documentLoadOptions1);
13
14LoadOptions documentLoadOptions2 = new LoadOptions();
15documentLoadOptions2.setDocumentPassword("pass");
16notebook.loadChildDocument(dataDir + "Locked Pass2.one", documentLoadOptions2);
Adding a Child Node to OneNote Notebook
Aspose.Note API lets you add/append a child node to a OneNote Notebook. The appendChildLast method exposed by the Notebook class allows you to achieve this.
1String dataDir = Utils.getSharedDataDir(AddChildNode.class) + "Notebook/";
2// Load a OneNote Notebook
3Notebook notebook = new Notebook(dataDir + "Notizbuch �ffnen.onetoc2");
4
5// Append a new child to the Notebook
6notebook.appendChild(new Document(dataDir + "Neuer Abschnitt 1.one"));
7
8dataDir = dataDir + "AddChildNodetoOneNoteNotebook_out.onetoc2";
9
10// Save the Notebook
11notebook.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.
1String dataDir = Utils.getSharedDataDir(RemoveChildNode.class) + "Notebook/";
2
3// Load a OneNote Notebook
4Notebook notebook = new Notebook(dataDir + "test.onetoc2");
5
6// Traverse through its child nodes for searching the desired child item
7for (INotebookChildNode child : new List<>(notebook))
8{
9 if (child.getDisplayName() == "Remove Me")
10 {
11 // Remove the Child Item from the Notebook
12 notebook.removeChild(child);
13 }
14}
15
16dataDir = dataDir + "RemoveChildNodeFromOneNoteNotebook_out.onetoc2";
17
18// Save the Notebook
19notebook.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. This article shows all the approaches to export Notebook to the image.
Exporting Notebook to Image
1String dataDir = Utils.getSharedDataDir(ConvertToImage.class) + "Notebook/";
2
3// Load a OneNote Notebook
4Notebook notebook = new Notebook(dataDir + "Notizbuch �ffnen.onetoc2");
5
6dataDir = dataDir + "ExportNotebooktoImage_out.png";
7
8// Save the Notebook
9notebook.save(dataDir);
Exporting Notebook to Image with Options
1String dataDir = Utils.getSharedDataDir(ConvertToImageWithOptions.class) + "Notebook/";
2// Load a OneNote Notebook
3Notebook notebook = new Notebook(dataDir + "test.onetoc2");
4
5NotebookImageSaveOptions notebookSaveOptions = new NotebookImageSaveOptions(SaveFormat.Png);
6
7ImageSaveOptions documentSaveOptions = notebookSaveOptions.getDocumentSaveOptions();
8
9documentSaveOptions.setResolution(400);
10
11dataDir = dataDir + "ExportNotebooktoImagewithOptions_out.png";
12
13// Save the Notebook
14notebook.save(dataDir, notebookSaveOptions);
Export to Image as a Flattened Notebook
1String dataDir = Utils.getSharedDataDir(ConvertToImageAsFlattenedNotebook.class) + "Notebook/";
2
3Notebook notebook = new Notebook(dataDir + "test.onetoc2");
4
5NotebookImageSaveOptions saveOptions = new NotebookImageSaveOptions(SaveFormat.Png);
6
7ImageSaveOptions documentSaveOptions = saveOptions.getDocumentSaveOptions();
8
9documentSaveOptions.setResolution(400);
10
11saveOptions.setFlatten(true);
12
13notebook.save(dataDir + "ExportImageasFlattenedNotebook_out.png", saveOptions);
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 Java x.x will be displayed against these fields.
Exporting Notebook to PDF
1String dataDir = Utils.getSharedDataDir(ConvertToPDF.class) + "Notebook/";
2
3// Load a OneNote Notebook
4Notebook notebook = new Notebook(dataDir + "Notizbuch �ffnen.onetoc2");
5
6dataDir = dataDir + "ExportNotebooktoPDF_out.pdf";
7
8// Save the Notebook
9notebook.save(dataDir);
Exporting Notebook to PDF with Options
1String dataDir = Utils.getSharedDataDir(ConvertToPDFWithOptions.class) + "Notebook/";
2
3// Load a OneNote Notebook
4Notebook notebook = new Notebook(dataDir + "Notizbuch �ffnen.onetoc2");
5
6NotebookPdfSaveOptions notebookSaveOptions = new NotebookPdfSaveOptions();
7
8PdfSaveOptions documentSaveOptions = notebookSaveOptions.getDocumentSaveOptions();
9
10documentSaveOptions.setPageSplittingAlgorithm (new KeepSolidObjectsAlgorithm());
11
12dataDir = dataDir + "ExportNotebooktoPDFwithOptions_out.pdf";
13
14// Save the Notebook
15notebook.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.
1String dataDir = Utils.getSharedDataDir(ExportNotebookToPDFAsFlattened.class) + "Notebook/";
2
3// Load a OneNote Notebook
4Notebook notebook = new Notebook(dataDir + "Notizbuch îffnen.onetoc2");
5
6NotebookPdfSaveOptions notebookSaveOptions = new NotebookPdfSaveOptions();
7
8notebookSaveOptions.setFlatten(true);
9
10dataDir = dataDir + "ExportNotebookToPDFAsFlattened_out.pdf";
11
12// Save the Notebook
13notebook.save(dataDir, notebookSaveOptions);
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 dataDir = Utils.getSharedDataDir(RetrieveDocumentsfromOneNoteNotebook.class) + "Notebook/";
2Notebook rootNotebook = new Notebook(dataDir + "test.onetoc2");
3
4List<Document> allDocuments = rootNotebook.getChildNodes(Document.class);
5for (Document document : allDocuments) {
6 System.out.println(document.getDisplayName());
7}
Read RichText Nodes from Microsoft OneNote Notebook
The following code snippet demonstrates how to print all RichText nodes from MS OneNote notebook:
1String dataDir = Utils.getSharedDataDir(LoadingNotebook.class) + "Notebook/";
2Notebook rootNotebook = new Notebook(dataDir + "test.onetoc2");
3
4List<RichText> allRichTextNodes = rootNotebook.getChildNodes(RichText.class);
5for (RichText richTextNode : allRichTextNodes) {
6 System.out.println(richTextNode.getText());
7}