Java 中的高级演示文稿文本提取
从幻灯片提取文本
Aspose.Slides for Java 提供了 SlideUtil 类。该类公开了多个重载的静态方法,用于从演示文稿或幻灯片中提取完整文本。要从 PPTX 演示文稿的幻灯片中提取文本,使用由 SlideUtil 类公开的 getAllTextBoxes 重载静态方法。此方法接受 Slide 对象作为参数。
执行时,Slide 方法会扫描作为参数传入的幻灯片的全部文本,并返回一个 TextFrame 对象数组。这意味着可以获取文本的所有格式信息。下面的代码片段提取了演示文稿第一张幻灯片上的所有文本:
//实例化表示 PPTX 文件的 Presentation 类
Presentation pres = new Presentation("demo.pptx");
try {
for (ISlide slide : pres.getSlides())
{
//从 PPTX 的所有幻灯片获取 ITextFrame 对象数组
ITextFrame[] textFramesPPTX = SlideUtil.getAllTextBoxes(slide);
//遍历 TextFrames 数组
for (int i = 0; i < textFramesPPTX.length; i++) {
//遍历当前 ITextFrame 中的段落
for (IParagraph para : textFramesPPTX[i].getParagraphs()) {
//遍历当前 IParagraph 中的部分
for (IPortion port : para.getPortions()) {
//显示当前部分的文本
System.out.println(port.getText());
//显示文本的字体高度
System.out.println(port.getPortionFormat().getFontHeight());
//显示文本的字体名称
if (port.getPortionFormat().getLatinFont() != null)
System.out.println(port.getPortionFormat().getLatinFont().getFontName());
}
}
}
}
} finally {
pres.dispose();
}
从演示文稿提取文本
要扫描整个演示文稿的文本,请使用 SlideUtil 类公开的 getAllTextFrames 静态方法。它接受两个参数:
- 首先,一个表示要提取文本的演示文稿的 Presentation 对象。
- 其次,一个布尔值,用于确定在扫描演示文稿文本时是否包括母版幻灯片。
该方法返回一个包含文本格式信息的 TextFrame 对象数组。下面的代码扫描演示文稿的文本及其格式信息,包括母版幻灯片。
//实例化表示 PPTX 文件的 Presentation 类
Presentation pres = new Presentation("demo.pptx");
try {
//从 PPTX 的所有幻灯片获取 ITextFrame 对象数组
ITextFrame[] textFramesPPTX = SlideUtil.getAllTextFrames(pres, true);
//遍历 TextFrames 数组
for (int i = 0; i < textFramesPPTX.length; i++)
{
//遍历当前 ITextFrame 中的段落
for (IParagraph para : textFramesPPTX[i].getParagraphs())
{
//遍历当前 IParagraph 中的部分
for (IPortion port : para.getPortions())
{
//显示当前部分的文本
System.out.println(port.getText());
//显示文本的字体高度
System.out.println(port.getPortionFormat().getFontHeight());
//显示文本的字体名称
if (port.getPortionFormat().getLatinFont() != null)
System.out.println(port.getPortionFormat().getLatinFont().getFontName());
}
}
}
} finally {
pres.dispose();
}
分类与快速文本提取
Presentation 类新增了静态方法 getPresentationText。该方法有三个重载版本:
public IPresentationText getPresentationText(String file, int mode);
public IPresentationText getPresentationText(InputStream stream, int mode);
public IPresentationText getPresentationText(InputStream stream, int mode, ILoadOptions options);
The TextExtractionArrangingMode enum argument indicates the mode to organize the output of text result and can be set to the following values:
- Unarranged - The raw text with no respect to position on the slide
- Arranged - The text is positioned in the same order as on the slide
Unarranged mode can be used when speed is critical, it’s faster than Arranged mode.
IPresentationText represents the raw text extracted from the presentation. It contains a getSlidesText method which returns an array of ISlideText objects. Every object represent the text on the corresponding slide. ISlideText object have the following methods:
- ISlideText.getText - The text on the slide’s shapes
- ISlideText.getMasterText - The text on the master page’s shapes for this slide
- ISlideText.getLayoutText - The text on the layout page’s shapes for this slide
- ISlideText.getNotesText - The text on the notes page’s shapes for this slide
There is also a SlideText class which implements the ISlideText interface.
The new API can be used like this:
IPresentationText text1 = PresentationFactory.getInstance().getPresentationText("presentation.pptx", TextExtractionArrangingMode.Unarranged);
System.out.println(text1.getSlidesText()[0].getText());
System.out.println(text1.getSlidesText()[0].getLayoutText());
System.out.println(text1.getSlidesText()[0].getMasterText());
System.out.println(text1.getSlidesText()[0].getNotesText());
常见问题
Aspose.Slides 在文本提取过程中处理大型演示文稿的速度如何?
Aspose.Slides 已针对高性能进行优化,能够高效处理甚至是大型演示文稿,因此适用于实时或批量处理场景。
Aspose.Slides 能够从演示文稿中的表格和图表提取文本吗?
是的,Aspose.Slides 完全支持从表格、图表及其他复杂幻灯片元素中提取文本,使您能够轻松访问和分析所有文本内容。
提取演示文稿文本是否需要特殊的 Aspose.Slides 许可证?
您可以使用 Aspose.Slides 的免费试用版进行文本提取,但它存在一些限制,例如只能处理有限数量的幻灯片。若需无限制使用并处理更大的演示文稿,建议购买完整许可证。