DEV Community

E-iceblue Product Family
E-iceblue Product Family

Posted on

Add, Manipulate and Delete SmartArt in PowerPoint in Java

A SmartArt is a visual representation of information. In MS PowerPoint, you can create many different layout types of SmartArt graphics to express your ideas. In this article, I will show you how to add, manipulate and delete SmartArt in a PowerPoint document programmatically in Java by using Free Spire.Presentation for Java API.

Add Dependencies

First of all, you need to add needed dependencies for including Free Spire.Presentation for Java into your Java project. There are two ways to do that.
If you use maven, you need to add the following code to your project’s pom.xml file.

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

For non-maven projects, download Free Spire.Presentation for Java pack from this website, unzip the package and add Spire.Presentation.jar in the lib folder into your project as a dependency.

Add SmartArt

Free Spire.Presentation for Java API provides a ShapeList.appendSmartArt method for adding SmartArt to a PowerPoint slide. The following example shows how to add a SmartArt of a specified layout type to a PowerPoint document.

import com.spire.presentation.FileFormat;
import com.spire.presentation.ISlide;
import com.spire.presentation.Presentation;
import com.spire.presentation.diagrams.ISmartArt;
import com.spire.presentation.diagrams.SmartArtColorType;
import com.spire.presentation.diagrams.SmartArtLayoutType;
import com.spire.presentation.diagrams.SmartArtStyleType;

public class AddSmartArt {
    public static void main(String[] args) throws Exception {
        //Create a Presentation instance
        Presentation ppt = new Presentation();
        //Get the first slide
        ISlide slide = ppt.getSlides().get(0);

        //Add a SmartArt of a specified layout type to the slide
        ISmartArt smartArt = slide.getShapes().appendSmartArt(100,100, 400, 400, SmartArtLayoutType.BASIC_BLOCK_LIST);
        //Set the shape style of SmartArt
        smartArt.setStyle(SmartArtStyleType.CARTOON);
        //Set the color type of SmartArt
        smartArt.setColorStyle(SmartArtColorType.COLORFUL_ACCENT_COLORS);

        //Set text for the nodes in the SmartArt
        smartArt.getNodes().get(0).getTextFrame().setText("My SmartArt_1");
        smartArt.getNodes().get(1).getTextFrame().setText("My SmartArt_2");
        smartArt.getNodes().get(2).getTextFrame().setText("My SmartArt_3");
        smartArt.getNodes().get(3).getTextFrame().setText("My SmartArt_4");
        smartArt.getNodes().get(4).getTextFrame().setText("My SmartArt_5");

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

Alt Text

Manipulate SmartArt

You can access an existing SmartArt and perform a series of manipulations on it, for instance, add/delete nodes, edit/extract text and change color style/shape style etc. Note that you can’t change the LayoutType of the SmartArt as it is read only and is set only when the SmartArt shape is added.

The following example shows how to delete a node from an existing SmartArt by using ISmartArtNodeCollection.removeNode method.

import com.spire.presentation.FileFormat;
import com.spire.presentation.IShape;
import com.spire.presentation.ISlide;
import com.spire.presentation.Presentation;
import com.spire.presentation.diagrams.ISmartArt;

public class ManipulateSmartArt {
    public static void main(String[] args) throws Exception {
        //Create a Presentation instance
        Presentation ppt = new Presentation();
        //Load a PowerPoint document
        ppt.loadFromFile("AddSmartArt.pptx");

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

        //Loop through the shapes on the slide
        for (Object shapeObj: slide.getShapes()) {
            IShape shape = (IShape)shapeObj;
            //Detect if the shape is SmartArt
            if(shape instanceof ISmartArt){
                ISmartArt smartArt = (ISmartArt)shape;
                //Remove the fifth node from the SmartArt
                smartArt.getNodes().removeNode(4);
                //do some other manipulations...
            }
        }

        //Save the result document
        ppt.saveToFile("ManipulateSmartArt.pptx", FileFormat.PPTX_2013);
    }
}

Alt Text

Delete SmartArt

The following example shows how to remove all the SmartArt shapes on a PowerPoint slide by using ShapeList.remove method.

import com.spire.presentation.FileFormat;
import com.spire.presentation.IShape;
import com.spire.presentation.ISlide;
import com.spire.presentation.Presentation;
import com.spire.presentation.diagrams.ISmartArt;

public class DeleteSmartArt {
    public static void main(String[] args) throws Exception {
        //Create a Presentation instance
        Presentation ppt = new Presentation();
        //Load a PowerPoint document
        ppt.loadFromFile("AddSmartArt.pptx");

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

        //Loop through the shapes on the slide
        for(int i = slide.getShapes().getCount()-1; i>=0; i--){
            IShape shape = slide.getShapes().get(i);
            //Detect if the shape is SmartArt
            if(shape instanceof  ISmartArt){
            //Remove the SmartArt
            slide.getShapes().remove(shape);
            }
        }

        //Save the result document
        ppt.saveToFile("DeleteSmartArt.pptx", FileFormat.PPTX_2013);
    }
}

Alt Text

Top comments (0)