一、概述及環境準備
SmartArt 圖形通過將文字、圖形從多種不同布局、組合來表現內容和觀點的邏輯關系,能夠快速、有效地傳達設計者的意圖和資訊,這種圖文表達的視覺表示形式常用于PPT,Word,Excel等辦公檔案中,本文將以在PPT中創建SmartArt圖形為例來介紹通過Java程式來添加SmartArt圖形到PPT的方法,以及如何讀取SmartArt圖形中的文本內容,
工具:Free Spire.Presentation for Java(免費版)
Jar獲取及匯入:官網下載jar包,并解壓將lib檔案夾下的jar檔案匯入Java程式,或者通過maven倉庫下載匯入,
二、代碼示例
1. Java在PPT中創建SmartArt圖形
這里創建SmartArt形狀時,可在默認創建的形狀中添加內容,也可以自定義圖形節點來添加內容,
import com.spire.presentation.*; import com.spire.presentation.diagrams.*; public class SmartArt { public static void main(String[] args) throws Exception{ //創建PPT檔案,獲取一張幻燈片(創建的空白PPT檔案,默認包含一張幻燈片) Presentation ppt = new Presentation(); ISlide slide = ppt.getSlides().get(0); //創建SmartArt圖形1 ISmartArt smartArt1 = slide.getShapes().appendSmartArt(50,50,200,200, SmartArtLayoutType.BASIC_CYCLE);//在幻燈片指定位置添加指定大小和布局型別的SmartArt圖形 smartArt1.setColorStyle(SmartArtColorType.COLORFUL_ACCENT_COLORS_4_TO_5);//設定SmartArt圖形顏色型別 smartArt1.setStyle(SmartArtStyleType.INTENCE_EFFECT);//設定SmartArt圖形樣式 ISmartArtNode smartArtNode1 = smartArt1.getNodes().get(0); smartArtNode1.getTextFrame().setText("設計");//獲取默認節點,添加內容 smartArt1.getNodes().get(1).getTextFrame().setText("模仿"); smartArt1.getNodes().get(2).getTextFrame().setText("學習"); smartArt1.getNodes().get(3).getTextFrame().setText("實踐"); smartArt1.getNodes().get(4).getTextFrame().setText("創新"); //創建SmartArt圖形2,自定義節點內容 ISmartArt smartArt2 = slide.getShapes().appendSmartArt(400,200,200,200,SmartArtLayoutType.BASIC_RADIAL); smartArt2.setColorStyle(SmartArtColorType.DARK_2_OUTLINE); smartArt2.setStyle(SmartArtStyleType.MODERATE_EFFECT); //洗掉默認的節點(SmartArt中的圖形) for (Object a : smartArt2.getNodes()) { smartArt2.getNodes().removeNode((ISmartArtNode) a); } //添加一個母節點 ISmartArtNode node2 = smartArt2.getNodes().addNode(); //在母節點下添加三個子節點 ISmartArtNode node2_1 = node2.getChildNodes().addNode(); ISmartArtNode node2_2 = node2.getChildNodes().addNode(); ISmartArtNode node2_3 = node2.getChildNodes().addNode(); //在節點上設定文字及文字大小 node2.getTextFrame().setText("設備"); node2.getTextFrame().getTextRange().setFontHeight(14f); node2_1.getTextFrame().setText("機械"); node2_1.getTextFrame().getTextRange().setFontHeight(12f); node2_2.getTextFrame().setText("電氣"); node2_2.getTextFrame().getTextRange().setFontHeight(12f); node2_3.getTextFrame().setText("自動化"); node2_3.getTextFrame().getTextRange().setFontHeight(12f); // 保存檔案 ppt.saveToFile("AddSmartArt.pptx",FileFormat.PPTX_2013); ppt.dispose(); } }
創建結果:

2. 讀取SmartArt中的文本
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(); } }
文本提取結果:

(完)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/181227.html
標籤:Java
上一篇:直播基本流程
