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. In this article, you will learn how to create a SmartArt in PowerPoint and custom its layout programmatically using Spire.Presentation for Java.
Install Spire.Presentation for Java
First of all, you're required to add the Spire.Presentation.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.
<repositories> <repository> <id>com.e-iceblue</id> <name>e-iceblue</name> <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url> </repository> </repositories> <dependencies> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.presentation</artifactId> <version>9.8.3</version> </dependency> </dependencies>
Create a SmartArt in PowerPoint
The detailed steps are as follows.
- Create a Presentation object.
- Get a specified slide using Presentation.getSlides().get() method.
- Insert a SmartArt into the specified slide using ISlide.getShapes().appendSmartArt() method.
- Set the style and color of the SmartArt using ISmartArt.setStyle() method and ISmartArt.setColorStyle() method.
- Loop through the nodes in SmartArt and remove all default nodes using ISmartArt.getNodes().removeNode() method.
- Add a parent node to the SmartArt using ISmartArt.getNodes().addNode() method, and then add 4 child nodes using ISmartArtNode.getChildNodes().addNode() method.
- Add text to each node using ISmartArtNode.getTextFrame().setText() method.
- Get the text range of the above added text using ISmartArtNode.getTextFrame().getTextRange() method, and then set the text font size using PortionEx.setFontHeight() method.
- Save the document to file using Presentation.saveToFile() method.
- Java
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 Presentation object. Presentation presentation = new Presentation(); //Get the first slide ISlide slide = presentation.getSlides().get(0); //Insert a SmartArt (Organization Chart) into the slide ISmartArt smartArt = slide.getShapes().appendSmartArt(60, 60, 500, 400, SmartArtLayoutType.ORGANIZATION_CHART); //Set the style and color of SmartArt smartArt.setStyle(SmartArtStyleType.MODERATE_EFFECT); smartArt.setColorStyle(SmartArtColorType.COLORFUL_ACCENT_COLORS_4_TO_5); //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 its 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(); } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.