该文将介绍如何使用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();
}
}
效果图: