该文将介绍如何使用spire.presentation for java来提取ppt中smartart图形的文本内容。
首先, 准备一个文档中含有smartart图形的幻灯片文件:
提取ppt中smartart图形的文本内容代码如下:
import com.spire.presentation.presentation;
import com.spire.presentation.diagrams.ismartart;
import java.io.*;
public class extracttextfromsmartart {
public static void main(string[] args) throws exception {
presentation presentation = new presentation();
presentation.loadfromfile("sample.pptx");
//新建txt文档
string result = "output/extracttextfromsmartart.txt";
file file=new file(result);
if(file.exists()){
file.delete();
}
file.createnewfile();
filewriter fw =new filewriter(file,true);
bufferedwriter bw =new bufferedwriter(fw);
bw.write("below is extracted text from smartart:" "\r\n");
//遍历所有幻灯片并获取smartart图形.
for (int i = 0; i < presentation.getslides().getcount(); i )
{
for (int j = 0; j < presentation.getslides().get(i).getshapes().getcount(); j )
{
if (presentation.getslides().get(i).getshapes().get(j) instanceof ismartart)
{
ismartart smartart = (ismartart)presentation.getslides().get(i).getshapes().get(j);
//提取smartart中的文本
for (int k = 0; k < smartart.getnodes().getcount(); k )
{
bw.write(smartart.getnodes().get(k).gettextframe().gettext() "\r\n");
}
}
}
}
bw.flush();
bw.close();
fw.close();
}
}
效果图: