zoukankan      html  css  js  c++  java
  • Java 在PPT中创建SmartArt图形、读取SmartArt图形中的文本

    一、概述及环境准备

    SmartArt 图形通过将文字、图形从多种不同布局、组合来表现内容和观点的逻辑关系,能够快速、有效地传达设计者的意图和信息。这种图文表达的视觉表示形式常用于PPT,Word,Excel等办公文档中。本文将以在PPT中创建SmartArt图形为例来介绍通过Java程序来添加SmartArt图形到PPT的方法,以及如何读取SmartArt图形中的文本内容。

    工具:Free Spire.Presentation for Java(免费版)

    Jar获取及导入:官网下载jar包,并解压将lib文件夹下的jar文件导入Java程序,或者通过maven仓库下载导入

    二、代码示例

     1. Java在PPT中创建SmartArt图形

    这里创建SmartArt形状时,可在默认创建的形状中添加内容,也可以自定义图形节点来添加内容。

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

    创建结果:

     

    2. 读取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 GetTextOfSmartArt {
        public static void main(String[] args) throws Exception{
            //创建实例,加载测试文档
            Presentation presentation = new Presentation();
            presentation.loadFromFile("AddSmartArt.pptx");
    
            //新建txt文档,用于写入提取出来的文本
            String result = "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);
    
            //遍历所有幻灯片并获取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() + "
    ");
                        }
                    }
                }
            }
            bw.flush();
            bw.close();
            fw.close();
        }
    }

    文本提取结果:

    (完)

  • 相关阅读:
    LeetCode 32. 最长有效括号(Longest Valid Parentheses)
    LeetCode 141. 环形链表(Linked List Cycle)
    LeetCode 160. 相交链表(Intersection of Two Linked Lists)
    LeetCode 112. 路径总和(Path Sum)
    LeetCode 124. 二叉树中的最大路径和(Binary Tree Maximum Path Sum)
    LightGBM新特性总结
    sql service 事务与锁
    C#泛型实例详解
    C# 中的委托和事件(详解)
    C# DateTime日期格式化
  • 原文地址:https://www.cnblogs.com/Yesi/p/12964332.html
Copyright © 2011-2022 走看看