Add, Manipulate and Delete SmartArt in PowerPoint in Java
Tags: java, powerpoint, smartart
Published: 2020-09-17 09:20:36

---
title: Add, Manipulate and Delete SmartArt in PowerPoint in Java
published: true
description: This article shows how to add, manipulate and delete SmartArt in a PowerPoint document in Java.
tags: java, powerpoint, smartart
//cover_image: https://direct_url_to_image.jpg
---

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](https://www.e-iceblue.com/Introduce/free-presentation-for-java.html) 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.
```java
<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](https://www.e-iceblue.com/Download/presentation-for-java-free.html), 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.
```java
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](https://dev-to-uploads.s3.amazonaws.com/i/f6tx1daqovftgbyuzuzx.png)
##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.
```java
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](https://dev-to-uploads.s3.amazonaws.com/i/mi6r7ym9l76u5hie3y8k.png)
##Delete SmartArt
The following example shows how to remove all the SmartArt shapes on a PowerPoint slide by using **ShapeList.remove** method.
```java
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](https://dev-to-uploads.s3.amazonaws.com/i/1rgepz5nptp6x79dg0b2.png)