以下经验内容介绍如何通过Java程序代码来提取PPT SmartArt图形中的文本。
工具/原料
Free Spire.Presentation for Java(免费版)
Jar获取及导入:
1、通过e-iceblue官网下载jar包,下载后,解压将lib文件夹下的jar文件导入Java程序,或者通过maven仓库下载导入。
Java代码示例
1、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(); } }
2、文本提取结果: