SmartArt其實(shí)就是一個(gè)文字的可視化工具,用戶可在PowerPoint,Word,Excel中使用該特性創(chuàng)建各種圖形圖表。SmartArt 圖形是信息和觀點(diǎn)的視覺表示形式??梢酝ㄟ^從多種不同布局中進(jìn)行選擇來創(chuàng)建 SmartArt 圖形,從而快速、輕松、有效地傳達(dá)信息。簡(jiǎn)單的來說SmartArt就是PPT內(nèi)建的邏輯圖表,主要用于表達(dá)文本之間的邏輯關(guān)系,可幫助你快速、輕松、有效的傳達(dá)信息。本文就將為您介紹如何通過Java應(yīng)用程序在PPT中創(chuàng)建SmartArt圖形。下面是我整理的具體步驟及方法,并附上Java代碼供大家參考。
代碼編譯環(huán)境:
IntelliJ IDEA 2019(jdk 1.8.0)
Presentation Jar包:Free Spire.Presentation for Java 5.1.0
引入jar包
導(dǎo)入方法1:
手動(dòng)引入。將Free Spire. Presentation for Java下載到本地,解壓,找到lib文件夾下的Spire. Presentation.jar文件。在IDEA中打開如下界面,將本地路徑中的jar文件引入Java程序:
導(dǎo)入方法2:如果您想通過 Maven安裝,則可以在 pom.xml 文件中添加以下代碼導(dǎo)入 JAR 文件。
<repositories> <repository> <id>com.e-iceblue</id> <name>e-iceblue</name> <url>https://repo.e-iceblue.cn/repository/maven-public/</url> </repository> </repositories> <dependencies> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.presentation.free</artifactId> <version>5.1.0</version> </dependency> </dependencies>
創(chuàng)建 SmartArt 圖形
- 創(chuàng)建 SmartArt 圖形時(shí),可根據(jù)創(chuàng)建的圖形,在預(yù)設(shè)的節(jié)點(diǎn)中添加內(nèi)容;也可以根據(jù)設(shè)計(jì)需要自行添加節(jié)點(diǎn)或者刪除節(jié)點(diǎn)。下面,是本次創(chuàng)建 SmartArt 圖形的主要步驟:
- 創(chuàng)建 Presentation 類的對(duì)象。
- 通過 Presentation.getSlides().get(int index) 方法獲取指定幻燈片。
- 使用 ISlide.getShapes().appendSmartArt(float x, float y, float width, float height, SmartArtLayoutType layoutType) 方法添加 SmartArt 圖形到幻燈片。
- 使用 IsmartArt.setColorStyle(SmartArtColorType smartArtColorType)方法和IsmartArt.setStyle(SmartArtStyleType smartArtStyleType) 方法設(shè)置圖形顏色和樣式。
- 通過 IsmartArtNode.getNodes().get(int index)方法獲取指定節(jié)點(diǎn),然后使用ISmartArtNode.getTextFrame().setText(String string) 方法向節(jié)點(diǎn)添加內(nèi)容。
- 如需自定義節(jié)點(diǎn)內(nèi)容,可在添加圖形后,通過 ISmartArt.getNodes().removeNode(IsmartArtNode iSmartArtNode) 方法刪除原有節(jié)點(diǎn)后,以 ISmartArt.getNodes().addNode() 方法添加節(jié)點(diǎn)和 IsmartArtNode.getChildNodes().addNode() 方法添加子節(jié)點(diǎn),然后采用上一步驟的方法添加內(nèi)容到自定義的節(jié)點(diǎn)。
- 最后,使用 Presentation.saveToFile(String file, FileFormat fileFormat) 方法保存幻燈片文檔到指定路徑。
完整代碼
Java
import com.spire.presentation.*; import com.spire.presentation.diagrams.*; public class SmartArt { public static void main(String[] args) throws Exception{ //創(chuàng)建PPT文檔,獲取一張幻燈片(創(chuàng)建的空白PPT文檔,默認(rèn)包含一張幻燈片) Presentation ppt = new Presentation(); ISlide slide = ppt.getSlides().get(0); //創(chuàng)建SmartArt圖形1 ISmartArt smartArt1 = slide.getShapes().appendSmartArt(50,50,200,200, SmartArtLayoutType.BASIC_CYCLE);//在幻燈片指定位置添加指定大小和布局類型的SmartArt圖形 smartArt1.setColorStyle(SmartArtColorType.COLORFUL_ACCENT_COLORS_4_TO_5);//設(shè)置SmartArt圖形顏色類型 smartArt1.setStyle(SmartArtStyleType.INTENCE_EFFECT);//設(shè)置SmartArt圖形樣式 ISmartArtNode smartArtNode1 = smartArt1.getNodes().get(0);//獲取節(jié)點(diǎn) smartArtNode1.getTextFrame().setText("設(shè)計(jì)");//添加內(nèi)容 smartArt1.getNodes().get(1).getTextFrame().setText("求實(shí)"); smartArt1.getNodes().get(2).getTextFrame().setText("練習(xí)"); smartArt1.getNodes().get(3).getTextFrame().setText("實(shí)踐"); smartArt1.getNodes().get(4).getTextFrame().setText("創(chuàng)新"); //創(chuàng)建SmartArt圖形2,自定義節(jié)點(diǎn)內(nèi)容 ISmartArt smartArt2 = slide.getShapes().appendSmartArt(400,200,200,200,SmartArtLayoutType.BASIC_RADIAL); smartArt2.setColorStyle(SmartArtColorType.DARK_2_OUTLINE); smartArt2.setStyle(SmartArtStyleType.MODERATE_EFFECT); //刪除默認(rèn)的節(jié)點(diǎn)(SmartArt中的圖形) for (Object a : smartArt2.getNodes()) { smartArt2.getNodes().removeNode((ISmartArtNode) a); } //添加一個(gè)母節(jié)點(diǎn) ISmartArtNode node2 = smartArt2.getNodes().addNode(); //在母節(jié)點(diǎn)下添加三個(gè)子節(jié)點(diǎn) ISmartArtNode node2_1 = node2.getChildNodes().addNode(); ISmartArtNode node2_2 = node2.getChildNodes().addNode(); ISmartArtNode node2_3 = node2.getChildNodes().addNode(); //在節(jié)點(diǎn)上設(shè)置文字及文字大小 node2.getTextFrame().setText("設(shè)備"); node2.getTextFrame().getTextRange().setFontHeight(14f); node2_1.getTextFrame().setText("機(jī)械"); node2_1.getTextFrame().getTextRange().setFontHeight(12f); node2_2.getTextFrame().setText("電氣"); node2_2.getTextFrame().getTextRange().setFontHeight(12f); node2_3.getTextFrame().setText("自動(dòng)化"); node2_3.getTextFrame().getTextRange().setFontHeight(12f); //保存文檔 ppt.saveToFile("AddSmartArt.pptx",FileFormat.PPTX_2013); ppt.dispose(); } }
效果圖
文章來源:http://www.zghlxwxcb.cn/news/detail-416246.html
—本文完—文章來源地址http://www.zghlxwxcb.cn/news/detail-416246.html
到了這里,關(guān)于如何通過Java應(yīng)用程序在PPT中創(chuàng)建SmartArt圖形的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!