0
点赞
收藏
分享

微信扫一扫

Java实现在PPT中创建SmartArt图形的示例代码

一叶轻舟okok 2024-01-12 阅读 21

SmartArt其实就是一个文字的可视化工具,用户可在PowerPoint,Word,Excel中使用该特性创建各种图形图表。SmartArt 图形是信息和观点的视觉表示形式。可以通过从多种不同布局中进行选择来创建 SmartArt 图形,从而快速、轻松、有效地传达信息。简单的来说SmartArt就是PPT内建的逻辑图表,主要用于表达文本之间的逻辑关系,可帮助你快速、轻松、有效的传达信息。本文就将为您介绍如何通过Java应用程序在PPT中创建SmartArt图形。下面是我整理的具体步骤及方法,并附上Java代码供大家参考。


代码编译环境

IntelliJ IDEA 2019(jdk 1.8.0)

Presentation Jar包:Free Spire.Presentation for Java 5.1.0


引入jar包

导入方法1:

手动引入。将Free Spire. Presentation for Java下载到本地,解压,找到lib文件夹下的Spire. Presentation.jar文件。在IDEA中打开如下界面,将本地路径中的jar文件引入Java程序:

Java实现在PPT中创建SmartArt图形的示例代码_Java

导入方法2:如果您想通过 Maven安装,则可以在 pom.xml 文件中添加以下代码导入 JAR 文件。

1

2

3

4

5

6

7

8

9

10

11

12

13

14


<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>



创建 SmartArt 图形

  • 创建 SmartArt 图形时,可根据创建的图形,在预设的节点中添加内容;也可以根据设计需要自行添加节点或者删除节点。下面,是本次创建 SmartArt 图形的主要步骤:
  • 创建 Presentation 类的对象。
  • 通过 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) 方法设置图形颜色和样式。
  • 通过 IsmartArtNode.getNodes().get(int index)方法获取指定节点,然后使用ISmartArtNode.getTextFrame().setText(String string) 方法向节点添加内容。
  • 如需自定义节点内容,可在添加图形后,通过 ISmartArt.getNodes().removeNode(IsmartArtNode iSmartArtNode) 方法删除原有节点后,以 ISmartArt.getNodes().addNode() 方法添加节点和 IsmartArtNode.getChildNodes().addNode() 方法添加子节点,然后采用上一步骤的方法添加内容到自定义的节点。
  • 最后,使用 Presentation.saveToFile(String file, FileFormat fileFormat) 方法保存幻灯片文档到指定路径。


完整代码

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51


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

}

}



效果图

Java实现在PPT中创建SmartArt图形的示例代码_Java_02

到此这篇关于Java实现在PPT中创建SmartArt图形的示例代码的文章就介绍到这了,更多相关Java PPT创建SmartArt图形内容请搜索51CTO以前的文章或继续浏览下面的相关文章希望大家以后多多支持51CTO!

举报

相关推荐

0 条评论