本文介紹如何通過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();
}
}
文本提取結果:
<完>