Load OneNote Document or File in Java API

Loading OneNote Documents

The Document class exposed by the OneNote Java API or library can be used to load Microsoft OneNote .One file format files. The following sample code in Java programming language shows 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");
3        
4System.out.println(oneFile.getFileFormat());

Increase Performance for Consequent Export Operations

Aspose.Note API allows increasing 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 Java offers the Document class that represents a OneNote file. The Document class exposes the AutomaticLayoutChangesDetectionEnabled property and DetectLayoutChanges method, their combined use enable users to disable automatic detection of layout changes and handle them manually:

 1String dataDir = Utils.getSharedDataDir(OptimizePerformanceForConsequentExportOperations.class) + "load/";
 2// initialize the new Document
 3Document doc = new Document();
 4doc.setAutomaticLayoutChangesDetectionEnabled(false);
 5// initialize the new Page
 6Page page = new Page(doc);
 7// default style for all text in the document.
 8ParagraphStyle textStyle = new ParagraphStyle();
 9
10textStyle.setFontColor(Color.BLACK);
11textStyle.setFontName("Arial");
12textStyle.setFontSize(10);
13
14// title text
15RichText titleText = new RichText(doc);
16titleText.setText("Title text.");
17titleText.setParagraphStyle(textStyle);
18
19// title date
20RichText titleDate = new RichText(doc);
21titleDate.setText("2011,11,11");
22titleDate.setParagraphStyle(textStyle);
23
24// title time
25RichText titleTime = new RichText(doc);
26titleTime.setText("12:34");
27titleTime.setParagraphStyle(textStyle);
28
29Title title = new Title(doc);
30title.setTitleText(titleText);
31title.setTitleDate(titleDate);
32title.setTitleTime(titleTime);
33page.setTitle(title);
34
35// append page node
36doc.appendChildLast(page);
37// save OneNote document in the HTML format
38try {
39	doc.save(dataDir + "OptimizePerformanceForConsequentExportOperations_out.html");
40} catch (Exception e) {
41	// TODO Auto-generated catch block
42	// e.printStackTrace();
43	System.out.println("Saving as Html: " + e.getMessage());
44}
45// save OneNote document in the PDF format
46try {
47	doc.save(dataDir + "OptimizePerformanceForConsequentExportOperations_out.pdf");
48} catch (Exception e) {
49	// TODO Auto-generated catch block
50	// e.printStackTrace();
51	System.out.println("Saving as PDF: " + e.getMessage());
52}
53// save OneNote document in the JPG format
54try {
55	doc.save(dataDir + "OptimizePerformanceForConsequentExportOperations_out.jpg");
56} catch (Exception e) {
57	// TODO Auto-generated catch block
58	// e.printStackTrace();
59	System.out.println("Saving as JPG: " + e.getMessage());
60}
61// set text font size
62textStyle.setFontSize(11);
63// detect layout changes manually
64doc.detectLayoutChanges();
65// save OneNote document in the BMP format
66try {
67	doc.save(dataDir + "OptimizePerformanceForConsequentExportOperations_out.bmp");
68} catch (Exception e) {
69	// TODO Auto-generated catch block
70	// e.printStackTrace();
71	System.out.println("Saving as bmp: " + e.getMessage());
72}

Working with Password Protected OneNote Documents

Aspose.Note API allows to load password-protected OneNote documents. The API’s LoadOptions class provides the setDocumentPassword method to specify the document password.

Creating Password Protected OneNote Documents

Aspose.Note API allows to set password for the document while saving OneNote documents. The setDocumentPassword method exposed by OneSaveOptions class provides the capability to password protect a document.

 1// Load the document into Aspose.Note.
 2String dataDir = Utils.getSharedDataDir(CreatePasswordProtectedOneNoteDocuments.class) + "load/";
 3
 4Document document = new Document(dataDir + "Sample1.one");
 5
 6OneSaveOptions saveOptions = new OneSaveOptions();
 7
 8saveOptions.setDocumentPassword("pass");
 9
10document.save(dataDir + "CreatePasswordProtected_out.one", saveOptions);

Loading Password Protected OneNote Documents

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 setDocumentPassword method
  3. Load the document with the defined object of the LoadOptions class

