Java adds SmartArt graphics to PPT

Keywords: Java Maven

SmartArt graphics is a visual representation of information and opinions. It has powerful functions of text to graphic and typesetting. This article shows you how to use Java code to create SmartArt graphics and customize the layout in your slides.

 

Using tool: free flame.presentation for Java (free version)

 

Jar file import method

Method 1:

download Free Spire.Presentation for Java Package and extract it, then import the spirit.presentation.jar package into your Java application from the lib folder. (see the figure below after importing successfully)

 

 

Method 2:

Import through Maven warehouse installation. Please refer to the link for detailed operation steps:

https://www.e-iceblue.cn/licensing/install-spirepdf-for-java-from-maven-repository.html

 

Java code example

 

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 {

        //Establish PowerPoint File
        Presentation presentation = new Presentation();

        //Get the first slide
        ISlide slide = presentation.getSlides().get(0);

        //Create organization chart in slide'Organization Chart'
        ISmartArt smartArt = slide.getShapes().appendSmartArt(60, 60, 500, 300, SmartArtLayoutType.ORGANIZATION_CHART);

        //Set up SmartArt Style and color of
        smartArt.setStyle(SmartArtStyleType.MODERATE_EFFECT);
        smartArt.setColorStyle(SmartArtColorType.DARK_2_OUTLINE);

        //Delete default node( SmartArt Graphics in)
        for (Object a : smartArt.getNodes()) {
            smartArt.getNodes().removeNode(0);
        }

        //Add a parent node
        ISmartArtNode node1 = smartArt.getNodes().addNode();

        //Add four child nodes under the parent node
        ISmartArtNode node1_1 = node1.getChildNodes().addNode();
        ISmartArtNode node1_2 = node1.getChildNodes().addNode();
        ISmartArtNode node1_3 = node1.getChildNodes().addNode();
        ISmartArtNode node1_4 = node1.getChildNodes().addNode();

        //Set text and text size on nodes
        node1.getTextFrame().setText("Company headquarters");
        node1.getTextFrame().getTextRange().setFontHeight(14f);
        node1_1.getTextFrame().setText("Investment Management Department");
        node1_1.getTextFrame().getTextRange().setFontHeight(12f);
        node1_2.getTextFrame().setText("Finance Department");
        node1_2.getTextFrame().getTextRange().setFontHeight(12f);
        node1_3.getTextFrame().setText("General Office");
        node1_3.getTextFrame().getTextRange().setFontHeight(12f);
        node1_4.getTextFrame().setText("Technology Department");
        node1_4.getTextFrame().getTextRange().setFontHeight(12f);

        //Save document
        presentation.saveToFile("SmartArt.pptx", FileFormat.PPTX_2010);
        presentation.dispose();
    }
}

Add SmartArt renderings:

Posted by lonerunner on Wed, 29 Apr 2020 08:43:24 -0700