DEV Community

CodeSharing
CodeSharing

Posted on

[Java] How to Create a SmartArt Graphic in PowerPoint

A SmartArt graphic is a visual representation of information and ideas, it can turn ordinary text into the predefined graphic, which makes the text information easier to understand. This article will show you how to create a SmartArt in PowerPoint and custom its layout by using Free Spire.Presentation for Java.

Installation
Method 1: Download the Free Spire.Presentation for Java and unzip it. Then add the Spire.Presentation.jar file to your project as dependency.

Method 2: You can also add the jar dependency to your maven project by adding the following configurations to the pom.xml.

<repositories>
        <repository>
            <id>com.e-iceblue</id>
            <name>e-iceblue</name>
            <url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
        </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.presentation.free</artifactId>
        <version>2.6.1</version>
    </dependency>
</dependencies>

Enter fullscreen mode Exit fullscreen mode

Creating SmartArt Graphic:

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 {

        //Create a PowerPoint document
        Presentation presentation = new Presentation();

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

        //Insert an Organization Chart into the slide
        ISmartArt smartArt = slide.getShapes().appendSmartArt(60, 60, 500, 400, SmartArtLayoutType.ORGANIZATION_CHART);

        //Set the color of the smartart
        smartArt.setStyle(SmartArtStyleType.MODERATE_EFFECT);
        smartArt.setColorStyle(SmartArtColorType.GRADIENT_RANGE_ACCENT_1);

        //Remove all default nodes
        for (Object a : smartArt.getNodes()) {
            smartArt.getNodes().removeNode(0);
        }

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

        //Add 4 child nodes
        ISmartArtNode node1_1 = node1.getChildNodes().addNode();
        ISmartArtNode node1_2 = node1.getChildNodes().addNode();
        ISmartArtNode node1_3 = node1.getChildNodes().addNode();
        ISmartArtNode node1_4 = node1.getChildNodes().addNode();

        //Add text to each node and set the font size
        node1.getTextFrame().setText("General Manager");
        node1.getTextFrame().getTextRange().setFontHeight(14f);
        node1_1.getTextFrame().setText("Marketing Manager");
        node1_1.getTextFrame().getTextRange().setFontHeight(12f);
        node1_2.getTextFrame().setText("Operation Manager");
        node1_2.getTextFrame().getTextRange().setFontHeight(12f);
        node1_3.getTextFrame().setText("Human Resource Manager");
        node1_3.getTextFrame().getTextRange().setFontHeight(12f);
        node1_4.getTextFrame().setText("Account Manager");
        node1_4.getTextFrame().getTextRange().setFontHeight(12f);

        //Save the document
        presentation.saveToFile("SmartArt.pptx", FileFormat.PPTX_2010);
        presentation.dispose();
    }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)