1String dataDir = Utils.getSharedDataDir(LoadPasswordProtectedOneNoteDoc.class) + "load/";
2
3LoadOptions loadOptions = new LoadOptions();
4
5loadOptions.setDocumentPassword("password");
6
7Document doc = new Document(dataDir + "Sample1.one", loadOptions);
8		
9System.out.println(doc.getFileFormat());
 1        String dataDir = Paths.get(Utils.getSharedDataDir(LoadPasswordProtectedOneNoteDoc.class), "load").toString();
 2
 3        LoadOptions loadOptions = new LoadOptions();
 4        loadOptions.setDocumentPassword("password");
 5
 6        FileInputStream stream = new FileInputStream(Paths.get(dataDir, "Sample1.one").toString());
 7
 8        try {
 9            Document ref[] = { null };
10            if (!Document.isEncrypted(stream, loadOptions, ref))
11            {
12                System.out.println("The document is loaded and ready to be processed.");
13            }
14            else
15            {
16                System.out.println("The document is encrypted. Provide a password.");
17            }
18        }
19        finally {
20            stream.close();
21        }
 1        String dataDir = Paths.get(Utils.getSharedDataDir(LoadPasswordProtectedOneNoteDoc.class), "load").toString();
 2
 3        Document ref[] = { null };
 4        if (!Document.isEncrypted(Paths.get(dataDir, "Sample1.one").toString(), "VerySecretPassword", ref))
 5        {
 6            if (ref[0] != null)
 7            {
 8                System.out.println("The document is decrypted. It is loaded and ready to be processed.");
 9            }
10            else
11            {
12                System.out.println("The document is encrypted. Invalid password was provided.");
13            }
14        }
15        else
16        {
17            System.out.println("The document is NOT encrypted. It is loaded and ready to be processed.");
18        }

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 a 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 Splitting Algorithm Method

 1// For complete examples and data files, please go to https://github.com/aspose-note/Aspose.Note-for-Java
 2
 3String inputFile = "Sample1.one";
 4Path inputPath = Utils.getPath(UsingSplittingAlgorithmMethod.class, inputFile);
 5
 6String outputFile = "output.Jpeg";
 7Path outputPath = Utils.getPath(UsingSplittingAlgorithmMethod.class, outputFile);
 8
 9Document doc = new Document(inputPath.toString());
10
11PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
12pdfSaveOptions.setPageSplittingAlgorithm(new AlwaysSplitObjectsAlgorithm());
13// or
14pdfSaveOptions.setPageSplittingAlgorithm(new KeepPartAndCloneSolidObjectToNextPageAlgorithm());
15// or
16pdfSaveOptions.setPageSplittingAlgorithm(new KeepSolidObjectsAlgorithm());
17try {
18	doc.save(outputPath.toString(), pdfSaveOptions);
19} catch (Exception ex) {
20	System.out.println("Exception: " + ex.getMessage());
21}

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/aspose-note/Aspose.Note-for-Java
2float heightLimitOfClonedPart = 500;
3PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
4pdfSaveOptions
5		.setPageSplittingAlgorithm(new KeepPartAndCloneSolidObjectToNextPageAlgorithm(heightLimitOfClonedPart));
6// or
7pdfSaveOptions.setPageSplittingAlgorithm(new KeepSolidObjectsAlgorithm(heightLimitOfClonedPart));

todo:image_alt_text

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/aspose-note/Aspose.Note-for-Java
2pdfSaveOptions.setPageSplittingAlgorithm(new KeepSolidObjectsAlgorithm(100));

todo:image_alt_text

\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/aspose-note/Aspose.Note-for-Java
2pdfSaveOptions.setPageSplittingAlgorithm(new KeepSolidObjectsAlgorithm(400));

todo:image_alt_text

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/aspose-note/Aspose.Note-for-Java
2pdfSaveOptions.setPageSplittingAlgorithm(new KeepPartAndCloneSolidObjectToNextPageAlgorithm(100));

todo:image_alt_text

\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/aspose-note/Aspose.Note-for-Java
2pdfSaveOptions.setPageSplittingAlgorithm(new KeepPartAndCloneSolidObjectToNextPageAlgorithm(400));

todo:image_alt_text

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.