Arbeiten mit OneNote-Notizbüchern

Erstellen und Speichern eines Notizbuchs

Die von der API bereitgestellte Klasse Notebook ermöglicht das Erstellen eines Notizbuchs von Grund auf und das Speichern als OneNote-Notizbuch (.onetoc2). Ein OneNote-Notizbuch kann mit dem Standardkonstruktor der Klasse Notebook erstellt werden. Dieser Artikel zeigt, wie man ein Notizbuch erstellt und speichert.

Speichern des Notizbuchs in einen Stream

 1// Load the document into Aspose.Note.
 2String dataDir = Utils.getSharedDataDir(SaveNotebooktoStream.class) + "Notebook/";
 3
 4Notebook notebook = new Notebook();
 5
 6FileOutputStream stream = new FileOutputStream(dataDir + "output.onetoc2");
 7
 8notebook.save(stream);
 9
10if (notebook.getCount() > 0) {
11	Document childDocument0 = (Document) notebook.get_Item(0);
12
13	OutputStream childStream = new FileOutputStream(dataDir + "childStream.one");
14	childDocument0.save(childStream);
15
16	Document childDocument1 = (Document) notebook.get_Item(1);
17
18	childDocument1.save(dataDir + "child.one");
19}

Unterstützung des Schreibens passwortgeschützter Dokumente als Teil eines .onetoc2-Notizbuchs

 1// Load the document into Aspose.Note.
 2String dataDir = Utils.getSharedDataDir(WritingPasswordProtectedDoc.class) + "Notebook/";
 3		
 4Notebook notebook = new Notebook();
 5		
 6NotebookOneSaveOptions saveOptions = new NotebookOneSaveOptions();
 7saveOptions.setDeferredSaving(true);
 8notebook.save(dataDir + "Open Notebook.onetoc2", saveOptions);
 9
10Document childDocument0 = (Document) notebook.get_Item(0);
11childDocument0.save(dataDir + "Not Locked.one");
12
13Document childDocument1 = (Document) notebook.get_Item(1);
14OneSaveOptions documentSaveOptions1 = new OneSaveOptions();
15documentSaveOptions1.setDocumentPassword("pass1");
16childDocument1.save(dataDir + "Locked Pass1.one", documentSaveOptions1);
17
18Document childDocument2 = (Document) notebook.get_Item(2);
19OneSaveOptions documentSaveOptions2 = new OneSaveOptions();
20documentSaveOptions2.setDocumentPassword("pass2");
21childDocument2.save(dataDir + "Locked Pass2.one", documentSaveOptions2);

Lesen einer Notizbuchdatei

Die Notebook-Klasse der API ermöglicht das Lesen einer OneNote-Notizbuchdatei (.onetoc2) von der Festplatte. Die Klasse NotebookLoadOptions bietet zusätzliche Möglichkeiten, die Datei mit benutzerdefinierten Optionen zu laden.

Laden der Notizbuchdatei

 1String dataDir = Utils.getSharedDataDir(LoadingNotebook.class) + "Notebook/";
 2Notebook notebook = new Notebook(dataDir + "Notizbuch �ffnen.onetoc2");
 3      
 4      
 5for (INotebookChildNode notebookChildNode : notebook) {
 6	System.out.println(notebookChildNode.getDisplayName());
 7
 8	if (notebookChildNode instanceof Document) {
 9		// Do something with child document
10	} else if (notebookChildNode instanceof Notebook) {
11		// Do something with child notebook
12	}
13}

Laden der Notizbuchdatei aus einem Stream

Laden der Notizbuchdatei mit LoadOptions

Standardmäßig werden untergeordnete Dokumente eines Notizbuchs „lazy“ geladen, d. h. deren Ladevorgang wird verzögert, bis sie direkt angesprochen werden. Alternativ können alle untergeordneten Dokumente sofort geladen werden, indem man dem Konstruktor NotebookLoadOptions mit dem Flag InstantLoading = true übergibt. Die folgenden Codebeispiele zeigen beide Ansätze.

Laden eines Notizbuchs mit Lazy Loading Option

 1String dataDir = Utils.getSharedDataDir(LoadingNotebookFilewithLoadOptions.class) + "Notebook/";
 2
 3// By default children loading is "lazy".
 4Notebook notebook = new Notebook(dataDir + "test.onetoc2");
 5
 6for (INotebookChildNode notebookChildNode : notebook) {
 7	// Actual loading of the child document happens only here.
 8	if (notebookChildNode instanceof Document) {
 9		// Do something with child document
10	}
11}

Sofortiges Laden eines Notizbuchs

 1// By default children loading is "lazy".
 2// Therefore for instant loading has took place,
 3// it is necessary to set the NotebookLoadOptions.InstantLoading flag.
 4NotebookLoadOptions loadOptions = new NotebookLoadOptions();
 5loadOptions.setInstantLoading(true);
 6String dataDir = Utils.getSharedDataDir(LoadingNotebookInstantly.class) + "Notebook/";
 7Notebook notebook = new Notebook(dataDir + "test.onetoc2", loadOptions);
 8
 9// All child documents are already loaded.
10for (INotebookChildNode notebookChildNode : notebook) {
11	if (notebookChildNode instanceof Document) {
12		// Do something with child document
13	}
14}

In der aktuellen Implementierung ist Lazy Loading nur für OneNote 2010-Unterdokumente/-notizbücher implementiert. OneNote Online-Erste-Ebene-Dokumente/-notizbücher werden immer sofort geladen.

Beispielsweise bei folgender Hierarchiestruktur:

— Root Notebook (2010)
— Abschnitt 1 Ebene 1 (2010)
— Abschnitt 2 Ebene 1 (Online)
— Notizbuch 1 Ebene 1 (2010)
—— Abschnitt 1 Ebene 2 (Online)
— Notizbuch 2 Ebene 1 (Online)
—— Abschnitt 1 Ebene 2 (Online)
—— Abschnitt 2 Ebene 2 (2010)

Werden nur folgende Kinder sofort geladen:
Root Notebook -> Abschnitt 2 Ebene 1,
Root Notebook -> Notizbuch 2 Ebene 1,
Root Notebook -> Notizbuch 2 Ebene 1 -> Abschnitt 1 Ebene 2.

Beachte, dass „Root Notebook -> Notizbuch 1 Ebene 1 -> Abschnitt 1 Ebene 2“ nicht zusammen mit dem „Root Notebook“ geladen wird, sondern erst dann, wenn „Notizbuch 1 Ebene 1“ geladen wird.

Laden passwortgeschützter Dokumente als Teil eines .onetoc2-Notizbuchs

Ein passwortgeschütztes Notizbuch kann mit Hilfe der Klasse LoadOptions und Angabe des Passworts geladen werden.

 1// Load the document into Aspose.Note.
 2String dataDir = Utils.getSharedDataDir(LoadPasswordProtectedDocuments.class) + "Notebook/";
 3		
 4NotebookLoadOptions loadOptions = new NotebookLoadOptions();
 5loadOptions.setDeferredLoading(true);
 6Notebook notebook = new Notebook(dataDir + "Notizbuch �ffnen.onetoc2", loadOptions);
 7
 8notebook.loadChildDocument(dataDir + "Neuer Abschnitt 1.one");
 9
10LoadOptions documentLoadOptions1 = new LoadOptions();
11documentLoadOptions1.setDocumentPassword("pass");
12notebook.loadChildDocument(dataDir + "Locked Pass1.one", documentLoadOptions1);
13
14LoadOptions documentLoadOptions2 = new LoadOptions();
15documentLoadOptions2.setDocumentPassword("pass");
16notebook.loadChildDocument(dataDir + "Locked Pass2.one", documentLoadOptions2);

Hinzufügen eines untergeordneten Knotens zu einem OneNote-Notizbuch

Die Aspose.Note API ermöglicht das Hinzufügen bzw. Anhängen eines untergeordneten Knotens zu einem Notizbuch. Die Methode appendChildLast der Klasse Notebook wird dafür verwendet.

 1String dataDir = Utils.getSharedDataDir(AddChildNode.class) + "Notebook/";
 2// Load a OneNote Notebook
 3Notebook notebook = new Notebook(dataDir + "Notizbuch �ffnen.onetoc2");
 4
 5// Append a new child to the Notebook
 6notebook.appendChild(new Document(dataDir + "Neuer Abschnitt 1.one"));
 7
 8dataDir = dataDir + "AddChildNodetoOneNoteNotebook_out.onetoc2";
 9
10// Save the Notebook
11notebook.save(dataDir);

Entfernen eines untergeordneten Knotens aus einem OneNote-Notizbuch

Die API stellt die Methode removeChild bereit, mit der ein untergeordneter Knoten aus einem OneNote-Notizbuch entfernt werden kann.

 1String dataDir = Utils.getSharedDataDir(RemoveChildNode.class) + "Notebook/";
 2		
 3// Load a OneNote Notebook
 4Notebook notebook = new Notebook(dataDir + "test.onetoc2");
 5
 6// Traverse through its child nodes for searching the desired child item
 7for (INotebookChildNode child : new List<>(notebook))
 8{
 9    if (child.getDisplayName() == "Remove Me")
10    {
11        // Remove the Child Item from the Notebook
12        notebook.removeChild(child);
13    }
14}
15
16dataDir = dataDir + "RemoveChildNodeFromOneNoteNotebook_out.onetoc2";
17
18// Save the Notebook
19notebook.save(dataDir);

Konvertieren eines Notizbuchdokuments in ein Bild

Die Aspose.Note API ermöglicht die Konvertierung von Notizbuchdokumenten (.onetoc2) in Bilder. Die API kann die Konvertierung direkt vornehmen oder es können zusätzliche Optionen mit NotebookImageSaveOptions festgelegt werden.

Exportieren des Notizbuchs als Bild

1String dataDir = Utils.getSharedDataDir(ConvertToImage.class) + "Notebook/";
2		
3// Load a OneNote Notebook
4Notebook notebook = new Notebook(dataDir + "Notizbuch �ffnen.onetoc2");
5
6dataDir = dataDir + "ExportNotebooktoImage_out.png";
7		
8// Save the Notebook
9notebook.save(dataDir);

Exportieren des Notizbuchs mit Optionen

 1String dataDir = Utils.getSharedDataDir(ConvertToImageWithOptions.class) + "Notebook/";
 2// Load a OneNote Notebook
 3Notebook notebook = new Notebook(dataDir + "test.onetoc2");
 4
 5NotebookImageSaveOptions notebookSaveOptions = new NotebookImageSaveOptions(SaveFormat.Png);
 6
 7ImageSaveOptions documentSaveOptions = notebookSaveOptions.getDocumentSaveOptions();
 8
 9documentSaveOptions.setResolution(400);
10
11dataDir = dataDir + "ExportNotebooktoImagewithOptions_out.png";
12
13// Save the Notebook
14notebook.save(dataDir, notebookSaveOptions);

Exportieren als flaches Notizbuch-Bild

 1String dataDir = Utils.getSharedDataDir(ConvertToImageAsFlattenedNotebook.class) + "Notebook/";
 2		
 3Notebook notebook = new Notebook(dataDir + "test.onetoc2");
 4
 5NotebookImageSaveOptions saveOptions = new NotebookImageSaveOptions(SaveFormat.Png);
 6
 7ImageSaveOptions documentSaveOptions = saveOptions.getDocumentSaveOptions();
 8
 9documentSaveOptions.setResolution(400);
10
11saveOptions.setFlatten(true);
12
13notebook.save(dataDir + "ExportImageasFlattenedNotebook_out.png", saveOptions);

Konvertieren des Notizbuchdokuments in PDF

Die Aspose.Note API ermöglicht die Konvertierung von Notizbuchdokumenten (.onetoc2) ins PDF-Format. Die API unterstützt sowohl den direkten Export als auch die Verwendung zusätzlicher Optionen mittels NotebookPdfSaveOptions.

Bitte beachte, dass du keine Werte für die Felder Application und Producer setzen kannst – dort werden “Aspose Ltd.” und “Aspose.Note for Java x.x” angezeigt.

Exportieren des Notizbuchs als PDF

1String dataDir = Utils.getSharedDataDir(ConvertToPDF.class) + "Notebook/";
2		
3// Load a OneNote Notebook
4Notebook notebook = new Notebook(dataDir + "Notizbuch �ffnen.onetoc2");
5
6dataDir = dataDir + "ExportNotebooktoPDF_out.pdf";
7
8// Save the Notebook
9notebook.save(dataDir);

Exportieren des Notizbuchs mit Optionen

 1String dataDir = Utils.getSharedDataDir(ConvertToPDFWithOptions.class) + "Notebook/";
 2		
 3// Load a OneNote Notebook
 4Notebook notebook = new Notebook(dataDir + "Notizbuch �ffnen.onetoc2");
 5
 6NotebookPdfSaveOptions notebookSaveOptions = new NotebookPdfSaveOptions();
 7
 8PdfSaveOptions documentSaveOptions = notebookSaveOptions.getDocumentSaveOptions();
 9
10documentSaveOptions.setPageSplittingAlgorithm (new KeepSolidObjectsAlgorithm());
11
12dataDir = dataDir + "ExportNotebooktoPDFwithOptions_out.pdf";
13
14// Save the Notebook
15notebook.save(dataDir, notebookSaveOptions);

Exportieren als flaches Notizbuch-PDF

Die Aspose.Note API kann verwendet werden, um ein OneNote-Notizbuch als flaches PDF zu exportieren, wobei alle untergeordneten Inhalte in ein einzelnes Verzeichnis exportiert werden.

 1String dataDir = Utils.getSharedDataDir(ExportNotebookToPDFAsFlattened.class) + "Notebook/";
 2
 3// Load a OneNote Notebook
 4Notebook notebook = new Notebook(dataDir + "Notizbuch îffnen.onetoc2");
 5
 6NotebookPdfSaveOptions notebookSaveOptions = new NotebookPdfSaveOptions();
 7
 8notebookSaveOptions.setFlatten(true);
 9
10dataDir = dataDir + "ExportNotebookToPDFAsFlattened_out.pdf";
11
12// Save the Notebook
13notebook.save(dataDir, notebookSaveOptions);

Abrufen von Dokumenten aus einem OneNote-Notizbuch

Das folgende Codebeispiel zeigt, wie man alle Dokumente in einem MS OneNote-Notizbuch einschließlich untergeordneter Notizbücher erhält:

1String dataDir = Utils.getSharedDataDir(RetrieveDocumentsfromOneNoteNotebook.class) + "Notebook/";
2Notebook rootNotebook = new Notebook(dataDir + "test.onetoc2");
3
4List<Document> allDocuments = rootNotebook.getChildNodes(Document.class);
5for (Document document : allDocuments) {
6	System.out.println(document.getDisplayName());
7}

RichText-Knoten aus einem Microsoft OneNote-Notizbuch lesen

Das folgende Codebeispiel zeigt, wie man alle RichText-Knoten aus einem OneNote-Notizbuch ausgibt:

1String dataDir = Utils.getSharedDataDir(LoadingNotebook.class) + "Notebook/";
2Notebook rootNotebook = new Notebook(dataDir + "test.onetoc2");
3
4List<RichText> allRichTextNodes = rootNotebook.getChildNodes(RichText.class);
5for (RichText richTextNode : allRichTextNodes) {
6	System.out.println(richTextNode.getText());
7}
Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.