Introduction
Contents
[
Hide
Show
]Create a Hello World OneNote document Using API
This article explains how to create a OneNote document from scratch using the simple APIs provided by Aspose.Note for .NET. 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 AppendChild 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 ParagraphStyle class defines the text formatting. Similarly, the ParagraphStyle property exposed by RichText class allows applying to format on the text. Text style settings to be used if there is no matching TextStyle object in Styles collection either this object doesn’t specify a needed setting.
- Generate the OneNote document by calling the Save method of the Document object.
Create a OneNote Document with Simple RichText
1// The path to the documents directory.
2string dataDir = RunExamples.GetDataDir_LoadingAndSaving();
3
4// Create an object of the Document class
5Document doc = new Document();
6// Initialize Page class object
7Page page = new Page(doc);
8// Initialize Outline class object
9Outline outline = new Outline(doc);
10// Initialize OutlineElement class object
11OutlineElement outlineElem = new OutlineElement(doc);
12
13// Initialize TextStyle class object and set formatting properties
14ParagraphStyle textStyle = new ParagraphStyle { FontColor = Color.Black, FontName = "Arial", FontSize = 10 };
15// Initialize RichText class object and apply text style
16RichText text = new RichText(doc) { Text = "Hello OneNote text!", ParagraphStyle = textStyle };
17
18// Add RichText node
19outlineElem.AppendChildLast(text);
20// Add OutlineElement node
21outline.AppendChildLast(outlineElem);
22// Add Outline node
23page.AppendChildLast(outline);
24// Add Page node
25doc.AppendChildLast(page);
26
27dataDir = dataDir + "CreateDocWithSimpleRichText_out.one";
28// Save OneNote document
29doc.Save(dataDir);
Create a OneNote Document with Formatted RichText
1// The path to the documents directory.
2string dataDir = RunExamples.GetDataDir_LoadingAndSaving();
3
4// Create an object of the Document class
5Document doc = new Document();
6// Initialize Page class object
7Aspose.Note.Page page = new Aspose.Note.Page(doc);
8// Initialize Title class object
9Title title = new Title(doc);
10// Initialize TextStyle class object and set formatting properties
11ParagraphStyle defaultTextStyle = new ParagraphStyle
12{
13 FontColor = Color.Black,
14 FontName = "Arial",
15 FontSize = 10
16};
17
18RichText titleText = new RichText(doc)
19{
20 Text = "Title!",
21 ParagraphStyle = defaultTextStyle
22};
23Outline outline = new Outline(doc)
24{
25 VerticalOffset = 100,
26 HorizontalOffset = 100
27};
28OutlineElement outlineElem = new OutlineElement(doc);
29// RunIndex = 5 means the style will be applied only to 0-4 characters. ("Hello")
30TextStyle textStyleForHelloWord = new TextStyle
31{
32 FontColor = Color.Red,
33 FontName = "Arial",
34 FontSize = 10,
35 RunIndex = 5,
36};
37// RunIndex = 13 means the style will be applied only to 5-12 characters. (" OneNote")
38TextStyle textStyleForOneNoteWord = new TextStyle
39{
40 FontColor = Color.Green,
41 FontName = "Calibri",
42 FontSize = 10,
43 IsItalic = true,
44 RunIndex = 13,
45};
46// RunIndex = 18 means the style will be applied only to 13-17 characters. (" text").
47// Other characters ("!") will have the default style.
48TextStyle textStyleForTextWord = new TextStyle
49{
50 FontColor = Color.Blue,
51 FontName = "Arial",
52 FontSize = 15,
53 IsBold = true,
54 IsItalic = true,
55 RunIndex = 18,
56};
57RichText text = new RichText(doc)
58{
59 Text = "Hello OneNote text!",
60 ParagraphStyle = defaultTextStyle,
61 Styles = { textStyleForHelloWord, textStyleForOneNoteWord, textStyleForTextWord }
62};
63
64title.TitleText = titleText;
65// Set page title
66page.Title = 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
76dataDir = dataDir + "CreateDocWithFormattedRichText_out.one";
77// Save OneNote document
78doc.Save(dataDir);