Create OneNote file

OneNote files are used to keep the information organized as digital notes. You can create OneNote files from scratch using Java. In this article, you will explore different features like adding text, pages, or tags in .One files:

OneNote Document Creator – Java API Installation

Aspose.Note for Java API supports creating, editing, or manipulating OneNote files. You can easily make some API calls and the output file is generated as per your requirements. Please download the JAR file from the Downloads section, or use the following configuration in your Maven-based projects:

Repository:

 <repositories>
    <repository>
        <id>AsposeJavaAPI</id>
        <name>Aspose Java API</name>
        <url>https://repository.aspose.com/repo/</url>
    </repository>
</repositories>

Dependency:

 <dependencies>
    <dependency>
        <groupId>com.aspose</groupId>
        <artifactId>aspose-note</artifactId>
        <version>21.7</version>
        <classifier>jdk17</classifier>        
    </dependency>
</dependencies>

Create OneNote Document with Simple Rich Text Programmatically using Java

You can create a OneNote document with simple rich text with the following steps:

  1. Create an object of the Document class.
  2. Initialize Page and Outline class objects.
  3. Add RichText node.
  4. Save output OneNote document.

The following code shows how to create a OneNote document with simple rich text using Java:

// create an object of the Document class
Document doc = new Document();
// initialize Page class object
Page page = new Page(doc);
// initialize Outline class object
Outline outline = new Outline(doc);
// initialize OutlineElement class object
OutlineElement outlineElem = new OutlineElement(doc);
// initialize ParagraphStyle class object and set formatting properties
ParagraphStyle textStyle = new ParagraphStyle();
textStyle.setFontColor(Color.black);
textStyle.setFontName("Arial");
textStyle.setFontSize(10);
// initialize RichText class object and apply text style
RichText text = new RichText(doc);
text.setText("Hello OneNote text!");
text.setParagraphStyle(textStyle);
// add RichText node
outlineElem.appendChildLast(text);
// add OutlineElement node
outline.appendChildLast(outlineElem);
// add Outline node
page.appendChildLast(outline);
// add Page node
doc.appendChildLast(page);
// save OneNote document
doc.save("CreateOneNoteDocumentWithSimpleRichText_out.one", SaveFormat.One);

Create OneNote Document with Formatted Rich Text Programmatically using Java

Moving another step further, you can learn how to create a OneNote file with formatted rich text using Java:

  1. Create an object of the Document class.
  2. Initialize Page and TextStyle class objects.
  3. Format and add RichText node.
  4. Save output OneNote file.

The code below explains how to create a OneNote document with formatted rich text using Java:

// create an object of the Document class
Document doc = new Document();
// initialize Page class object
Page page = new Page(doc);
// initialize Title class object
Title title = new Title(doc);
// initialize TextStyle class object and set formatting properties
ParagraphStyle defaultTextStyle = new ParagraphStyle();
defaultTextStyle.setFontColor(Color.black);
defaultTextStyle.setFontName("Arial");
defaultTextStyle.setFontSize(10);
RichText titleText = new RichText(doc);
titleText.setText("Title!");
titleText.setParagraphStyle(defaultTextStyle);
Outline outline = new Outline(doc);
outline.setVerticalOffset(100);
outline.setHorizontalOffset(100);
OutlineElement outlineElem = new OutlineElement(doc);
// RunIndex = 5 means the style will be applied only to 0-4 characters.
// ("Hello")
TextStyle textStyleForHelloWord = new TextStyle();
textStyleForHelloWord.setFontColor(Color.red);
textStyleForHelloWord.setFontName("Arial");
textStyleForHelloWord.setFontSize(10);
textStyleForHelloWord.setRunIndex(5);
// RunIndex = 13 means the style will be applied only to 5-12
// characters. (" OneNote")
TextStyle textStyleForOneNoteWord = new TextStyle();
textStyleForOneNoteWord.setFontColor(Color.green);
textStyleForOneNoteWord.setFontName("Calibri");
textStyleForOneNoteWord.setFontSize(10);
textStyleForOneNoteWord.setItalic(true);
textStyleForOneNoteWord.setRunIndex(13);
// RunIndex = 18 means the style will be applied only to 13-17
// characters. (" text").
// Other characters ("!") will have the default style.
TextStyle textStyleForTextWord = new TextStyle();
textStyleForTextWord.setFontColor(Color.blue);
textStyleForTextWord.setFontName("Arial");
textStyleForTextWord.setFontSize(15);
textStyleForTextWord.setBold(true);
textStyleForTextWord.setItalic(true);
textStyleForTextWord.setRunIndex(18);
RichText text = new RichText(doc);
text.setText("Hello OneNote text!");
text.setParagraphStyle(defaultTextStyle);
text.getStyles().addItem(textStyleForHelloWord);
text.getStyles().addItem(textStyleForOneNoteWord);
text.getStyles().addItem(textStyleForTextWord);
title.setTitleText(titleText);
// set page title
page.setTitle(title);
// add RichText node
outlineElem.appendChildLast(text);
// add OutlineElement node
outline.appendChildLast(outlineElem);
// add Outline node
page.appendChildLast(outline);
// add Page node
doc.appendChildLast(page);
// save OneNote document
doc.save(dataDir + "CreateOneNoteDocument_out.one", SaveFormat.One);

Insert Pages in OneNote File Programmatically with Java

You can insert roots as well as sub-level pages in the OneNote document with the below steps:

  1. Initialize an instance of the Document class.
  2. Insert 3 pages while specifying their levels.
  3. Add nodes to the pages and insert pages in the OneNote document.
  4. Finally, save output OneNote document.

The code below elaborates how to insert pages in the OneNote file with Java:

// create an object of the Document class
Document doc = new Document();
// initialize Page class object and set its level
Page page1 = new Page(doc);
page1.setLevel((byte) 1);
// initialize Page class object and set its level
Page page2 = new Page(doc);
page1.setLevel((byte) 2);
// initialize Page class object and set its level
Page page3 = new Page(doc);
page1.setLevel((byte) 1);
// ---------- Adding nodes to first Page ----------
Outline outline = new Outline(doc);
OutlineElement outlineElem = new OutlineElement(doc);
ParagraphStyle textStyle = new ParagraphStyle();
textStyle.setFontColor(java.awt.Color.black);
textStyle.setFontName("David Transparent");
textStyle.setFontSize(10);
RichText text = new RichText(doc);
text.setText("First page.");
text.setParagraphStyle(textStyle);
outlineElem.appendChildLast(text);
outline.appendChildLast(outlineElem);
page1.appendChildLast(outline);
// ---------- Adding nodes to second Page ----------
Outline outline2 = new Outline(doc);
OutlineElement outlineElem2 = new OutlineElement(doc);
ParagraphStyle textStyle2 = new ParagraphStyle();
textStyle2.setFontColor(java.awt.Color.black);
textStyle2.setFontName("David Transparent");
textStyle2.setFontSize(10);
RichText text2 = new RichText(doc);
text2.setText("Second page.");
text2.setParagraphStyle(textStyle2);
outlineElem2.appendChildLast(text2);
outline2.appendChildLast(outlineElem2);
page2.appendChildLast(outline2);
// ---------- Adding nodes to third Page ----------
Outline outline3 = new Outline(doc);
OutlineElement outlineElem3 = new OutlineElement(doc);
ParagraphStyle textStyle3 = new ParagraphStyle();
textStyle3.setFontColor(java.awt.Color.black);
textStyle3.setFontName("Broadway");
textStyle3.setFontSize(10);
RichText text3 = new RichText(doc);
text3.setText("Third page.");
text3.setParagraphStyle(textStyle3);
outlineElem3.appendChildLast(text3);
outline3.appendChildLast(outlineElem3);
page3.appendChildLast(outline3);
// ---------- Add pages to the OneNote Document ----------
doc.appendChildLast(page1);
doc.appendChildLast(page2);
doc.appendChildLast(page3);
try {
doc.save(dataDir + "GenerateRootAndSubLevelPagesInOneNote_out.one", SaveFormat.One);
} catch (IOException e) {
}

Add Tags in OneNote Files in Java

You can tag the contents in OneNote files. The following steps explain how to add a text node with a tag:

  1. Create an object of the Document class.
  2. Add RichText and Initialize NoteTag class object.
  3. Save output OneNote file.

The following code shows how to add tags in the OneNote file using Java:

// Create an object of the Document class
Document doc = new Document();
// Initialize Page class object
Page page = new Page(doc);
// Initialize Outline class object
Outline outline = new Outline(doc);
// Initialize OutlineElement class object
OutlineElement outlineElem = new OutlineElement(doc);
ParagraphStyle textStyle = new ParagraphStyle();
textStyle.setFontColor(Color.BLACK);
textStyle.setFontName("Arial");
textStyle.setFontSize(10);
RichText text = new RichText(doc);
text.setText("OneNote text.");
text.setParagraphStyle(textStyle);
NoteTag noteTag = new NoteTag();
noteTag.setIcon(TagIcon.YellowStar);
text.getTags().addItem(noteTag);
// Add text node
outlineElem.appendChildLast(text);
// Add outline element node
outline.appendChildLast(outlineElem);
// Add outline node
page.appendChildLast(outline);
// Add page node
doc.appendChildLast(page);
dataDir = dataDir + "AddTextNodeWithTag_out.one";
// Save OneNote document
doc.save(dataDir);

Get Free API License

You can request a Free Temporary License for evaluating the API in its full capacity.

Conclusion

In this article, you have learned how to create OneNote files from scratch programmatically using Java. It covers all the details for inserting pages, adding rich text with a simple or formatted appearance in .One file. Moreover, you can check several other features by visiting the Documentation. Furthermore, please feel free to contact us at the Free Support Forum for any inquiries.

See Also