Erstellen eines OneNote-Notizbuchs

Erstellen und Speichern eines Notizbuchs

Die von der API bereitgestellte Klasse Notebook ermöglicht es, ein Notizbuch von Grund auf neu zu erstellen und es als OneNote-Notizbuch (.onetoc2) zu speichern. Ein OneNote-Notizbuch kann mit dem Standardkonstruktor der Klasse Notebook erstellt werden. Dieser Artikel zeigt, wie ein Notizbuchdokument erstellt und gespeichert wird.

1// The path to the documents directory.
2string dataDir = RunExamples.GetDataDir_NoteBook();
3            
4var notebook = new Notebook();
5
6dataDir = dataDir + "test_out.onetoc2";
7
8// Save the Notebook
9notebook.Save(dataDir);

Notizbuch in einen Stream speichern

 1// The path to the documents directory.
 2string dataDir = RunExamples.GetDataDir_NoteBook();
 3var notebook = new Notebook();
 4
 5MemoryStream stream = new MemoryStream();
 6notebook.Save(stream);
 7if (notebook.Count > 0)
 8{
 9    var childDocument0 = notebook[0] as Document;
10
11    MemoryStream childStream = new MemoryStream();
12    childDocument0.Save(childStream);
13
14    var childDocument1 = notebook[1] as Document;
15    childDocument0.Save(dataDir + "SaveNotebookToStream_out.one");
16}

Unterstützung für das Schreiben kennwortgeschützter Dokumente als Teil des .onetoc2-Notizbuchs

 1// The path to the documents directory.
 2string dataDir = RunExamples.GetDataDir_NoteBook();
 3
 4var notebook = new Notebook(dataDir + "notebook2.onetoc2", new NotebookLoadOptions() { DeferredLoading = false });
 5
 6notebook.Save(dataDir + "notebook_out.onetoc2", new NotebookOneSaveOptions() { DeferredSaving = true });
 7
 8if (notebook.Count > 0)
 9{
10    var childDocument0 = notebook[0] as Document;
11
12    childDocument0.Save(dataDir + "Not Locked_out.one");
13
14    var childDocument1 = notebook[1] as Document;
15
16    childDocument1.Save(dataDir + "Locked Pass1_out.one", new OneSaveOptions() { DocumentPassword = "pass" });
17
18    var childDocument2 = notebook[2] as Document;
19
20    childDocument2.Save(dataDir + "Locked Pass2_out.one", new OneSaveOptions() { DocumentPassword = "pass2" });
21}

Notizbuchdatei lesen

Das Dateiformat .onetoc2 stellt das gespeicherte Inhaltsverzeichnis für das OneNote-Dokument dar. Die Klasse Notebook kann verwendet werden, um diesen Microsoft .one-Dateityp zu lesen und weiter zu analysieren. Das folgende Codebeispiel zeigt die Verwendung dieser Klasse.

Notizbuchdatei laden

 1// The path to the documents directory.
 2string dataDir = RunExamples.GetDataDir_LoadingAndSaving();
 3string fileName = "Open Notebook.onetoc2";
 4if (fileName != "")
 5{
 6    try
 7    {
 8        var notebook = new Notebook(dataDir + fileName);
 9        foreach (var notebookChildNode in notebook)
10        {
11            Console.WriteLine(notebookChildNode.DisplayName);
12            if (notebookChildNode is Document)
13            {
14                // Do something with child document
15            }
16            else if (notebookChildNode is Notebook)
17            {
18                // Do something with child notebook
19            }
20        }
21    }
22    catch (Exception ex)
23    {
24        Console.WriteLine(ex.Message);
25    }
26}
27else
28{
29    Console.WriteLine("\nPlease enter valid file name.");
30}

Notizbuch aus einem Stream laden

1// The path to the documents directory.
2string dataDir = RunExamples.GetDataDir_NoteBook();
3FileStream stream = new FileStream(dataDir + "Notizbuch öffnen.onetoc2", FileMode.Open);
4var notebook = new Notebook(stream);
5
6FileStream childStream = new FileStream(dataDir + "Aspose.one", FileMode.Open);
7notebook.LoadChildDocument(childStream);
8           
9notebook.LoadChildDocument(dataDir + "Sample1.one");

Notizbuch mit Ladeoptionen laden

Standardmäßig werden untergeordnete Dokumente eines Notizbuchs “lazy” geladen, d. h. ihr Laden wird verschoben, bis direkt auf ein bestimmtes Kind zugegriffen wird. Alternativ können alle untergeordneten Elemente auf einmal geladen werden, indem dem Konstruktor von Notebook ein NotebookLoadOptions mit gesetztem InstantLoading-Flag übergeben wird. Die folgenden Codebeispiele demonstrieren beide Ansätze.

Notizbuch mit Lazy-Loading-Option laden

 1string inputFile = "Notizbuch öffnen.onetoc2";
 2string dataDir = RunExamples.GetDataDir_NoteBook();
 3
 4// By default children loading is "lazy".
 5Notebook notebook = new Notebook(dataDir + inputFile);
 6
 7foreach (INotebookChildNode notebookChildNode in notebook) {
 8 // Actual loading of the child document happens only here.
 9 if (notebookChildNode is Document) {
10  // Do something with child document
11 }
12}

Notizbuch sofort laden

 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.InstantLoading = true;
 6String inputFile = "Notizbuch öffnen.onetoc2";
 7String dataDir = RunExamples.GetDataDir_NoteBook();
 8Notebook notebook = new Notebook(dataDir + inputFile, loadOptions);
 9
10// All child documents are already loaded.
11foreach (INotebookChildNode notebookChildNode in notebook) {
12 if (notebookChildNode is Document) {
13  // Do something with child document
14 }
15}

Kennwortgeschützte Dokumente als Teil eines .onetoc2-Notizbuchs laden

Ein Notizbuch mit kennwortgeschützten .one-Dokumenten kann mit Hilfe der LoadOptions-Klasse der API geladen werden, wobei das Kennwort angegeben wird.

1// The path to the documents directory.
2string dataDir = RunExamples.GetDataDir_NoteBook();
3var notebook = new Notebook(dataDir + "test.onetoc2", new NotebookLoadOptions() { DeferredLoading = true });
4
5notebook.LoadChildDocument(dataDir + "Aspose.one");  
6notebook.LoadChildDocument(dataDir + "Locked Pass1.one", new LoadOptions() { DocumentPassword = "pass" });
7notebook.LoadChildDocument(dataDir + "Locked Pass2.one", new LoadOptions() { DocumentPassword = "pass2" });

Ein untergeordnetes Element zum OneNote-Notizbuch hinzufügen

Die Aspose.Note-API ermöglicht das Hinzufügen oder Anhängen eines untergeordneten Elements am Anfang oder Ende eines Notizbuchs. Die von der Klasse Notebook bereitgestellten Methoden AppendChildLast oder AppendChildFirst ermöglichen dies.

 1// The path to the documents directory.
 2string dataDir = RunExamples.GetDataDir_NoteBook();
 3
 4// Load a OneNote Notebook
 5var notebook = new Notebook(dataDir + "Notizbuch �ffnen.onetoc2");
 6
 7// Append a new child to the Notebook
 8notebook.AppendChild(new Document(dataDir + "Neuer Abschnitt 1.one"));
 9
10dataDir = dataDir + "AddChildNode_out.onetoc2";
11
12// Save the Notebook
13notebook.Save(dataDir);

Ein untergeordnetes Element aus dem OneNote-Notizbuch entfernen

Die Aspose.Note-API bietet die Möglichkeit, ein untergeordnetes Element aus einem Notizbuch zu entfernen. Die von der Klasse Notebook bereitgestellte Methode RemoveChild ermöglicht dies.

 1// The path to the documents directory.
 2string dataDir = RunExamples.GetDataDir_NoteBook();
 3
 4// Load a OneNote Notebook
 5var notebook = new Notebook(dataDir + "test.onetoc2");
 6
 7// Traverse through its child nodes for searching the desired child item
 8foreach (var child in new List<INotebookChildNode>(notebook))
 9{
10    if (child.DisplayName == "Remove Me")
11    {
12        // Remove the Child Item from the Notebook
13        notebook.RemoveChild(child);
14    }
15}
16
17dataDir = dataDir + "RemoveChildNode_out.onetoc2";
18
19// Save the Notebook
20notebook.Save(dataDir);

Notizbuchdokument in ein Bild konvertieren

Die Aspose.Note-API ermöglicht die Konvertierung von Notizbuchdokumenten (.onetoc2) in Bilder. Die API kann das Dokument direkt exportieren oder zusätzliche Speicheroptionen über die Klasse NotebookImageSaveOptions angeben.

Notizbuch in ein Bild exportieren

 1// The path to the documents directory.
 2string dataDir = RunExamples.GetDataDir_NoteBook();
 3
 4// Load a OneNote Notebook
 5var notebook = new Notebook(dataDir + "Notizbuch �ffnen.onetoc2");
 6
 7dataDir = dataDir + "ConvertToImage_out.png";
 8
 9// Save the Notebook
10notebook.Save(dataDir);

Notizbuch mit Optionen in ein Bild exportieren

 1// The path to the documents directory.
 2string dataDir = RunExamples.GetDataDir_NoteBook();
 3
 4// Load a OneNote Notebook
 5var notebook = new Notebook(dataDir + "Notizbuch �ffnen.onetoc2");
 6
 7var notebookSaveOptions = new NotebookImageSaveOptions(SaveFormat.Png);
 8
 9var documentSaveOptions = notebookSaveOptions.DocumentSaveOptions;
10
11documentSaveOptions.Resolution = 400;
12
13dataDir = dataDir + "ConvertToImageWithOptions_out.png";
14
15// Save the Notebook
16notebook.Save(dataDir, notebookSaveOptions);

Als flaches Notizbuch in ein Bild exportieren

 1// The path to the documents directory.
 2string dataDir = RunExamples.GetDataDir_NoteBook();
 3
 4// Load a OneNote Notebook
 5var notebook = new Notebook(dataDir + "Notizbuch öffnen.onetoc2");
 6
 7var notebookSaveOptions = new NotebookImageSaveOptions(SaveFormat.Png);
 8
 9var documentSaveOptions = notebookSaveOptions.DocumentSaveOptions;
10
11documentSaveOptions.Resolution = 400;
12notebookSaveOptions.Flatten = true;
13
14dataDir = dataDir + "ConvertToImageAsFlattenedNotebook_out.png";
15
16// Save the Notebook
17notebook.Save(dataDir, notebookSaveOptions);

Notizbuchdokument in PDF konvertieren

Die Aspose.Note-API bietet die Möglichkeit, Notizbuchdokumente (.onetoc2) in das PDF-Format zu konvertieren. Die API kann das Dokument direkt exportieren oder zusätzliche Speicheroptionen über die Klasse NotebookPdfSaveOptions angeben. Dieser Artikel zeigt alle Ansätze zur Konvertierung eines Notizbuchs in PDF.

Bitte beachten Sie, dass Sie keine Werte für die Felder Application und Producer festlegen können, da “Aspose Ltd.” und “Aspose.Note for .NET x.x” in diesen Feldern angezeigt werden.

Notizbuch in PDF exportieren

 1// The path to the documents directory.
 2string dataDir = RunExamples.GetDataDir_NoteBook();
 3
 4// Load a OneNote Notebook
 5var notebook = new Notebook(dataDir + "Notizbuch �ffnen.onetoc2");
 6
 7dataDir = dataDir + "ConvertToPDF_out.pdf";
 8
 9// Save the Notebook
10notebook.Save(dataDir);

Notizbuch mit Optionen in PDF exportieren

 1// The path to the documents directory.
 2string dataDir = RunExamples.GetDataDir_NoteBook();
 3
 4// Load a OneNote Notebook
 5var notebook = new Notebook(dataDir + "Notizbuch �ffnen.onetoc2");
 6
 7var notebookSaveOptions = new NotebookPdfSaveOptions();
 8
 9var documentSaveOptions = notebookSaveOptions.DocumentSaveOptions;
10
11documentSaveOptions.PageSplittingAlgorithm = new KeepSolidObjectsAlgorithm();
12
13dataDir = dataDir + "ConvertToPDF_out.pdf";
14
15// Save the Notebook
16notebook.Save(dataDir, notebookSaveOptions);

Als flaches Notizbuch in PDF exportieren

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

 1// The path to the documents directory.
 2string dataDir = RunExamples.GetDataDir_NoteBook();
 3
 4// Load a OneNote Notebook
 5var notebook = new Notebook(dataDir + "Notizbuch �ffnen.onetoc2");
 6
 7dataDir = dataDir + "ConvertToPDFAsFlattened_out.pdf";
 8// Save the Notebook
 9notebook.Save(
10    dataDir,
11    new NotebookPdfSaveOptions
12    {
13        Flatten = true
14    });         

Dokumente aus einem OneNote-Notizbuch abrufen

Der folgende Codeausschnitt zeigt, wie man alle Dokumente abruft, die im gesamten Microsoft OneNote-Notizbuch einschließlich untergeordneter Notizbücher vorhanden sind.

1string inputFile = "notebook.onetoc2";
2string dataDir = RunExamples.GetDataDir_NoteBook();
3Notebook rootNotebook = new Notebook(dataDir + inputFile);
4
5IList<Document> allDocuments = rootNotebook.GetChildNodes<Document>();
6foreach (Document document in allDocuments) {
7 Console.WriteLine(document.DisplayName);
8}

RichText-Knoten aus einem Microsoft OneNote-Notizbuch lesen

Der folgende Codeausschnitt zeigt, wie man alle RichText-Knoten aus einem OneNote-Notizbuch ausgibt:

1string inputFile = "notebook.onetoc2";
2string dataDir = RunExamples.GetDataDir_NoteBook();
3
4Notebook rootNotebook = new Notebook(dataDir + inputFile);
5
6IList<RichText> allRichTextNodes = rootNotebook.GetChildNodes<RichText>();
7foreach (RichText richTextNode in allRichTextNodes) {
8 Console.WriteLine(richTextNode.Text);
9}
Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.