Arbeiten mit Text
Text aus einem OneNote-Dokument extrahieren
Eine der Aufgaben, die Entwickler häufig durchführen müssen, ist das Extrahieren von Text aus einem OneNote-Dokument. Aspose.Note für .NET ermöglicht es Entwicklern, Text auf verschiedene Arten aus einem OneNote-Dokument zu extrahieren.
Aspose.Note für .NET stellt die Klasse Document bereit, die die OneNote-Datei repräsentiert. Eine einfache LINQ-Abfrage kann verwendet werden, um den Text zu extrahieren.
Gesamten Text aus einem OneNote-Dokument extrahieren
Dieses Beispiel funktioniert wie folgt:
- Erstellen eines Objekts der Klasse Document.
- Aufruf von LINQ-basiertem Code zum Extrahieren des Textes.
- Anzeige des Textes auf dem Ausgabebildschirm.
Der folgende Beispielcode zeigt, wie der gesamte Text aus einem OneNote-Dokument extrahiert wird.
1// The path to the documents directory.
2string dataDir = RunExamples.GetDataDir_Text();
3
4// Load the document into Aspose.Note.
5Document oneFile = new Document(dataDir + "Aspose.one");
6
7// Retrieve text
8string text = string.Join(Environment.NewLine, oneFile.GetChildNodes<RichText>().Select(e => e.Text)) + Environment.NewLine;
9
10// Print text on the output screen
11Console.WriteLine(text);
Text von einer bestimmten Seite eines OneNote-Dokuments extrahieren
Dieses Beispiel funktioniert wie folgt:
- Erstellen eines Objekts der Klasse Document.
- Herausfiltern einer Liste von Seitenelementen.
- Abrufen der Seite per Index.
- Aufruf von LINQ-basiertem Code zum Extrahieren des Textes.
- Anzeige des Textes auf dem Ausgabebildschirm.
Der folgende Beispielcode zeigt, wie Text von einer bestimmten Seite eines OneNote-Dokuments extrahiert wird.
1// The path to the documents directory.
2string dataDir = RunExamples.GetDataDir_Text();
3
4// Load the document into Aspose.Note.
5Document oneFile = new Document(dataDir + "Aspose.one");
6
7// Get list of page nodes
8IList<Node> nodes = oneFile.GetChildNodes<Node>();
9
10if (nodes.Count > 0 && nodes[0].NodeType == NodeType.Page)
11{
12 Page page = (Page)nodes[0];
13 // Retrieve text
14 string text = string.Join(Environment.NewLine, page.GetChildNodes<RichText>().Select(e => e.Text)) + Environment.NewLine;
15 // Print text on the output screen
16 Console.WriteLine(text);
17}
Text auf Seiten eines OneNote-Dokuments ersetzen
Aspose.Note für .NET unterstützt das Suchen und Ersetzen von Text innerhalb eines OneNote-Dokuments. Die Klasse Document repräsentiert dabei die OneNote-Datei.
Text auf allen Seiten ersetzen
Der folgende Beispielcode zeigt, wie Text auf allen Seiten ersetzt wird.
1// The path to the documents directory.
2string dataDir = RunExamples.GetDataDir_Text();
3
4Dictionary<string, string> replacements = new Dictionary<string, string>();
5replacements.Add("Some task here", "New Text Here");
6
7// Load the document into Aspose.Note.
8Document oneFile = new Document(dataDir + "Aspose.one");
9
10// Get all RichText nodes
11IList<RichText> textNodes = oneFile.GetChildNodes<RichText>();
12
13foreach (RichText richText in textNodes)
14{
15 foreach (KeyValuePair<string, string> kvp in replacements)
16 {
17 if (richText != null && richText.Text.Contains(kvp.Key))
18 {
19 // Replace text of a shape
20 richText.Text = richText.Text.Replace(kvp.Key, kvp.Value);
21 }
22 }
23}
24
25dataDir = dataDir + "ReplaceTextOnAllPages_out.pdf";
26
27// Save to any supported file format
28oneFile.Save(dataDir, SaveFormat.Pdf);
Text auf einer bestimmten Seite ersetzen
Der folgende Beispielcode zeigt, wie Text auf einer bestimmten Seite ersetzt wird.
1// The path to the documents directory.
2string dataDir = RunExamples.GetDataDir_Text();
3
4Dictionary<string, string> replacements = new Dictionary<string, string>();
5replacements.Add("voice over", "voice over new text");
6
7// Load the document into Aspose.Note.
8Document oneFile = new Document(dataDir + "Aspose.one");
9
10IList<Page> pageNodes = oneFile.GetChildNodes<Page>();
11
12
13// Get all RichText nodes
14IList<RichText> textNodes = pageNodes[0].GetChildNodes<RichText>();
15
16foreach (RichText richText in textNodes)
17{
18 foreach (KeyValuePair<string, string> kvp in replacements)
19 {
20 if (richText != null && richText.Text.Contains(kvp.Key))
21 {
22 // Replace text of a shape
23 richText.Text = richText.Text.Replace(kvp.Key, kvp.Value);
24 }
25 }
26}
27dataDir = dataDir + "ReplaceTextOnParticularPage_out.pdf";
28// Save to any supported file format
29oneFile.Save(dataDir, SaveFormat.Pdf);
Eigenschaften von Aufzählungs- oder Nummerierungslisten abrufen
Aspose.Note für .NET bietet umfassende Unterstützung für Microsoft OneNote-Listen. Entwickler können eine Vielzahl von Listeneigenschaften wie Schriftart, Nummerierungsformat usw. abrufen.
Eigenschaften abrufen
Aspose.Note für .NET bietet die Klasse
OutlineElement, die ein Gliederungselement in einem OneNote-Dokument darstellt. Die Eigenschaft NumberList
ermöglicht das Abrufen von Eigenschaften nummerierter oder aufzählender Listen.
1string dataDir = RunExamples.GetDataDir_Text();
2
3// Load the document into Aspose.Note.
4Document oneFile = new Document(dataDir + "ApplyNumberingOnText.one");
5
6// Retrieve a collection nodes of the outline element
7IList<OutlineElement> nodes = oneFile.GetChildNodes<OutlineElement>();
8
9// Iterate through each node
10foreach (OutlineElement node in nodes)
11{
12 if (node.NumberList != null)
13 {
14 NumberList list = node.NumberList;
15 // Retrieve font name
16 Console.WriteLine("Font Name: " + list.Font);
17 // Retrieve font length
18 Console.WriteLine("Font Length: " + list.Font.Length);
19 // Retrieve font size
20 Console.WriteLine("Font Size: " + list.FontSize);
21 // Retrieve font color
22 Console.WriteLine("Font Color: " + list.FontColor);
23 // Retrieve format
24 Console.WriteLine("Font format: " + list.Format);
25 // Check bold
26 Console.WriteLine("Is bold: " + list.IsBold);
27 // Check italic
28 Console.WriteLine("Is italic: " + list.IsItalic);
29 Console.WriteLine();
30 }
31}
Aufzählung auf Text anwenden
Texte in Aufzählungen zu strukturieren, ist eine gängige Praxis. Aspose.Note für .NET ermöglicht es Entwicklern, Textelemente als Aufzählungen zu formatieren.
Dazu wird ein
OutlineElement erstellt, dessen NumberList
-Eigenschaft die Aufzählung definiert.
Schritte zur Anwendung von Aufzählungen:
- Erstellen einer Instanz der Klasse Document.
- Initialisieren von drei Page-Objekten und Festlegen ihrer Ebenen.
- Initialisieren der Klassen Outline, OutlineElement und RichText.
- Anwenden der
NumberList
-Eigenschaft vonOutlineElement
. - Festlegen der Textformatierung über die Klasse ParagraphStyle.
- Speichern des Dokuments mittels der Methode
Save
.
Beispiel:
1string dataDir = RunExamples.GetDataDir_Text();
2
3// Create an object of the Document class
4Aspose.Note.Document doc = new Aspose.Note.Document();
5// Initialize Page class object
6Aspose.Note.Page page = new Aspose.Note.Page(doc);
7// Initialize Outline class object
8Outline outline = new Outline(doc);
9// Initialize TextStyle class object and set formatting properties
10ParagraphStyle defaultStyle = new ParagraphStyle { FontColor = Color.Black, FontName = "Arial", FontSize = 10 };
11
12// Initialize OutlineElement class objects and apply bullets
13OutlineElement outlineElem1 = new OutlineElement(doc) { NumberList = new NumberList("*", "Arial", 10) };
14// Initialize RichText class object and apply text style
15RichText text1 = new RichText(doc) { Text = "First", ParagraphStyle = defaultStyle };
16outlineElem1.AppendChildLast(text1);
17
18OutlineElement outlineElem2 = new OutlineElement(doc) { NumberList = new NumberList("*", "Arial", 10) };
19RichText text2 = new RichText(doc) { Text = "Second", ParagraphStyle = defaultStyle };
20outlineElem2.AppendChildLast(text2);
21
22OutlineElement outlineElem3 = new OutlineElement(doc) { NumberList = new NumberList("*", "Arial", 10) };
23RichText text3 = new RichText(doc) { Text = "Third", ParagraphStyle = defaultStyle };
24outlineElem3.AppendChildLast(text3);
25
26// Add outline elements
27outline.AppendChildLast(outlineElem1);
28outline.AppendChildLast(outlineElem2);
29outline.AppendChildLast(outlineElem3);
30
31// Add Outline node
32page.AppendChildLast(outline);
33// Add Page node
34doc.AppendChildLast(page);
35
36dataDir = dataDir + "ApplyBulletsOnText_out.one";
37// Save OneNote document
38doc.Save(dataDir);
Nummerierung auf Text anwenden
Analog zur Aufzählung können auch Nummerierungen angewendet werden.
Verfahren identisch wie oben beschrieben.
Beispiel:
1string dataDir = RunExamples.GetDataDir_Text();
2
3// Create an object of the Document class
4Document doc = new Document();
5
6// Initialize Page class object
7Aspose.Note.Page page = new Aspose.Note.Page(doc);
8
9// Initialize Outline class object
10Outline outline = new Outline(doc);
11
12// Initialize TextStyle class object and set formatting properties
13ParagraphStyle defaultStyle = new ParagraphStyle { FontColor = Color.Black, FontName = "Arial", FontSize = 10 };
14
15// Initialize OutlineElement class objects and apply numbering
16// Numbers in the same outline are automatically incremented.
17OutlineElement outlineElem1 = new OutlineElement(doc) { NumberList = new NumberList("{0})", NumberFormat.DecimalNumbers, "Arial", 10) };
18RichText text1 = new RichText(doc) { Text = "First", ParagraphStyle = defaultStyle };
19outlineElem1.AppendChildLast(text1);
20
21OutlineElement outlineElem2 = new OutlineElement(doc) { NumberList = new NumberList("{0})", NumberFormat.DecimalNumbers, "Arial", 10) };
22RichText text2 = new RichText(doc) { Text = "Second", ParagraphStyle = defaultStyle };
23outlineElem2.AppendChildLast(text2);
24
25OutlineElement outlineElem3 = new OutlineElement(doc) { NumberList = new NumberList("{0})", NumberFormat.DecimalNumbers, "Arial", 10) };
26RichText text3 = new RichText(doc) { Text = "Third", ParagraphStyle = defaultStyle };
27outlineElem3.AppendChildLast(text3);
28
29// Add outline elements
30outline.AppendChildLast(outlineElem1);
31outline.AppendChildLast(outlineElem2);
32outline.AppendChildLast(outlineElem3);
33
34// Add Outline node
35page.AppendChildLast(outline);
36// Add Page node
37doc.AppendChildLast(page);
38
39dataDir = dataDir + "ApplyNumberingOnText_out.one";
40// Save OneNote document
41doc.Save(dataDir);
Chinesische Nummerierung in OneNote-Dokument einfügen
Aspose.Note für .NET unterstützt auch chinesische Nummerierungsformate.
Verfahren identisch, nur mit chinesischem Zählformat.
Beispiel:
1string dataDir = RunExamples.GetDataDir_Text();
2
3// Initialize OneNote document
4Aspose.Note.Document doc = new Aspose.Note.Document();
5// Initialize OneNote page
6Aspose.Note.Page page = new Aspose.Note.Page(doc);
7Outline outline = new Outline(doc);
8// Apply text style settings
9ParagraphStyle defaultStyle = new ParagraphStyle { FontColor = Color.Black, FontName = "Arial", FontSize = 10 };
10
11// Numbers in the same outline are automatically incremented.
12OutlineElement outlineElem1 = new OutlineElement(doc) { NumberList = new NumberList("{0})", NumberFormat.ChineseCounting, "Arial", 10) };
13RichText text1 = new RichText(doc) { Text = "First", ParagraphStyle = defaultStyle };
14outlineElem1.AppendChildLast(text1);
15//------------------------
16OutlineElement outlineElem2 = new OutlineElement(doc) { NumberList = new NumberList("{0})", NumberFormat.ChineseCounting, "Arial", 10) };
17RichText text2 = new RichText(doc) { Text = "Second", ParagraphStyle = defaultStyle };
18outlineElem2.AppendChildLast(text2);
19//------------------------
20OutlineElement outlineElem3 = new OutlineElement(doc) { NumberList = new NumberList("{0})", NumberFormat.ChineseCounting, "Arial", 10) };
21RichText text3 = new RichText(doc) { Text = "Third", ParagraphStyle = defaultStyle };
22outlineElem3.AppendChildLast(text3);
23//------------------------
24outline.AppendChildLast(outlineElem1);
25outline.AppendChildLast(outlineElem2);
26outline.AppendChildLast(outlineElem3);
27page.AppendChildLast(outline);
28doc.AppendChildLast(page);
29
30dataDir = dataDir + "InsertChineseNumberList_out.one";
31// Save OneNote document
32doc.Save(dataDir);
Seitentitel im Stil von MS OneNote erstellen
Aspose.Note für .NET erlaubt das Erstellen eines Seitentitels im typischen OneNote-Stil über die Eigenschaft DefaultMsOneNoteTitleTextStyle
der Klasse TextStyle
.
Seitentitel im Microsoft OneNote-Stil festlegen
1string dataDir = RunExamples.GetDataDir_Text();
2string outputPath = dataDir + "CreateTitleMsStyle_out.one";
3
4var doc = new Document();
5var page = new Page(doc);
6
7page.Title = new Title(doc)
8{
9 TitleText = new RichText(doc)
10 {
11 Text = "Title text.",
12 ParagraphStyle = ParagraphStyle.Default
13 },
14 TitleDate = new RichText(doc)
15 {
16 Text = new DateTime(2011, 11, 11).ToString("D", CultureInfo.InvariantCulture),
17 ParagraphStyle = ParagraphStyle.Default
18 },
19 TitleTime = new RichText(doc)
20 {
21 Text = "12:34",
22 ParagraphStyle = ParagraphStyle.Default
23 }
24};
25
26doc.AppendChildLast(page);
27
28doc.Save(outputPath);