Working with OneNote Document

Extract OneNote Content using DocumentVisitor

Use the DocumentVisitor class to implement this usage scenario. This class corresponds to the well-known Visitor design pattern. With the DocumentVisitor, you can define and execute custom operations that require enumeration over the document tree.

DocumentVisitor provides a set of VisitXXX methods that are invoked when a particular document element (node) is encountered. For example, DocumentVisitor.VisitRichTextStart is called when the text item starts and DocumentVisitor.VisitImageEnd is called when the visitor has visited all the children nodes. Each DocumentVisitor.VisitXXX method accepts the corresponding object that it encounters so you can use it as needed.

These are the steps you should follow to programmatically determine and extract various parts of a document:

  1. Create a class derived from DocumentVisitor.
  2. Override and provide implementations for some or all of the DocumentVisitor.VisitXXX methods to perform custom operations.
  3. Call Node.Accept on the node from where you want to start the enumeration. For example, if you want to enumerate the whole document, use Document.Accept.

DocumentVisitor provides default implementations for all of the DocumentVisitor.VisitXXX methods. This makes it easier to create new document visitors as only the methods required for the particular visitor need to be overridden. It is not necessary to override all of the visitor methods.

This example shows how to use the Visitor pattern to add new operations to the Aspose.Note object model. In this case, we create a simple document converted into a text format.

Aspose.Note Document Object Model

Node Classes

When Aspose.Note reads a OneNote document into memory, objects of different types are created to represent various document elements. Every RichText of text, title, table, and even the OneNote itself is a node. Aspose.Note defines a class for every type of document node. The following illustration is a UML class diagram that shows inheritance between node classes of the Aspose.Note Document Object Model (DOM). The names of abstract classes are Node and CompositeNode. Please note that the Aspose.Note DOM also contains non-node classes such as TextStyle, Margins, NumberList, etc that do not participate in the inheritance and they are not shown on this diagram.

todo:image_alt_text

The following table lists Aspose.Note node classes and their short descriptions.

Aspose.Note ClassCategoryDescription
DocumentDocumentA document object that, as the root of the document tree, provides access to the entire OneNote document.
TitleTitleA page title of OneNote document.
PagePageA page of OneNote document.
AttachedFileFileRepresents an attached file within the OneNote document.
ImageImageRepresents an image file within the OneNote document.
OutlineGroupOutlineRepresents a group of outlines.
OutlineElementOutlineRepresents an outline element.
OutlineOutlineRepresents an outline.
TableTablesA table in a OneNote document.
TableCellTablesA cell of a table row.
TableRowTablesA row of a table.
RichTextTextA RichText of text with consistent formatting.
ClassDescription
NodeAbstract base class for all nodes of a OneNote document. Provides basic functionality of a child node.
CompositeNodeBase class for nodes that can contain other nodes. Provides operations to access, insert, remove and select child nodes.

Distinguish Nodes by NodeType

Although the class of the node is sufficient enough to distinguish different nodes from each other, Aspose.Note provides the NodeType enumeration to simplify some API tasks such as selecting nodes of a specific type. The type of each node can be obtained using the NodeType enumeration. This returns a NodeType enumeration value. For example, a RichText node (represented by the RichText class) returns NodeType.RichText, a table node (represented by the Table class) returns NodeType.Table, and so on.

Create an Empty OneNote Document with a Page Title

Aspose.Note for Java supports generating OneNote documents from scratch.

Use this code snippet to create from scratch a new document with a title.

This example works as follows:

  1. Create an object of the Document class.
  2. Initialize Page class object by passing the Document class object.
  3. Set Page title using Page.setTitle() method.
  4. Call Document class’ appendChildLast method and pass Page class object.
  5. Finally, save it by calling save method of Document class.

The following code snippet shows you how to create an empty OneNote document with a page title.

Getting File Format Information

Aspose.Note API supports Microsoft OneNote as well as on OneNote online file formats. The lateral has some limitations such as no support for attachments and images in the document is not available. In order to guide the users about these two different types of documents, the API provides the method to determine the file format of the OneNote document so as to know the limitations until these are fully supported.

This article shows how to retrieve the file format information of the OneNote document using the FileFormat property of the Document class.