Working with Pages

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

Get Number of Pages from the OneNote Document

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

This example work as follows:

  1. Create an object of the Document class.
  2. Call getChildNodes method of the Document class.
  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 shows you how to get the number of pages from a OneNote file

1String dataDir = Utils.getSharedDataDir(GetPageCount.class) + "pages/";
2// Load the document into Aspose.Note
3Document doc = new Document(dataDir + "Sample1.one");
4
5// Get number of pages
6int count = doc.getChildNodes(Page.class).size();
7
8// Print page count
9System.out.printf("Total Pages: %s", 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 code below shows how to get information about each page in a OneNote document.

 1String dataDir = Utils.getSharedDataDir(GetInfo.class) + "pages/";
 2// Load the document into Aspose.Note
 3LoadOptions options = new LoadOptions();
 4Document doc = new Document(dataDir + "Sample1.one", options);
 5
 6// Get page revisions
 7List<Page> pages = doc.getChildNodes(Page.class);
 8
 9// Traverse list of pages
10for (Page pageRevision : pages) {
11	System.out.println("LastModifiedTime: " + pageRevision.getLastModifiedTime());
12	System.out.println("CreationTime: " + pageRevision.getCreationTime());
13	System.out.println("Title: " + pageRevision.getTitle());
14	System.out.println("Level: " + pageRevision.getLevel());
15	System.out.println("Author: " + pageRevision.getAuthor());
16}

Create OneNote Document with Root and Sub Level Pages

This article 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 below 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 AppendChildLast 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 TextStyle class defines the text formatting.
  5. Generate the OneNote document by calling the Save method of the Document object.
 1String dataDir = Utils.getSharedDataDir(CreateDocWithRootAndSubPages.class) + "pages/";
 2		
 3// create an object of the Document class
 4Document doc = new Document();
 5// initialize Page class object and set its level
 6Page page1 = new Page(doc);
 7page1.setLevel((byte) 1);
 8// initialize Page class object and set its level
 9Page page2 = new Page(doc);
10page1.setLevel((byte) 2);
11// initialize Page class object and set its level
12Page page3 = new Page(doc);
13page1.setLevel((byte) 1);
14
15// ---------- Adding nodes to first Page ----------
16Outline outline = new Outline(doc);
17OutlineElement outlineElem = new OutlineElement(doc);
18ParagraphStyle textStyle = new ParagraphStyle();
19textStyle.setFontColor(java.awt.Color.black);
20textStyle.setFontName("David Transparent");
21textStyle.setFontSize(10);
22
23RichText text = new RichText(doc);
24text.setText("First page.");
25text.setParagraphStyle(textStyle);
26
27outlineElem.appendChildLast(text);
28outline.appendChildLast(outlineElem);
29page1.appendChildLast(outline);
30
31// ---------- Adding nodes to second Page ----------
32Outline outline2 = new Outline(doc);
33OutlineElement outlineElem2 = new OutlineElement(doc);
34// var textStyle2 = new TextStyle { FontColor = Color.Black, FontName =
35// "Arial", FontSize = 10 };
36ParagraphStyle textStyle2 = new ParagraphStyle();
37textStyle2.setFontColor(java.awt.Color.black);
38textStyle2.setFontName("David Transparent");
39textStyle2.setFontSize(10);
40
41// var text2 = new RichText(doc) { Text = "Second page.", DefaultStyle =
42// textStyle2 };
43RichText text2 = new RichText(doc);
44text2.setText("Second page.");
45text2.setParagraphStyle(textStyle2);
46
47outlineElem2.appendChildLast(text2);
48outline2.appendChildLast(outlineElem2);
49page2.appendChildLast(outline2);
50
51// ---------- Adding nodes to third Page ----------
52Outline outline3 = new Outline(doc);
53OutlineElement outlineElem3 = new OutlineElement(doc);
54ParagraphStyle textStyle3 = new ParagraphStyle();
55textStyle3.setFontColor(java.awt.Color.black);
56textStyle3.setFontName("Broadway");
57textStyle3.setFontSize(10);
58
59RichText text3 = new RichText(doc);
60text3.setText("Third page.");
61text3.setParagraphStyle(textStyle3);
62
63outlineElem3.appendChildLast(text3);
64outline3.appendChildLast(outlineElem3);
65page3.appendChildLast(outline3);
66
67// ---------- Add pages to the OneNote Document ----------
68doc.appendChildLast(page1);
69doc.appendChildLast(page2);
70doc.appendChildLast(page3);
71
72try {
73	doc.save(dataDir + "GenerateRootAndSubLevelPagesInOneNote_out.bmp", SaveFormat.Bmp);
74
75} catch (IOException e) {
76
77}

Working with Page Revision Information

 1// The path to the documents directory.
 2String dataDir = Utils.getSharedDataDir(WorkingWithPageRevisions.class) + "pages\\";
 3
 4// Load OneNote document and get first child
 5Document document = new Document(dataDir + "Sample1.one");
 6Page page = document.getFirstChild();
 7
 8// Reading Content Revision Summary for this page
 9RevisionSummary pageRevisionInfo = page.getPageContentRevisionSummary();
10
11System.out.println(String.format("Author:\t%s\nModified:\t%s",
12        pageRevisionInfo.getAuthorMostRecent(),
13        pageRevisionInfo.getLastModifiedTime().toString()));
14
15// Update Page Revision Summary for this page
16pageRevisionInfo.setAuthorMostRecent("New Author");
17Calendar modifiedDate = Calendar.getInstance();
18pageRevisionInfo.setLastModifiedTime(modifiedDate.getTime());
19document.save(dataDir + "WorkingWithPageRevisions_out.one");

Working with Page History

Get All Revisions of a Specific Page

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 case of such a file with a huge amount of history data consider using LoadHistory=false, to decrease memory and CPU usage.

The code sample below shows how to get a particular Page and then get its history revisions detail.

 1String dataDir = Utils.getSharedDataDir(GetPageRevisions.class) + "pages/";
 2
 3LoadOptions loadOptions = new LoadOptions();
 4loadOptions.setLoadHistory(true);
 5// load OneNote document
 6Document document = new Document(dataDir + "Sample1.one", loadOptions);
 7// get first page
 8Page firstPage = document.getFirstChild();
 9for (Page pageRevision : document.getPageHistory(firstPage)) {
10	// Use pageRevision like a regular page.
11	System.out.println("LastModifiedTime: " + pageRevision.getLastModifiedTime());
12	System.out.println("CreationTime: " + pageRevision.getCreationTime());
13	System.out.println("Title: " + pageRevision.getTitle());
14	System.out.println("Level: " + pageRevision.getLevel());
15	System.out.println("Author: " + pageRevision.getAuthor());
16	System.out.println();
17}

Roll Back To Previous Page Version

 1String dataDir = Utils.getSharedDataDir(InsertPages.class) + "pages/";
 2
 3// Load OneNote document and get first child
 4Document document = new Document(dataDir + "Sample1.one");
 5
 6Page page = document.getFirstChild();
 7
 8PageHistory pageHistory = document.getPageHistory(page);
 9
10document.removeChild(page);
11
12document.appendChildLast(pageHistory.get_Item(pageHistory.size() - 1));
13
14document.save(dataDir + "RollBackToPreviousPageVersion_out.one");

Push Current Page Version To History

 1String dataDir = Utils.getSharedDataDir(InsertPages.class) + "pages/";
 2// Load OneNote document and get first child
 3Document document = new Document(dataDir + "Sample1.one");
 4Page page = document.getFirstChild();
 5
 6PageHistory pageHistory = document.getPageHistory(page);
 7
 8pageHistory.addItem(page.deepClone());
 9
10document.save(dataDir + "PushCurrentPageVersion_out.one");

Modify Page History

 1String dataDir = Utils.getSharedDataDir(InsertPages.class) + "pages/";
 2// Load OneNote document and get first child
 3Document document = new Document(dataDir + "Sample1.one");
 4
 5Page page = document.getFirstChild();
 6
 7PageHistory pageHistory = document.getPageHistory(page);
 8
 9pageHistory.removeRange(0, 1);
10
11pageHistory.set_Item(0, new Page(document));
12
13pageHistory.get_Item(1).getTitle().getTitleText().setText("New Title");
14
15pageHistory.addItem(new Page(document));
16
17pageHistory.insertItem(1, new Page(document));
18
19document.save(dataDir + "ModifyPageHistory_out.one");

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 method of history page as shown below.

 1String dataDir = Utils.getSharedDataDir(GetInfo.class) + "pages/";
 2// Load the document into Aspose.Note
 3LoadOptions options = new LoadOptions();
 4Document doc = new Document(dataDir + "Aspose.one", options);
 5
 6DateFormat df = new SimpleDateFormat("dd.MM.yyyy HH.mm.ss");
 7		
 8PageHistory history = doc.getPageHistory(doc.getFirstChild());
 9for (int i = 0; i < history.size(); i++)
10{
11	Page historyPage = history.get_Item(i);
12	System.out.format(" %d. Author: %s, %s",
13                  i,
14                  historyPage.getPageContentRevisionSummary().getAuthorMostRecent(),
15                  df.format(historyPage.getPageContentRevisionSummary().getLastModifiedTime()));
16	System.out.println(historyPage.isConflictPage() ? ", IsConflict: true" : "");
17    // By default conflict pages are just skipped on saving.
18    // If mark it as non-conflict then it will be saved as usual one in the history.
19    if (historyPage.isConflictPage())
20        historyPage.setConflictPage(false);
21}
22
23doc.save(dataDir + "ConflictPageManipulation_out.one", SaveFormat.One);
Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.