这篇文章将介绍如何使用spire.presentation for java提取powerpoint文档中的文本。
源文档:
import com.spire.presentation.*;
import java.io.filewriter;
public class extracttext {
public static void main(string[] args) throws exception {
//创建presentation实例
presentation ppt = new presentation();
//加载powerpoint文档
ppt.loadfromfile("input.pptx");
stringbuilder buffer = new stringbuilder();
//遍历文档中的幻灯片,提取文本
for (object slide : ppt.getslides()) {
for (object shape : ((islide) slide).getshapes()) {
if (shape instanceof iautoshape) {
for (object tp : ((iautoshape) shape).gettextframe().getparagraphs()) {
buffer.append(((paragraphex) tp).gettext());
}
}
}
}
//保存文本到文本文件
filewriter writer = new filewriter("extracttext.txt");
writer.write(buffer.tostring());
writer.flush();
writer.close();
}
}
输出: