1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > spire抽取ppt图片java_Java 在PPT中添加 提取SmartArt图形

spire抽取ppt图片java_Java 在PPT中添加 提取SmartArt图形

时间:2019-09-17 23:14:24

相关推荐

spire抽取ppt图片java_Java 在PPT中添加 提取SmartArt图形

一、前言及环境构建

SmartArt图形常用于将文字量少、层次较明显的文本转换为具有各种附属关系、并列关系等关系结构的文档插图,以此来快速、轻松、有效地传达信息。本文将通过Java程序来介绍为PPT文档添加SmartArt图形的方法,并演示如何从文档中提取SmartArt图形的文本信息。

此次教程所用工具为Free Spire.Presentation for Java(免费版),可通过官网下载获取。工具下载后进行解压,然后将lib文件夹下的Spire.Presentation.jar 文件导入Java程序。或者也可直接通过maven仓库进行工具的下载导入。

二、代码演示

示例1 添加SmartArt图形到PPT

以下代码里包含了添加默认SmartArt图形及创建自定义图形节点两种方式。

import com.spire.presentation.*;

import com.spire.presentation.diagrams.*;

public class AddSmartArt {

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

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

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{

//创建实例,加载测试文档

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();

}

}

提取效果:

(本文完)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。