在之前的文章中介紹過如何在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中提取到的文本:" + "\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(); } }
代碼運行結果:

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/195914.html
標籤:Java
上一篇:Java面向物件之內部類
