Java 在PPT中添加、提取SmartArt圖形

一、前言及環(huán)境構(gòu)建

SmartArt圖形常用于將文字量少、層次較明顯的文本轉(zhuǎn)換為具有各種附屬關(guān)系、并列關(guān)系等關(guān)系結(jié)構(gòu)的文檔插圖,以此來快速、輕松、有效地傳達(dá)信息。本文將通過Java程序來介紹為PPT文檔添加SmartArt圖形的方法,并演示如何提取文檔中SmartArt圖形的文本信息。

此次教程所用工具為Free Spire.Presentation for Java(免費(fèi)版),可通過官網(wǎng)下載獲取。工具下載后進(jìn)行解壓,然后將lib文件夾下的Spire.Presentation.jar 文件導(dǎo)入Java程序?;蛘咭部芍苯油ㄟ^maven倉庫進(jìn)行工具的下載導(dǎo)入。

二、代碼演示

示例1 添加SmartArt圖形到PPT

以下代碼里包含了添加默認(rèn)SmartArt圖形及創(chuàng)建自定義圖形節(jié)點(diǎn)兩種方式。

import com.spire.presentation.*;

import com.spire.presentation.diagrams.*;

public class AddSmartArt {

public static void main(String[] args) throws Exception {

//創(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);//獲取默認(rèn)節(jié)點(diǎn),添加內(nèi)容

smartArtNode1.getTextFrame().setText("信息");

smartArt1.getNodes().get(1).getTextFrame().setText("溝通");

smartArt1.getNodes().get(2).getTextFrame().setText("分析");

smartArt1.getNodes().get(3).getTextFrame().setText("優(yōu)化");

smartArt1.getNodes().get(4).getTextFrame().setText("執(zhí)行");

//創(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 (Objecta : 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("生物");

node2.getTextFrame().getTextRange().setFontHeight(14f);

node2_1.getTextFrame().setText("動(dòng)物");

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("output/AddSmartArt.pptx",FileFormat.PPTX_2013);

ppt.dispose();

??????????? }

?????? }

添加效果:

示例2? 從PPT中提取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 ExtractTextFromSmartart {

public static void main(String[] args) throws Exception{

//創(chuàng)建實(shí)例,加載測試文檔

Presentation presentation = new Presentation();

presentation.loadFromFile("C:\\Users\\Test1\\Desktop\\AddSmartArt.pptx");

//新建txt文檔,用于寫入提取出來的文本

String result = "output/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);

bw.write("以下是從SmarArt圖形中提取出的文本信息:"+"\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中的文本,并寫入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();

??????????? }

??????? }

提取效果:

(本文完)

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容