This article describes how to add SmartArt to PowerPoint with Java REST API. You will learn to automatically insert PowerPoint SmartArt with Java Low Code API using the Java-based Cloud SDK. Various classes and enumerators are discussed to create different types of SmartArt in the PowerPoint presentation slide.
Prerequisite
Download Aspose.Slides Cloud SDK for Dotnet for inserting SmartArt in slides
Setup Java project with the above SDK to create a SmartArt graphics
Steps to Add PowerPoint Presentation SmartArt with Java REST API
- Set the credentials in the SlidesApi object for working with the SmartArt
- Upload the source presentation to the Cloud storage for inserting smart graphics
- Create graphics data by setting desired properties in the SmartArt object
- Insert the SmartArt using the CreateShape() method
- Download the updated presentation file after adding SmartArt to it
These steps explain how to work with SmartArt for PowerPoint with Java REST API. Create the SlidesApi class object, upload the source presentation, and create the SmartArt object with the specified settings including position, size, layout, quick style, and color style. Finally, multiple nodes for the SmartArt are added to the respective slide using the CreateShape() method.
Code to Add PowerPoint Smart Shapes with Java Java-Based API
import com.aspose.slides.ApiException; | |
import com.aspose.slides.api.SlidesApi; | |
import com.aspose.slides.model.*; | |
import java.io.File; | |
import java.io.IOException; | |
import java.nio.file.Files; | |
import java.nio.file.Path; | |
import java.nio.file.StandardCopyOption; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class Example_AddSmartArtShapeInPresentation { | |
protected static SlidesApi presentationApi; | |
public Example_AddSmartArtShapeInPresentation() { | |
if (presentationApi == null) { | |
presentationApi = new SlidesApi("appSid", "appKey"); | |
} | |
} | |
public void addCustomShapeInSlide() throws ApiException, IOException { | |
String localPath = "/home/downloads/"; | |
String fileName = "Sample.pptx"; | |
String imageFileName = "ShapeImage.png"; | |
String storageFolderName = "TempTests"; | |
presentationApi.uploadFile(storageFolderName+"/"+fileName, readFileToByteArray(localPath + fileName),null); | |
// Configure SmartArt properties | |
SmartArt smartArt = new SmartArt(); | |
smartArt.setX(50.0); // Horizontal position of the SmartArt | |
smartArt.setY(50.0); // Horizontal position of the SmartArt | |
smartArt.setWidth(250.0); // Width of the SmartArt | |
smartArt.setHeight(250.0); // Height of the SmartArt | |
smartArt.setLayout(SmartArt.LayoutEnum.BENDINGPICTURESEMITRANSPARENTTEXT); // SmartArt layout | |
smartArt.setQuickStyle(SmartArt.QuickStyleEnum.SIMPLEFILL); // Quick style | |
smartArt.setColorStyle(SmartArt.ColorStyleEnum.COLOREDFILLACCENT1); // Color style | |
List<SmartArtNode> nodes = new ArrayList<SmartArtNode>(); | |
SmartArtNode node1 = new SmartArtNode(); | |
node1.setText("Planning"); | |
nodes.add(node1); | |
SmartArtNode node2 = new SmartArtNode(); | |
node2.setText("Design"); | |
nodes.add(node2); | |
SmartArtNode node3 = new SmartArtNode(); | |
node3.setText("Implementation"); | |
nodes.add(node3); | |
smartArt.setNodes(nodes); | |
// Add SmartArt to the first slide | |
ShapeBase addedSmartArt = presentationApi.createShape(fileName, 1, smartArt, null, null, null, storageFolderName, null, null ); | |
System.out.println("SmartArt added to the presentation successfully."); | |
File presentationFile = presentationApi.downloadFile(storageFolderName+"/"+fileName, null, null); | |
// Copy the downloaded presentation with new image shape to the local directory | |
copyFile(presentationFile, new File(localPath, fileName)); | |
System.out.println("Presentation slide with SmartArt shape is copied to: " + localPath + fileName); | |
} | |
public static byte[] readFileToByteArray(String filePath) throws IOException { | |
Path path = new File(filePath).toPath(); | |
return Files.readAllBytes(path); | |
} | |
private void copyFile(File sourceFile, File targetFile) throws IOException { | |
if (sourceFile == null || !sourceFile.exists()) { | |
throw new IOException("Source file does not exist: " + sourceFile); | |
} | |
// Ensure the target directory exists | |
Path targetPath = targetFile.toPath(); | |
Files.createDirectories(targetPath.getParent()); | |
// Copy the file | |
Files.copy(sourceFile.toPath(), targetPath, StandardCopyOption.REPLACE_EXISTING); | |
} | |
} |
This code demonstrates how to insert a Smart Art graphic with Java REST Interface in a slide. Use the LayoutEnum to select the desired SmartArt shape from a large list of values including AccentProcess, AccentedPicture, ArrowRibbon, BasicPyramid, and BasicProcess, etc. Similarly, the quick style and color style enumerators have also a variety of options to customize the SmartArt.
This article has taught us how to create the SmartArt in a presentation slide. For adding custom shapes in a presentation, refer to the article on Create Custom Shapes in PowerPoint with Java REST API.