Java creates SmartArt graphics in PPT and reads text in SmartArt graphics

Keywords: Java Excel Maven

1, Overview and environmental preparation

SmartArt graphics can express the logical relationship between content and viewpoint through different layout and combination of text and graphics, which can quickly and effectively convey the intention and information of designers. This visual representation of graphic expression is commonly used in PPT, Word, Excel and other office documents. This article will take the creation of SmartArt graphics in PPT as an example to introduce the method of adding SmartArt graphics to PPT through Java program and how to read the text content in SmartArt graphics.

Tools: Free Spire.Presentation For Java (free)

Jar access and import: Official Website Download jar package And unzip the jar file under the lib folder into the Java program, or through the maven warehouse download and import.

2, Code example

1. Java creates SmartArt graphics in PPT

When you create a SmartArt shape here, you can add content to the shape created by default, or you can customize the graphic node to add content.

import com.spire.presentation.*;
import com.spire.presentation.diagrams.*;

public class SmartArt {
    public static void main(String[] args) throws Exception{
        //establish PPT Document, get a slide (blank created PPT Document, including one slide by default)
        Presentation ppt = new Presentation();
        ISlide slide = ppt.getSlides().get(0);

        //establish SmartArt Figure 1
        ISmartArt smartArt1 = slide.getShapes().appendSmartArt(50,50,200,200, SmartArtLayoutType.BASIC_CYCLE);//Add the SmartArt graphical
        smartArt1.setColorStyle(SmartArtColorType.COLORFUL_ACCENT_COLORS_4_TO_5);//set up SmartArt Graphic color type
        smartArt1.setStyle(SmartArtStyleType.INTENCE_EFFECT);//set up SmartArt Graphic styles
        ISmartArtNode smartArtNode1 = smartArt1.getNodes().get(0);
        smartArtNode1.getTextFrame().setText("Design");//Get default node, add content
        smartArt1.getNodes().get(1).getTextFrame().setText("imitate");
        smartArt1.getNodes().get(2).getTextFrame().setText("study");
        smartArt1.getNodes().get(3).getTextFrame().setText("practice");
        smartArt1.getNodes().get(4).getTextFrame().setText("innovate");


        //establish SmartArt Figure 2, custom node content
        ISmartArt smartArt2 = slide.getShapes().appendSmartArt(400,200,200,200,SmartArtLayoutType.BASIC_RADIAL);
        smartArt2.setColorStyle(SmartArtColorType.DARK_2_OUTLINE);
        smartArt2.setStyle(SmartArtStyleType.MODERATE_EFFECT);
        //Delete default node( SmartArt Graphics in)
        for (Object a : smartArt2.getNodes()) {
            smartArt2.getNodes().removeNode((ISmartArtNode) a);
        }
        //Add a parent node
        ISmartArtNode node2 = smartArt2.getNodes().addNode();
        //Add three child nodes under the parent node
        ISmartArtNode node2_1 = node2.getChildNodes().addNode();
        ISmartArtNode node2_2 = node2.getChildNodes().addNode();
        ISmartArtNode node2_3 = node2.getChildNodes().addNode();
        //Set text and text size on nodes
        node2.getTextFrame().setText("equipment");
        node2.getTextFrame().getTextRange().setFontHeight(14f);
        node2_1.getTextFrame().setText("Mechanics");
        node2_1.getTextFrame().getTextRange().setFontHeight(12f);
        node2_2.getTextFrame().setText("electrical");
        node2_2.getTextFrame().getTextRange().setFontHeight(12f);
        node2_3.getTextFrame().setText("automation");
        node2_3.getTextFrame().getTextRange().setFontHeight(12f);

        // Save document
        ppt.saveToFile("AddSmartArt.pptx",FileFormat.PPTX_2013);
        ppt.dispose();
    }
}

Create results:

 

2. Read text in 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{
        //Create instance, load test document
        Presentation presentation = new Presentation();
        presentation.loadFromFile("AddSmartArt.pptx");

        //newly build txt Document for writing extracted text
        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);

        //Traverse all slides and get SmartArt graphical.
        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);

                    //extract SmartArt Text in, writing 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();
    }
}

Text extraction results:

 

(end)

Posted by FezEvils on Tue, 26 May 2020 09:18:31 -0700