Working with Pages of OneNote Document using C#

Get Number of Pages from the OneNote Document

Aspose.Note for .NET supports retrieving the number of pages from a OneNote document.

This example works as follows:

  1. Create an object of the Document class.
  2. Call Document class’ GetChildNodes method.
  3. Get a list of page nodes.
  4. Get the number of pages via Count property.
  5. Display the count on the output screen.

The following code snippet demonstrates how to get the number of pages from a OneNote file

 1// The path to the documents directory.
 2string dataDir = RunExamples.GetDataDir_Pages();
 3
 4// Load the document into Aspose.Note.
 5Document oneFile = new Document(dataDir + "Aspose.one");
 6
 7// Get number of pages
 8int count = oneFile.GetChildNodes<Page>().Count;
 9
10// Print count on the output screen
11Console.WriteLine(count);

Get Information of Each Page from the OneNote Document

The Page class provides all the properties related to a page of a OneNote document. All the pages of the OneNote file are contained by page nodes of the Document object.

The following code example demonstrates how to get information about each page in a OneNote document.

 1// The path to the documents directory.
 2string dataDir = RunExamples.GetDataDir_Pages();
 3
 4// Load the document into Aspose.Note.
 5Document oneFile = new Document(dataDir + "Aspose.one");
 6
 7// Get all Page nodes
 8IList<Page> pages = oneFile.GetChildNodes<Page>();
 9
10foreach (Page page in pages)
11{
12    Console.WriteLine("LastModifiedTime: {0}", page.LastModifiedTime);
13    Console.WriteLine("CreationTime: {0}", page.CreationTime);
14    Console.WriteLine("Title: {0}", page.Title);
15    Console.WriteLine("Level: {0}", page.Level);
16    Console.WriteLine("Author: {0}", page.Author);
17    Console.WriteLine();
18}

Create a OneNote Document with Root and Sub Level Pages

This topic explains how developers can generate OneNote document including various pages at root or sub-level positions. Aspose.Note APIs lets the developer to programmatically choose the level of the page.

Generating Root and Sub Level Pages in OneNote

Please see a brief description to create a 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 separate objects of Outline, OutlineElement, and RichText classes for each Page object by passing the Document class object. Calling the AppendChild method of the OutlineElement, Outline, Page, and Document classes adds an appropriate new node that can be further used to add other nodes to the OneNote document.
  4. The ParagraphStyle class defines the text formatting.
  5. Generate the OneNote document by calling the Save method of the Document object.
 1// The path to the documents directory.
 2string dataDir = RunExamples.GetDataDir_Pages();
 3
 4// Create an object of the Document class
 5Document doc = new Document();
 6// Initialize Page class object and set its level
 7Aspose.Note.Page page1 = new Aspose.Note.Page(doc) { Level = 1 };
 8// Initialize Page class object and set its level
 9Aspose.Note.Page page2 = new Aspose.Note.Page(doc) { Level = 2 };
10// Initialize Page class object and set its level
11Aspose.Note.Page page3 = new Aspose.Note.Page(doc) { Level = 1 };
12
13/*---------- Adding nodes to first Page ----------*/
14Outline outline = new Outline(doc);
15OutlineElement outlineElem = new OutlineElement(doc);
16ParagraphStyle textStyle = new ParagraphStyle { FontColor = Color.Black, FontName = "Arial", FontSize = 10 };
17RichText text = new RichText(doc) { Text = "First page.", ParagraphStyle = textStyle };
18outlineElem.AppendChildLast(text);
19outline.AppendChildLast(outlineElem);
20page1.AppendChildLast(outline);
21
22/*---------- Adding nodes to second Page ----------*/
23var outline2 = new Outline(doc);
24var outlineElem2 = new OutlineElement(doc);
25var textStyle2 = new ParagraphStyle { FontColor = Color.Black, FontName = "Arial", FontSize = 10 };
26var text2 = new RichText(doc) { Text = "Second page.", ParagraphStyle = textStyle2 };
27outlineElem2.AppendChildLast(text2);
28outline2.AppendChildLast(outlineElem2);
29page2.AppendChildLast(outline2);
30
31/*---------- Adding nodes to third Page ----------*/
32var outline3 = new Outline(doc);
33var outlineElem3 = new OutlineElement(doc);
34var textStyle3 = new ParagraphStyle { FontColor = Color.Black, FontName = "Arial", FontSize = 10 };
35var text3 = new RichText(doc) { Text = "Third page.", ParagraphStyle = textStyle3 };
36outlineElem3.AppendChildLast(text3);
37outline3.AppendChildLast(outlineElem3);
38page3.AppendChildLast(outline3);
39
40/*---------- Add pages to the OneNote Document ----------*/
41doc.AppendChildLast(page1);
42doc.AppendChildLast(page2);
43doc.AppendChildLast(page3);
44
45dataDir = dataDir + "CreateDocWithRootAndSubPages_out.one";
46// Save OneNote document
47doc.Save(dataDir);

Working with Page Revision Information

 1// The path to the documents directory.
 2string dataDir = RunExamples.GetDataDir_Pages();
 3
 4// Load OneNote document and get first child           
 5Document document = new Document(dataDir + "Aspose.one");
 6Page page = document.FirstChild;
 7
 8// Reading Content Revision Summary for this page
 9var pageRevisionInfo = page.PageContentRevisionSummary;
