Create and Load a OneNote Document

Aspose.Note API supports loading Microsoft.Note documents using its Document Object Model. It supports loading Microsoft OneNote 2010, 2013 and OneNote Online (SharePoint) file formats. It provides support for .one as well as .onetoc2 file formats.

Create a OneNote and Save into HTML Format

Aspose.Note supports creating a OneNote file and then saving it into HTML file format. To use this feature, import the Aspose.Note.Saving namespace into your application project. It has numerous valuable classes for rendering, for example, HtmlSaveOptions, ImageSaveOptions, PdfSaveOptions, and SaveOptions.

Aspose.Note for .NET offers the Document class that represents a OneNote file. The Document class exposes the Save method that can be called to convert the OneNote file into the HTML file format. The HtmlSaveOptions class provides options for creating HTML file, such as PageIndex, PageCount, SaveFormat and others.

Creating a OneNote Document and Saving into HTML using the Default Options

The following code example demonstrates how to create a OneNote and save into the HTML using the default options.

 1// The path to the documents directory.
 2string dataDir = RunExamples.GetDataDir_LoadingAndSaving();
 3
 4// Initialize OneNote document
 5Document doc = new Document();
 6Aspose.Note.Page page = new Aspose.Note.Page(doc);
 7
 8// Default style for all text in the document.
 9ParagraphStyle textStyle = new ParagraphStyle { FontColor = Color.Black, FontName = "Arial", FontSize = 10 };
10page.Title = new Title(doc)
11{
12    TitleText = new RichText(doc) { Text = "Title text.", ParagraphStyle = textStyle },
13    TitleDate = new RichText(doc) { Text = new DateTime(2011, 11, 11).ToString("D", CultureInfo.InvariantCulture), ParagraphStyle = textStyle },
14    TitleTime = new RichText(doc) { Text = "12:34", ParagraphStyle = textStyle }
15};
16doc.AppendChildLast(page);
17dataDir = dataDir + "CreateOneNoteDocAndSaveToHTML_out.html";
18// Save as HTML format
19doc.Save(dataDir);

Creating a OneNote Document and Saving a Page Range into HTML

The following code example demonstrates how to create a OneNote and save a page range into HTML using the HtmlSaveOptions class. It sets the PageCount and PageIndex properties.

 1// The path to the documents directory.
 2string dataDir = RunExamples.GetDataDir_LoadingAndSaving();
 3
 4// Initialize OneNote document
 5Document doc = new Document();
 6
 7Aspose.Note.Page page = new Aspose.Note.Page(doc);
 8// Default style for all text in the document.
 9ParagraphStyle textStyle = new ParagraphStyle { FontColor = Color.Black, FontName = "Arial", FontSize = 10 };
10page.Title = new Title(doc)
11{
12    TitleText = new RichText(doc) { Text = "Title text.", ParagraphStyle = textStyle },
13    TitleDate = new RichText(doc) { Text = new DateTime(2011, 11, 11).ToString("D", CultureInfo.InvariantCulture), ParagraphStyle = textStyle },
14    TitleTime = new RichText(doc) { Text = "12:34", ParagraphStyle = textStyle }
15};
16doc.AppendChildLast(page);
17
18dataDir = dataDir + "CreateAndSavePageRange_out.html";
19// Save as HTML format
20doc.Save(dataDir, new HtmlSaveOptions
21{
22    PageCount = 1,
23    PageIndex = 0
24});

Save to Memory Stream with Embedded Resources

 1string dataDir = RunExamples.GetDataDir_LoadingAndSaving();
 2var document = new Aspose.Note.Document(dataDir + "Aspose.one");
 3
 4var options = new HtmlSaveOptions()
 5{
 6    ExportCss = ResourceExportType.ExportEmbedded,
 7    ExportFonts = ResourceExportType.ExportEmbedded,
 8    ExportImages = ResourceExportType.ExportEmbedded,
 9    FontFaceTypes = FontFaceType.Ttf
10};
11var r = new MemoryStream();
12document.Save(r, options);

