Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active August 3, 2021 15:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/9105f6e7434342a8499dd10ec4345f72 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/9105f6e7434342a8499dd10ec4345f72 to your computer and use it in GitHub Desktop.
Create OneNote files Programmatically 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);
// 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);
// 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 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) {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment