Create Custom Shapes in PowerPoint with Java REST API

Follow this article to create custom shapes in PowerPoint with Java REST API. You will learn to automatically create and add custom shapes for PowerPoint with Java RESTful Service. It shares all the details to create a shape, set its parameters, and add to a particular slide.

Prerequisite

Steps to Add PPT Shape with Java REST API

  1. Create the SlidesApi object and upload the source presentation for adding shapes to it
  2. Specify that the slide type to modify is a Master Slide
  3. Create a new shape object with specific properties
  4. Add the new shape to the specified slide using the CreateSpecialSlideShape() method
  5. Download the updated presentation file with the new shape in it

These steps explain how to generate presentation shapes with Java REST API. Create the SlidesApi object, upload the presentation to the Cloud storage, specify the type of slide to be modified, and instantiate the Shape object using the desired parameters. Add the shape to the master slide and download the updated presentation with the new shape in it.

Code to Add Shape for PPT with Java REST Interface

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;
public class Example_CreateCustomShapeInPresentation {
protected static SlidesApi presentationApi;
public Example_CreateCustomShapeInPresentation() {
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);
// Specify that the slide type to modify is a Master Slide
SpecialSlideType slideType = SpecialSlideType.MASTERSLIDE;
// Create a new shape object with specific properties
Shape dto = new Shape();
dto.setX(100.0);
dto.setY(100.0);
dto.setWidth(500.0);
dto.setHeight(200.0);
dto.setShapeType(GeometryShape.ShapeTypeEnum.RECTANGLE);
dto.setText("New shape");
// Add the new shape to the specified slide (master slide at index 1) and retrieve the created shape
Shape shape = (Shape)presentationApi.createSpecialSlideShape(fileName, 1, slideType, dto, null, null,
null,storageFolderName, null, null );
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 image 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 handle shapes for slides with Java RESTful Service. You may add any type of shapes using the enumerator GeometryShape.ShapeTypeEnum including Lines, Triangle, Rectangle, and Diamond etc. The option is also available to select other special types of slides such as LayoutSlide and NotesSlide.

This article has taught us to draw shapes on a slide. To add pictures, refer to the article Add Picture to PowerPoint with Java REST API.

 Magyar