Save to Memory Stream with Callbacks

 1// This code creates documentFolder containing document.html, css folder with style.css file, images folder with images and fonts folder with fonts.
 2// style.css file will contains at the end the following string "/* This line is appended to stream manually by user */"
 3var options = new HtmlSaveOptions()
 4{
 5    FontFaceTypes = FontFaceType.Ttf
 6};
 7var savingCallbacks = new UserSavingCallbacks()
 8{
 9    RootFolder = "documentFolder",
10    CssFolder = "css",
11    KeepCssStreamOpened = true,
12    ImagesFolder = "images",
13    FontsFolder = "fonts"
14};
15options.CssSavingCallback = savingCallbacks;
16options.FontSavingCallback = savingCallbacks;
17options.ImageSavingCallback = savingCallbacks;
18
19if (!Directory.Exists(savingCallbacks.RootFolder))
20{
21    Directory.CreateDirectory(savingCallbacks.RootFolder);
22}
23
24string dataDir = RunExamples.GetDataDir_LoadingAndSaving();
25var document = new Aspose.Note.Document(dataDir + "Aspose.one");
26
27using (var stream = File.Create(Path.Combine(savingCallbacks.RootFolder, "document.html")))
28{
29    document.Save(stream, options);
30}
31
32using (var writer = new StreamWriter(savingCallbacks.CssStream))
33{
34    writer.WriteLine();
35    writer.WriteLine("/* This line is appended to stream manually by user */");
36}

The above sample will require the following class for execution.

 1// For complete examples and data files, please go to https://github.com/aspose-note/Aspose.Note-for-.NET
 2class UserSavingCallbacks : ICssSavingCallback, IFontSavingCallback, IImageSavingCallback
 3{
 4    public string RootFolder { get; set; }
 5    public bool KeepCssStreamOpened { get; set; }
 6    public string CssFolder { get; set; }
 7    public Stream CssStream { get; private set; }
 8    public string FontsFolder { get; set; }
 9    public string ImagesFolder { get; set; }
10    public void FontSaving(FontSavingArgs args)
11    {
12        string uri;
13        Stream stream;
14        this.CreateResourceInFolder(this.FontsFolder, args.FileName, out uri, out stream);
15        args.Stream = stream;
16        args.Uri = Path.Combine("..", uri).Replace("\\", "\\\\");
17    }
18    public void CssSaving(CssSavingArgs args)
19    {
20        string uri;
21        Stream stream;
22        this.CreateResourceInFolder(this.CssFolder, args.FileName, out uri, out stream);
23        args.Stream = this.CssStream = stream;
24        args.KeepStreamOpen = this.KeepCssStreamOpened;
25        args.Uri = uri;
26    }
27    public void ImageSaving(ImageSavingArgs args)
28    {
29        string uri;
30        Stream stream;
31        this.CreateResourceInFolder(this.ImagesFolder, args.FileName, out uri, out stream);
32        args.Stream = stream;
33        args.Uri = uri;
34    }
35    private void CreateResourceInFolder(string folder, string filename, out string uri, out Stream stream)
36    {
37        var relativePath = folder;
38        var fullPath = Path.Combine(this.RootFolder, relativePath);
39        if (!Directory.Exists(fullPath))
40        {
41            Directory.CreateDirectory(fullPath);
42        }
43        stream = File.Create(Path.Combine(fullPath, filename));
44        uri = Path.Combine(relativePath, filename);
45    }
46}

Save as HTML File with Resources In Separate File

 1string dataDir = RunExamples.GetDataDir_LoadingAndSaving();
 2var document = new Aspose.Note.Document(dataDir + "Aspose.one");
 3
 4var options = new HtmlSaveOptions()
 5{
 6    ExportCss = ResourceExportType.ExportEmbedded,
 7    ExportFonts = ResourceExportType.ExportEmbedded,
 8    ExportImages = ResourceExportType.ExportEmbedded,
 9    FontFaceTypes = FontFaceType.Ttf
10};
11document.Save(dataDir + "document_out.html", options);

Loading OneNote Documents

The Document class exposed by the API can be used to load Microsoft OneNote .one file format files. The following code example demonstrates the usage of this class for loading a Microsoft OneNote document.

1// Load the document into Aspose.Note.
2Document oneFile = new Document(dataDir + "Aspose.one");

Increase Performance for Consequent Export Operations

Aspose.Note for .NET APIs allow to increase performance for consequent export operations. Users may involve in multiple conversions to several supported formats. In such scenarios, the performance is the highly affecting factor.

Aspose.Note for .NET offers the Document class that represents a OneNote file. The Document class exposes the AutomaticLayoutChangesDetectionEnabled property and DetectLayoutChanges method, their combine use enable users to disable automatic detection of layout changes and handle them manually:

 1// The path to the documents directory.
 2string dataDir = RunExamples.GetDataDir_LoadingAndSaving();
 3
 4// Initialize the new Document
 5Document doc = new Document() { AutomaticLayoutChangesDetectionEnabled = false };
 6// Initialize the new Page
 7Aspose.Note.Page page = new Aspose.Note.Page(doc);
 8// Default style for all text in the document.
 9ParagraphStyle textStyle = new ParagraphStyle { FontColor = Color.Black, FontName = "Arial", FontSize = 10 };
10page.Title = new Title(doc)
11{
12    TitleText = new RichText(doc) { Text = "Title text.", ParagraphStyle = textStyle },
13    TitleDate = new RichText(doc) { Text = new DateTime(2011, 11, 11).ToString("D", CultureInfo.InvariantCulture), ParagraphStyle = textStyle },
14    TitleTime = new RichText(doc) { Text = "12:34", ParagraphStyle = textStyle }
15};
16// Append page node
17doc.AppendChildLast(page);
18// Save OneNote document in different formats, set text font size and detect layout changes manually.
19doc.Save(dataDir + "ConsequentExportOperations_out.html");            
20doc.Save(dataDir + "ConsequentExportOperations_out.pdf");            
21doc.Save(dataDir + "ConsequentExportOperations_out.jpg");            
22textStyle.FontSize = 11;           
23doc.DetectLayoutChanges();            
24doc.Save(dataDir + "ConsequentExportOperations_out.bmp");

Working with Password Protected OneNote Documents

Creating Password Protected OneNote Documents

1// The path to the documents directory.
2string dataDir = RunExamples.GetDataDir_NoteBook();
3
4Document document = new Document();
5document.Save(dataDir + "CreatingPasswordProtectedDoc_out.one", new OneSaveOptions() { DocumentPassword = "pass" });

Loading Password Protected OneNote Documents

Aspose.Note API allows to load a password-protected OneNote documents. The API’s LoadOptions class provides the DocumentPassword property to specify the document password. A password-protected OneNote document can be loaded using the following steps:

  1. Create a new object of LoadOptions class
  2. Specify the password of the document using the DocumentPassword property
  3. Load the document with the defined object of the LoadOptions class

1// The path to the documents directory.
2string dataDir = RunExamples.GetDataDir_LoadingAndSaving();
3
4LoadOptions loadOptions = new LoadOptions
5{
6    DocumentPassword = "password"
7};
8Document doc = new Document(dataDir + "Sample1.one", loadOptions);
9            
 1            string dataDir = RunExamples.GetDataDir_LoadingAndSaving();
 2            string fileName = Path.Combine(dataDir, "Aspose.one");
 3
 4            Document document;
 5            if (!Document.IsEncrypted(fileName, out document))
 6            {
 7                Console.WriteLine("The document is loaded and ready to be processed.");
 8            }
 9            else
