本文介绍如何通过Java程序代码来提取PPT SmartArt图形中的文本。在此Java代码环境中使用了辅助工具PPT类库—Free Spire.Presentation for Java,可通过官网下载jar包,下载后,解压,并将lib文件夹下的Spire.Presentation.jar文件导入Java程序。
import com.spire.presentation.*;
import com.spire.presentation.diagrams.ISmartArt;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
public class GetTextOfSmartArt {
public static void main(String[] args) throws Exception{
//创建实例,加载测试文档
Presentation presentation = new Presentation();
presentation.loadFromFile("AddSmartArt.pptx");
//新建txt文档,用于写入提取出来的文本
String result = "extractTextOfSmartArt.txt";
File file=new File(result);
if(file.exists()){
file.delete();
}
file.createNewFile();
FileWriter fw =new FileWriter(file,true);
BufferedWriter bw =new BufferedWriter(fw);
//遍历所有幻灯片并获取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中的文本,写入txt
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();
}
}
文本提取结果:
<完>