SmartArt 圖形經過將文字、圖形從多種不一樣佈局、組合來表現內容和觀點的邏輯關係,可以快速、有效地傳達設計者的意圖和信息。這種圖文表達的視覺表示形式經常使用於PPT,Word,Excel等辦公文檔中。本篇文章以在PPT中建立SmartArt圖形爲例來介紹經過Java程序來實現的具體方法。
工具:Free Spire.Presentation for Java(免費版)
Jar獲取及導入:官網下載jar包,並解壓將lib文件夾下的jar文件導入Java程序,或者經過maven倉庫下載導入。html
這裏建立SmartArt形狀時,可在默認建立的形狀中添加內容,也能夠自定義圖形節點來添加內容。java
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(); } }
建立結果:node
(完)app