在之前的文章中介绍过如何在PPT中添加SmartArt图形, 今天本文将介绍如何在Java程序中提取SmartArt 图形的文本内容。(使用工具: Free Spire.Presentation for Java)
JAR包导入
方法一:下载Free Spire.Presentation for Java包并解压缩,然后将lib文件夹下的jar包作为依赖项直接导入到Java应用程序中。
方法二:通过Maven仓库安装jar包,配置pom.xml文件的代码如下:
<repositories> <repository> <id>com.e-iceblue</id> <url>http://repo.e-iceblue.cn/repository/maven-public/</url> </repository> </repositories> <dependencies> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.presentation.free</artifactId> <version>3.9.0</version> </dependency> </dependencies>
Java代码示例
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("SmartArt.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("以下内容为从SmartArt中提取到的文本:" + " "); //遍历所有幻灯片并获取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() + " "); } } } } bw.flush(); bw.close(); fw.close(); } }
代码运行结果: