一、概括及环境配置
在PowerPoint文档中,SmartArt图形能够直观地用于表现各种层级关系、附属关系、并列关系或循环关系等常用的关系结构。本文将通过Java代码来演示如何给PPT文档添加SmartArt图形及读取该图形的文本内容。
本教程使用到的控件是Free Spire.Presentation for Java(免费版)。在运行代码前,需将控件里的Jar包导入IDEA中。导入方式有两种:其一,在E-iceblue中文官网上下载产品包,解压后将lib文件夹下的Spire.Presentation.jar手动导入IDEA;其二,在IDEA中创建一个Maven仓库,然后在pom.xml文件下键入以下引用,最后点击“Import Changes”即可。
<repositories>
<repository>
<id>com.e-iceblue</id>
<url>http://repo.e-iceblue.cn/repository/maven-public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.presentation.free</artifactId>
<version>3.9.0</version>
</dependency>
</dependencies>
二、代码演示
创建SmartArt图形
在创建SmartArt图形时,Free Spire.Presentation for Java支持自定义设置图形样式,颜色,同时可以在节点上设置文字及文字大小。
import com.spire.presentation.FileFormat;
import com.spire.presentation.ISlide;
import com.spire.presentation.Presentation;
import com.spire.presentation.diagrams.*;
public class AddSmartArt {
public static void main(String[] args) throws Exception {
//创建PowerPoint文档
Presentation presentation = new Presentation();
//获取第一张幻灯片
ISlide slide = presentation.getSlides().get(0);
//添加SmartArt图形到幻灯片,并设置其位置与大小
ISmartArt smartArt = slide.getShapes().appendSmartArt(60,60,300,300,SmartArtLayoutType.BASIC_RADIAL);
//设置SmartArt的颜色
smartArt.setColorStyle(SmartArtColorType.COLORFUL_ACCENT_COLORS_4_TO_5);
//设置SmartArt的样式
smartArt.setStyle(SmartArtStyleType.INTENCE_EFFECT);
//删除默认的节点(SmartArt中的图形)
for (Object a : smartArt.getNodes()) {
smartArt.getNodes().removeNode((ISmartArtNode) a);
}
//添加一个母节点
ISmartArtNode node1 = smartArt.getNodes().addNode();
//在母节点下添加三个子节点
ISmartArtNode node1_1 = node1.getChildNodes().addNode();
ISmartArtNode node1_2 = node1.getChildNodes().addNode();
ISmartArtNode node1_3 = node1.getChildNodes().addNode();
//在节点上设置文字及文字大小
node1.getTextFrame().setText("设备");
node1.getTextFrame().getTextRange().setFontHeight(15f);
node1_1.getTextFrame().setText("机械");
node1_1.getTextFrame().getTextRange().setFontHeight(13f);
node1_2.getTextFrame().setText("电气");
node1_2.getTextFrame().getTextRange().setFontHeight(13f);
node1_3.getTextFrame().setText("自动化");
node1_3.getTextFrame().getTextRange().setFontHeight(13f);
// 保存文档
presentation.saveToFile("output/AddSmartArt.pptx",FileFormat.PPTX_2013);
presentation.dispose();
}
}
创建效果:
提取SmartArt图形的文本内容
import com.spire.presentation.Presentation;
import com.spire.presentation.diagrams.ISmartArt;
import java.io.*;
public class ExtractTextFromSmartArt {
public static void main(String[] args) throws Exception {
//加载PowerPoint示例文档
Presentation presentation = new Presentation();
presentation.loadFromFile("C:\\Users\\Test1\\Desktop\\AddSmartArt.pptx");
//新建txt文档
String result = "output/extractTextFromSmartArt.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("以下是示例PPT文档中的SmartArt图形的文本内容:" + "\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中的文本
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();
}
}
提取结果: