笔记控件Aspose.Note教程:从 OneNote 文档中提取文本或图像

本篇文章主要介绍了如何使用Aspose.Note控件从 OneNote 文档中提取文本或图像,欢迎查阅!

我们可以在 OneNote 文档中以文字、图画、截屏、音频评论等形式收集和整理笔记。我们有时可能需要在 Java 应用程序中以编程方式从 OneNote 文档中提取文本或图像。这种提取允许我们单独重用提取的文本或图像。在本文中,我们将学习 如何使用 Java 从 OneNote 文档中提取文本或图像。

 

(一) Java API 从 OneNote 中提取文本或图像

为了从OneNote文档中提取文本和图像  ,我们将使用 Aspose.Note API。它允许在不使用 MS OneNote 的情况下以编程方式创建、阅读和转换 OneNote 文档。请下载API 的 JAR 或在基于 Maven 的 Java 应用程序中添加以下pom.xml配置。

<repository><id>AsposeJavaAPI</id><name>Aspose Java API</name><url>http://repository.aspose.com/repo/</url></repository><dependency><groupId>com.aspose</groupId><artifactId>aspose-note</artifactId><version>22.1</version><classifier>jdk17</classifier></dependency>
(二) 使用 Java 从 OneNote 文档中提取所有文本

我们可以按照下面给出的步骤轻松地从 OneNote 文档中提取所有文本:

  1. 首先,使用Document类加载 OneNote 文件 。
  2. 之后, 以RichText.class作为参数调用GetChildNodes 方法以提取文本。
  3. 最后,显示提取的文本。

以下代码示例展示了如何使用 Java 从 OneNote 文件中提取所有文本。

// This code example demonstrates how to Extract all the text from OneNode document.// Load the document into Aspose.Note.Document oneFile = new Document("D:FilesNoteSample1.one"); // Retrieve textList<RichText> textNodes = (List<RichText>) oneFile.getChildNodes(RichText.class);for (RichText richText : textNodes) {if(!richText.getText().isBlank())System.out.println(richText.getText().toString());}

使用 Java 从 OneNote 文档中提取所有文本
(三) 从 Java 中的 OneNote 文档的特定页面获取文本

我们可以按照以下步骤从 OneNote 文档的特定页面中提取文本:

  1. 首先,使用Document类加载 OneNote 文件 。
  2. 接下来, 使用Page.class 作为参数调用GetChildNodes 方法 以提取页面。
  3. 然后,通过页面列表中的索引获取特定页面。
  4. 之后,使用 GetChildNodes 方法以RichText.class 作为参数获取页面的文本项列表。
  5. 最后,显示提取的文本。

以下代码示例展示了如何使用 Java 从 OneNote 文件的特定页面中提取文本。

// This code example demonstrates how to Extract text from a specific page of a OneNode document.// Load the document into Aspose.NoteDocument doc = new Document("D:FilesNoteSample1.one");// Get list of page nodesList<Page> pages = doc.getChildNodes(Page.class);// Get page by indexPage page = pages.get(0);// Get text of the pageList<RichText> textNodes = (List<RichText>) page.getChildNodes(RichText.class);// Show textfor (RichText richText : textNodes) {if(!richText.getText().isBlank())System.out.println(richText.getText().toString());}

从 Java 中的 OneNote 文档的特定页面获取文本

我们可以逐页遍历所有页面并提取每个页面的文本,如下面的代码示例所示:

// This code example demonstrates how to Extract text from pages of a OneNode document.// Load the document into Aspose.Note.Document doc = new Document("D:FilesNoteSample1.one");// Get list of page nodesList<Page> pages = doc.getChildNodes(Page.class);for (Page p : pages) {System.out.println("---- Page Started Here ----");List<RichText> textNodes = (List<RichText>) p.getChildNodes(RichText.class);for (RichText richText : textNodes) {if(!richText.getText().isBlank())System.out.println(richText.getText().toString());}System.out.println("---- Page Ended Here ----");System.out.println();}

从 Java 中的 OneNote 文档的特定页面获取文本
(四) 使用 Java 从 OneNote 文档中提取图像

我们还可以按照以下步骤从 OneNote 文档中提取图像:

  1. 首先,使用Document类加载 OneNote 文件 。
  2. 之后,使用 GetChildNodes 方法以Image.class作为参数获取图像列表。
  3. 最后,显示图像属性并保存到本地磁盘。

以下代码示例展示了如何使用 Java 从 OneNote 文件中提取图像。

// This code example demonstrates how to Extract images from OneNode document.// Load the document into Aspose.NoteDocument doc = new Document("D:FilesNoteSample1.one");// Get all imagesList<Image> list = doc.getChildNodes(Image.class);System.out.printf("Total Images: %snn", list.size());// Traverse the listfor (int i = 0; i < list.size(); i++) {Image image = list.get(i);// Show image propertiesSystem.out.println("Width: " + image.getWidth());System.out.println("Height: " + image.getHeight());System.out.println("OriginalWidth: " + image.getOriginalWidth());System.out.println("OriginalHeight: " + image.getOriginalHeight());System.out.println("FileName: " + image.getFileName());System.out.println("LastModifiedTime: " + image.getLastModifiedTime());String outputFile = "ExtractImages_out" + i + "_" + image.getFileName();// Save the imagebyte[] buffer = image.getBytes();Files.write(Paths.get("D:FilesNoteImages" + outputFile), buffer);System.out.printf("File saved: %sn", outputFile);}

使用 Java 从 OneNote 文档中提取图像
(五) 结论

在本文中,我们学习 了如何从整个 OneNote 文档或文档的特定页面 中提取文本。我们还 了解了如何以编程方式从 OneNote 文档中提取图像。此外,您可以使用文档了解更多关于 Aspose.Note for Java API 的信息。如有任何问题也可以联系我们。


欢迎下载|体验更多Aspose产品 

获取更多信息请咨询在线客服 或 加入Aspose技术交流群(

笔记控件Aspose.Note教程:从 OneNote 文档中提取文本或图像
标签:

来源:慧都

声明:本站部分文章及图片转载于互联网,内容版权归原作者所有,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2022年1月27日
下一篇 2022年1月27日

相关推荐

发表回复

登录后才能评论