10            {
11                Console.WriteLine("The document is encrypted. Provide a password.");
12            }
 1            string dataDir = RunExamples.GetDataDir_LoadingAndSaving();
 2            string fileName = Path.Combine(dataDir, "Aspose.one");
 3
 4            Document document;
 5            if (Document.IsEncrypted(fileName, "VerySecretPassword", out document))
 6            {
 7                if (document != null)
 8                {
 9                    Console.WriteLine("The document is decrypted. It is loaded and ready to be processed.");
10                }
11                else
12                {
13                    Console.WriteLine("The document is encrypted. Invalid password was provided.");
14                }
15            }
16            else
17            {
18                Console.WriteLine("The document is NOT encrypted. It is loaded and ready to be processed.");
19            }

Setting Page Splitting Algorithm

While converting a OneNote document to other formats, images and other contents may get disturbed due to their position in the document. Thus, it is necessary to specify some methods to split the page. The PdfSaveOptions.PageSplittingAlgorithm property provides different options to specify an algorithm of page splitting. These options are:

The default algorithm is KeepSolidObjectsAlgorithm.

Using the KeepSOlidObjectsAlgoirthm

1// For complete examples and data files, please go to https://github.com/kashifiqb/Aspose.Note-for-.NET
2var pdfSaveOptions = new PdfSaveOptions();
3pdfSaveOptions.PageSplittingAlgorithm = new AlwaysSplitObjectsAlgorithm();
4// Or
5pdfSaveOptions.PageSplittingAlgorithm = new KeepPartAndCloneSolidObjectToNextPageAlgorithm();
6// Or
7pdfSaveOptions.PageSplittingAlgorithm = new KeepSolidObjectsAlgorithm();

For KeepPartAndCloneSolidObjectToNextPageAlgorithm and KeepSolidObjectsAlgorithm, the maximum possible height of an object plays a role that can be cloned to the next page. In case an object cannot be cloned, it will be split using AlwaysSplitObjectsAlgorithm. Use the constructor parameter to change this limit as shown below:

1// For complete examples and data files, please go to https://github.com/kashifiqb/Aspose.Note-for-.NET
2float heightLimitOfClonedPart = 500;
3pdfSaveOptions.PageSplittingAlgorithm = new KeepPartAndCloneSolidObjectToNextPageAlgorithm(heightLimitOfClonedPart);
4// Or
5pdfSaveOptions.PageSplittingAlgorithm = new KeepSolidObjectsAlgorithm(heightLimitOfClonedPart);

KeepSolidObjectsAlgorithm

\1. The limit was exceeded and images were split on the pages joint.

1// For complete examples and data files, please go to https://github.com/kashifiqb/Aspose.Note-for-.NET
2pdfSaveOptions.PageSplittingAlgorithm = new KeepSolidObjectsAlgorithm(100);

\2. The limit is sufficient and images were completely cloned to the next page.

1// For complete examples and data files, please go to https://github.com/kashifiqb/Aspose.Note-for-.NET
2pdfSaveOptions.PageSplittingAlgorithm = new KeepSolidObjectsAlgorithm(400);

KeepPartAndCloneSolidObjectToNextPageAlgorithm

\1. The limit was exceeded and images were split into the pages joint.

1// For complete examples and data files, please go to https://github.com/kashifiqb/Aspose.Note-for-.NET
2pdfSaveOptions.PageSplittingAlgorithm = new KeepPartAndCloneSolidObjectToNextPageAlgorithm(100);

\2. The limit is sufficient and images were partially added to the first page and completely cloned to the next page.

1// For complete examples and data files, please go to https://github.com/kashifiqb/Aspose.Note-for-.NET
2pdfSaveOptions.PageSplittingAlgorithm = new KeepPartAndCloneSolidObjectToNextPageAlgorithm(400);

AlwaysSplitObjectsAlgorithm

Images were split into the pages joint.

todo:image_alt_text

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.