Introduction
Contents
[
Hide
Show
]Create a Hello World OneNote Document using the API
This article explains how to create a OneNote document from scratch using the simple APIs provided by Aspose.Note for Java. To create a OneNote document, add page, outline, outline element and text dynamically using the set of classes packaged in the Aspose.Note namespace.
Please follow these steps to create a OneNote document using the Aspose.Note APIs:
- Create an instance of the Document class that represents a OneNote document.
- If you have purchased a license, then embed the code to apply for that license with the help of the License class.
- Initialize one object of Page, Outline, OutlineElement and RichText classes bypassing 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 new nodes to the OneNote document.
- The TextStyle class defines the text formatting. Similarly, the DefaultStyle property exposed by RichText class allows applying to format on the text.
- Generate the OneNote document by calling the Save method of the Document object.
These articles show how to:
Create a OneNote Document with Simple RichText
1String dataDir = Utils.getSharedDataDir(CreateOneNoteDocumentWithSimpleRichText.class) + "introduction/";
2
3// For complete examples and data files, please go to
4// https://github.com/aspose-note/Aspose.Note-for-Java
5// create an object of the Document class
6Document doc = new Document();
7// initialize Page class object
8Page page = new Page(doc);
9// initialize Outline class object
10Outline outline = new Outline(doc);
11// initialize OutlineElement class object
12OutlineElement outlineElem = new OutlineElement(doc);
13
14// initialize ParagraphStyle class object and set formatting properties
15ParagraphStyle textStyle = new ParagraphStyle();
16textStyle.setFontColor(Color.black);
17textStyle.setFontName("Arial");
18textStyle.setFontSize(10);
19
20// initialize RichText class object and apply text style
21RichText text = new RichText(doc);
22text.setText("Hello OneNote text!");
23text.setParagraphStyle(textStyle);
24
25// add RichText node
26outlineElem.appendChildLast(text);
27// add OutlineElement node
28outline.appendChildLast(outlineElem);
29// add Outline node
30page.appendChildLast(outline);
31// add Page node
32doc.appendChildLast(page);
33// save OneNote document
34doc.save(dataDir + "CreateOneNoteDocumentWithSimpleRichText_out.pdf", SaveFormat.Pdf);
Create a OneNote Document with Formatted RichText
1String dataDir = Utils.getSharedDataDir(CreateOneNoteDocumentWithFormattedRichText.class) + "introduction/";
2
3// For complete examples and data files, please go to
4// https://github.com/aspose-note/Aspose.Note-for-Java
5// create an object of the Document class
6Document doc = new Document();
7// initialize Page class object
8Page page = new Page(doc);
9// initialize Title class object
10Title title = new Title(doc);
11// initialize TextStyle class object and set formatting properties
12ParagraphStyle defaultTextStyle = new ParagraphStyle();
13defaultTextStyle.setFontColor(Color.black);
14defaultTextStyle.setFontName("Arial");
15defaultTextStyle.setFontSize(10);
16
17RichText titleText = new RichText(doc);
18titleText.setText("Title!");
19titleText.setParagraphStyle(defaultTextStyle);
20
21// titleText.setTitleText(true);
22// IsTitleText = true
23
24Outline outline = new Outline(doc);
25outline.setVerticalOffset(100);
26outline.setHorizontalOffset(100);
27
28OutlineElement outlineElem = new OutlineElement(doc);
29// RunIndex = 5 means the style will be applied only to 0-4 characters.
30// ("Hello")
31TextStyle textStyleForHelloWord = new TextStyle();
32textStyleForHelloWord.setFontColor(Color.red);
33textStyleForHelloWord.setFontName("Arial");
34textStyleForHelloWord.setFontSize(10);
35textStyleForHelloWord.setRunIndex(5);
36
37// RunIndex = 13 means the style will be applied only to 5-12
38// characters. (" OneNote")
39TextStyle textStyleForOneNoteWord = new TextStyle();
40textStyleForOneNoteWord.setFontColor(Color.green);
41textStyleForOneNoteWord.setFontName("Calibri");
42textStyleForOneNoteWord.setFontSize(10);
43textStyleForOneNoteWord.setItalic(true);
44textStyleForOneNoteWord.setRunIndex(13);
45
46// RunIndex = 18 means the style will be applied only to 13-17
47// characters. (" text").
48// Other characters ("!") will have the default style.
49TextStyle textStyleForTextWord = new TextStyle();
50textStyleForTextWord.setFontColor(Color.blue);
51textStyleForTextWord.setFontName("Arial");
52textStyleForTextWord.setFontSize(15);
53textStyleForTextWord.setBold(true);
54textStyleForTextWord.setItalic(true);
55textStyleForTextWord.setRunIndex(18);
56
57RichText text = new RichText(doc);
58text.setText("Hello OneNote text!");
59text.setParagraphStyle(defaultTextStyle);
60text.getStyles().addItem(textStyleForHelloWord);
61text.getStyles().addItem(textStyleForOneNoteWord);
62text.getStyles().addItem(textStyleForTextWord);
63
64title.setTitleText(titleText);
65// set page title
66page.setTitle(title);
67// add RichText node
68outlineElem.appendChildLast(text);
69// add OutlineElement node
70outline.appendChildLast(outlineElem);
71// add Outline node
72page.appendChildLast(outline);
73// add Page node
74doc.appendChildLast(page);
75// save OneNote document
76doc.save(dataDir + "CreateOneNoteDocument_out.pdf", SaveFormat.Pdf);