在 C# 中处理 OneNote 文档:轻松读取、转换和操作

您是否厌倦了在 C# 应用程序中处理 Microsoft OneNote 文件的复杂性?无论您是处理大量笔记,需要将它们转换为不同格式,还是提取关键信息,处理 OneNote 文档可能都是一项艰巨的任务。在本文中,我们将深入探讨 OneNote 文档处理 API 的强大功能,并演示它如何提高您在 C# 中处理 OneNote 文档的生产力。

本文涵盖以下主题:

介绍 OneNote 文档处理 C# API

我们很高兴地宣布我们的 OneNote 文档处理 API - Aspose.Note for .NET。它旨在简化 OneNote 文件的处理。与其他可能提供有限功能或需要大量编码的工具不同,Aspose.Note 提供了一种全面的解决方案,可以轻松地读取、转换和操作 OneNote 文档。它允许开发人员在 C#、ASP.NET MVC、Web 服务或任何基于 .NET 的应用程序中处理 ONE 文件。

OneNote C# API 的关键功能和优点

Aspose.Note for .NET 无缝集成了 OneNote 文件处理到您的应用程序中,节省时间并减少复杂性。想象一下,能够读取 OneNote 文件,将其转换为 PDF 或 HTML 等各种格式,提取文本和图像,甚至以编程方式操作内容——所有这些只需几行代码。Aspose.Note for .NET 使这一切成为可能,提供了一套强大的功能来满足开发人员的需求。

转变您的工作流程

无论您是在开发笔记应用程序、档案系统,还是任何涉及 OneNote 文件的解决方案,Aspose.Note for .NET 都可以满足您的需求。在本文的接下来部分中,我们将探讨 Aspose.Note for .NET 的关键功能,并演示它如何转变您的 OneNote 文档处理任务。从读取和转换到提取和操作,您将发现这个 API 如何提升您的生产力并简化您的工作流程。

使用 C# 转换 OneNote 文档

在 C# 中将 OneNote 文档转换为 PNG、BMP、JPEG、GIF

Aspose.Note API 允许开发人员将一个笔记本页面保存为图像文件,因此制作缩略图非常简单。以下是将 OneNote 保存为图像的 C# 代码。

// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_LoadingAndSaving();
// Load the document into Aspose.Note.
Document oneFile = new Document(dataDir + "Aspose.one");
// Initialize ImageSaveOptions object
ImageSaveOptions opts = new ImageSaveOptions(SaveFormat.Png);
// Set page index
opts.PageIndex = 1;
dataDir = dataDir + "ConvertSpecificPageToImage_out.png";
// Save the document as PNG.
oneFile.Save(dataDir, opts);

要深入了解文档转换,请查看我们的指南 以编程方式将 OneNote 转换为 JPEG、PNG 或 TIFF 图像

在 C# 中将 OneNote 文档转换为 PDF

如果您有一个想与他人分享的 Microsoft OneNote 文件,但不希望他们能够更改格式,您可以将文件保存为 PDF。以下代码展示了如何将 OneNote 文件的所有或特定页面转换为 PDF 文件。

// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_LoadingAndSaving();
// Load the document into Aspose.Note.
Document oneFile = new Document(dataDir + "Aspose.one");
dataDir = dataDir + "SaveWithDefaultSettings_out.pdf";
// Save the document as PDF
oneFile.Save(dataDir, SaveFormat.Pdf);

了解更多关于 在 C# 中将 OneNote 文档转换为 PDF

使用 C# 读取 OneNote 文件

  • 显示 OneNote 文件中的所有文本 - 有时开发人员需要从 OneNote 文件中获取一些文本。以下代码提取并显示 OneNote 文件中的所有文本内容。
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_Text();
// Load the document into Aspose.Note.
Document oneFile = new Document(dataDir + "Aspose.one");
// Retrieve text
string text = string.Join(Environment.NewLine, oneFile.GetChildNodes<RichText>().Select(e => e.Text)) + Environment.NewLine;
// Print text on the output screen
Console.WriteLine(text);

阅读更多关于 从 OneNote 文档中提取文本

  • 获取 OneNote 文档中的页面数量 - 一个 OneNote 文档可能有多个页面:您可以显示页面计数。计数包括所有级别的页面。以下代码使用 C# 获取 OneNote 文件的页面计数。
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_Pages();
// Load the document into Aspose.Note.
Document oneFile = new Document(dataDir + "Aspose.one");
// Get number of pages
int count = oneFile.GetChildNodes<Page>().Count;
// Print count on the output screen
Console.WriteLine(count);

有关更多帮助,请参阅我们的指南 获取 OneNote 文档中的页面数量

  • 获取 OneNote 文档中的所有页面信息 - Aspose.Note API 允许开发人员访问每个页面的详细信息,例如页面标题、作者、页面级别、页面大小、创建时间等。以下是从 OneNote 文件中获取每个页面详细信息的代码。
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_Pages();
// Load the document into Aspose.Note.
Document oneFile = new Document(dataDir + "Aspose.one");
// Get all Page nodes
IList<Page> pages = oneFile.GetChildNodes<Page>();
foreach (Page page in pages)
{
Console.WriteLine("LastModifiedTime: {0}", page.LastModifiedTime);
Console.WriteLine("CreationTime: {0}", page.CreationTime);
Console.WriteLine("Title: {0}", page.Title);
Console.WriteLine("Level: {0}", page.Level);
Console.WriteLine("Author: {0}", page.Author);
Console.WriteLine();
}

阅读更多以获取进一步帮助:获取 OneNote 文档中每个页面的信息

  • 从 OneNote 文档的特定页面获取文本 - Aspose.Note 还允许您根据 OneNote 文档中的特定页面编号检索文本。以下代码展示了如何操作。
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_Text();
// Load the document into Aspose.Note.
Document oneFile = new Document(dataDir + "Aspose.one");
// Get list of page nodes
IList<Node> nodes = oneFile.GetChildNodes<Node>();
if (nodes.Count > 0 && nodes[0].NodeType == NodeType.Page)
{
Page page = (Page)nodes[0];
// Retrieve text
string text = string.Join(Environment.NewLine, page.GetChildNodes<RichText>().Select(e => e.Text)) + Environment.NewLine;
// Print text on the output screen
Console.WriteLine(text);
}

查看本文,了解如何 从 OneNote 文档的指定页面提取文本

  • 获取 OneNote 文档中的图像信息 - Aspose.Note 允许开发人员访问文档中每个图像的详细信息,例如文件名、宽度、高度、扩展名和最后修改时间。以下代码展示了如何获取 OneNote 文件中每个图像的详细信息。
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_Images();
// Load the document into Aspose.Note.
Document oneFile = new Document(dataDir + "Aspose.one");
// Get all Image nodes
IList<Aspose.Note.Image> images = oneFile.GetChildNodes<Aspose.Note.Image>();
foreach (Aspose.Note.Image image in images)
{
Console.WriteLine("Width: {0}", image.Width);
Console.WriteLine("Height: {0}", image.Height);
Console.WriteLine("OriginalWidth: {0}", image.OriginalWidth);
Console.WriteLine("OriginalHeight: {0}", image.OriginalHeight);
Console.WriteLine("FileName: {0}", image.FileName);
Console.WriteLine("LastModifiedTime: {0}", image.LastModifiedTime);
Console.WriteLine();
}

有关更多帮助,请参阅我们的指南 [获取 OneNote 文档中每个图像的信息

]8

使用 C# 从 OneNote 文件中提取图像

  • 从 OneNote 文档中提取所有图像 - 有时您希望将屏幕截图和剪辑作为实际图像文件。Aspose.Note 具有此功能,允许开发人员将这些文件保存为图像。以下代码提取 OneNote 文件中的所有图像。
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_Images();
// Load the document into Aspose.Note.
Document oneFile = new Document(dataDir + "Aspose.one");
// Get all Image nodes
IList<Aspose.Note.Image> nodes = oneFile.GetChildNodes<Aspose.Note.Image>();
foreach (Aspose.Note.Image image in nodes)
{
using (MemoryStream stream = new MemoryStream(image.Bytes))
{
using (Bitmap bitMap = new Bitmap(stream))
{
// Save image bytes to a file
bitMap.Save(String.Format(dataDir + "{0}", Path.GetFileName(image.FileName)));
}
}
}

探索如何 使用 C# 从 OneNote 文档中提取图像

使用 C# 在 OneNote 文件中搜索和替换文本

在编辑 OneNote 文档时,查找和替换文本非常有用。例如,如果项目名称更改,您可以轻松地将旧项目名称的所有实例替换为新名称。在 Aspose.Note 中,使用 RichText 类替换特定文本。

  • 在 OneNote 文档的所有页面上搜索和替换文本 - 以下代码在 OneNote 文件的所有页面上搜索和替换文本。
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_Text();
Dictionary<string, string> replacements = new Dictionary<string, string>();
replacements.Add("Some task here", "New Text Here");
// Load the document into Aspose.Note.
Document oneFile = new Document(dataDir + "Aspose.one");
// Get all RichText nodes
IList<RichText> textNodes = oneFile.GetChildNodes<RichText>();
foreach (RichText richText in textNodes)
{
foreach (KeyValuePair<string, string> kvp in replacements)
{
if (richText != null && richText.Text.Contains(kvp.Key))
{
// Replace text of a shape
richText.Text = richText.Text.Replace(kvp.Key, kvp.Value);
}
}
}
dataDir = dataDir + "ReplaceTextOnAllPages_out.pdf";
// Save to any supported file format
oneFile.Save(dataDir, SaveFormat.Pdf);

阅读更详细的文章,了解如何 以编程方式在 OneNote 文件中查找和替换文本

  • 在 OneNote 文档的特定页面上搜索和替换文本 - 以下是搜索和替换 OneNote 文件特定页面上文本的代码。
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_Text();
Dictionary<string, string> replacements = new Dictionary<string, string>();
replacements.Add("voice over", "voice over new text");
// Load the document into Aspose.Note.
Document oneFile = new Document(dataDir + "Aspose.one");
IList<Page> pageNodes = oneFile.GetChildNodes<Page>();
// Get all RichText nodes
IList<RichText> textNodes = pageNodes[0].GetChildNodes<RichText>();
foreach (RichText richText in textNodes)
{
foreach (KeyValuePair<string, string> kvp in replacements)
{
if (richText != null && richText.Text.Contains(kvp.Key))
{
// Replace text of a shape
richText.Text = richText.Text.Replace(kvp.Key, kvp.Value);
}
}
}
dataDir = dataDir + "ReplaceTextOnParticularPage_out.pdf";
// Save to any supported file format
oneFile.Save(dataDir, SaveFormat.Pdf);

有关更多帮助,请参阅我们的指南 在特定页面上替换文本

结论

总之,Aspose.Note for .NET 允许在您的 C# 应用程序中处理 OneNote 文档,提供了一种强大的解决方案,可以轻松读取、转换和操作文件。通过集成 Aspose.Note for .NET,您可以毫不费力地将 OneNote 文件转换为各种格式,提取关键信息,并以最小的努力操作内容。它满足开发人员的多样化需求,确保您能够精确高效地处理 OneNote 处理任务。

迈出下一步

准备好转变您的 OneNote 文档工作流程了吗?今天就 下载 Aspose.Note for .NET,亲自体验其带来的好处。有关更多见解和详细指南,请阅读 文档,探索我们的 相关文章,或联系 我们的免费支持论坛 获取帮助。现在就开始提升您的生产力并简化开发过程吧!

相关文章