10
11Console.WriteLine(string.Format(
12    "Author:\t{0}\nModified:\t{1}",
13    pageRevisionInfo.AuthorMostRecent,
14    pageRevisionInfo.LastModifiedTime.ToString("dd.MM.yyyy HH:mm:ss")));
15
16// Update Page Revision Summary for this page
17pageRevisionInfo.AuthorMostRecent = "New Author";
18pageRevisionInfo.LastModifiedTime = DateTime.Now;
19document.Save(dataDir + "WorkingWithPageRevisions_out.one");

Working with Page History

Get All Revisions of a Specific Page

Aspose.Note for .NET APIs provide support to get the history of a specific Page, e.g. how many times a page is updated, content tracking changes, and much more. All the pages of the OneNote file are contained by page nodes of the Document object.

There is a LoadHistory property of the LoadOptions class which tells whether the document loader should read and parse entire document history. The default value is true.

In the case of a file with a huge amount of history, data consider using LoadHistory=false, to decrease memory and CPU usage.

The following code example demonstrates how to get a particular Page and then get its history revisions detail.

 1// The path to the documents directory.
 2string dataDir = RunExamples.GetDataDir_Pages();
 3
 4// Load OneNote document
 5Document document = new Document(dataDir + "Aspose.one", new LoadOptions { LoadHistory = true });
 6// Get first page
 7Page firstPage = document.FirstChild;
 8foreach (Page pageRevision in document.GetPageHistory(firstPage))
 9{
10    /*Use pageRevision like a regular page.*/
11    Console.WriteLine("LastModifiedTime: {0}", pageRevision.LastModifiedTime);
12    Console.WriteLine("CreationTime: {0}", pageRevision.CreationTime);
13    Console.WriteLine("Title: {0}", pageRevision.Title);
14    Console.WriteLine("Level: {0}", pageRevision.Level);
15    Console.WriteLine("Author: {0}", pageRevision.Author);
16    Console.WriteLine();
17}

Roll Back to Previous Page Version

In order to roll back to a previous page version, the latest page needs to be removed from the document and the one before this one needs to be restored to the document as shown in the following code sample.

 1// The path to the documents directory.
 2string dataDir = RunExamples.GetDataDir_Pages();
 3
 4// Load OneNote document and get first child           
 5Document document = new Document(dataDir + "Aspose.one");
 6Page page = document.FirstChild;           
 7Page lastPage = null;
 8foreach (Page pageRevision in document.GetPageHistory(page))
 9{
10    lastPage = pageRevision;
11}
12document.RemoveChild(page);
13
14document.AppendChildLast(lastPage);
15
16document.Save(dataDir + "RollBackRevisions_out.one");          

Push Current Page Version to History

The current page can be added to history by cloning it using the Page.Clone method.

 1// The path to the documents directory.
 2string dataDir = RunExamples.GetDataDir_Pages();
 3
 4// Load OneNote document and get first child           
 5Document document = new Document(dataDir + "Aspose.one");
 6Page page = document.FirstChild;
 7
 8var pageHistory = document.GetPageHistory(page);
 9
10pageHistory.Add(page.Clone());
11
12document.Save(dataDir + "PushCurrentPageVersion_out.one");

Modify Page History

The current page can be added to history by cloning it using the Page.Clone method.

 1// The path to the documents directory.
 2string dataDir = RunExamples.GetDataDir_Pages();
 3
 4// Load OneNote document and get first child           
 5Document document = new Document(dataDir + "Aspose.one");
 6Page page = document.FirstChild;
 7
 8var pageHistory = document.GetPageHistory(page);
 9
10pageHistory.RemoveRange(0, 1);
11
12pageHistory[0] = new Page(document);
13if (pageHistory.Count > 1)
14{
15    pageHistory[1].Title.TitleText.Text = "New Title";
16
17    pageHistory.Add(new Page(document));
18
19    pageHistory.Insert(1, new Page(document));
20
21    document.Save(dataDir + "ModifyPageHistory_out.one");
22}

Working with Conflict Pages

A document can have pages with conflict pages in its history. Such pages are not saved to document by default. Aspose.Note API lets you manipulate such a page history using the IsConflictPage property of the history page as shown below.

 1string dataDir = RunExamples.GetDataDir_Pages();
 2
 3// Load OneNote document
 4Document doc = new Document(dataDir + "Aspose.one", new LoadOptions { LoadHistory = true });
 5
 6var history = doc.GetPageHistory(doc.FirstChild);
 7for (int i = 0; i < history.Count; i++)
 8{
 9    var historyPage = history[i];
10    Console.Write("    {0}. Author: {1}, {2:dd.MM.yyyy hh.mm.ss}",
11                    i,
12                    historyPage.PageContentRevisionSummary.AuthorMostRecent,
13                    historyPage.PageContentRevisionSummary.LastModifiedTime);
14    Console.WriteLine(historyPage.IsConflictPage ? ", IsConflict: true" : string.Empty);
15    // By default conflict pages are just skipped on saving.
16    // If mark it as non-conflict then it will be saved as usual one in the history.
17    if (historyPage.IsConflictPage)
18        historyPage.IsConflictPage = false;
19}
20
21doc.Save(dataDir + "ConflictPageManipulation_out.one", SaveFormat.One);

Cloning Page with History

 1// The path to the documents directory.
 2string dataDir = RunExamples.GetDataDir_Pages();
 3
 4// Load OneNote document
 5Document document = new Document(dataDir + "Aspose.one", new LoadOptions { LoadHistory = true });
 6
 7//Clone into new document without history
 8var cloned = new Document();
 9cloned.AppendChildLast(document.FirstChild.Clone());
10
11//Clone into new document with history
12cloned = new Document();
13cloned.AppendChildLast(document.FirstChild.Clone(true));
Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.