Create a Presentation using a Presentation Design Template with Java REST API

This article guides on how to create a presentation using a presentation design template with Java REST API. You will learn to create a PowerPoint presentation using slide template with Java RESTful Service using a Java-based SDK. It will also describe the creation of the XML script for filling the templates.

Prerequisite

Steps for Creating Presentation using Templates with Java REST Interface

  1. Set the environment by creating a SlidesApi object with Client ID and secret key
  2. Define the input template file name and output presentation name
  3. Upload the template to the Cloud storage using the UploadFile() method
  4. Create or load the XML file for filling the template
  5. Call the CreatePresentationFromTemplate() method to use the XML data and generate a presentation
  6. Download the newly created presentation file from the Could storage

These steps summarize the process of creating a presentation using a template with Java-based API. Upload the template file to the Cloud storage, create or load the XML script for the template, and call the CreatePresentationFromTemplate() by providing the output presentation name, uploaded template file, and XML data. Finally, save the output file by downloading the newly created presentation from the Cloud storage.

Code for Creating Presentation using Template with Java Low Code API

import com.aspose.slides.ApiException;
import com.aspose.slides.api.SlidesApi;
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_CreatePresentationFromDesignTemplate {
protected static SlidesApi presentationApi;
public Example_CreatePresentationFromDesignTemplate() {
if (presentationApi == null) {
presentationApi = new SlidesApi("appSid", "appKey");
}
}
public void CreatePresentationFromDesignTemplate() throws ApiException, IOException {
String localPath = "/home/downloads/";
String templateFileName = "TemplatePres.pptx";
String outputFileName = "GeneratedPres.pptx";
String storageFolderName = "TempTests";
String inputData = """
<staff><person>
<staffName>Alice Smith</staffName>
<address><line1>25 Maple Avenue</line1><line2>New York</line2></address>
<phone>+789 654321</phone>
<bio>Hello, I'm Alice and this is my resume</bio>
<domains>
<domain><experience>Python</experience><grade>Expert</grade></domain>
<domain><experience>JavaScript</experience><grade>Intermediate</grade></domain>
<domain><experience>Ruby</experience><grade>Beginner</grade></domain>
</domains>
</person></staff>
""";
var response = presentationApi.createPresentationFromTemplate(
outputFileName, templateFileName, inputData, null, null, false,
null, storageFolderName, null);
File presentationFile = presentationApi.downloadFile(storageFolderName+"/"+outputFileName, null, null);
// Copy the downloaded presentation with inserted HTML text to the local directory
copyFile(presentationFile, new File(localPath, outputFileName));
System.out.println("Presentation created from Design Template is copied to: " + localPath + outputFileName);
}
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 has demonstrated how to create a presentation using design template with Java REST API. If the template is password protected, set the password in the function call. Moreover, you can also set the password for the output presentation file if required.

This article has taught us to create a presentation from a template. To display document properties, refer to the article Display document properties in PowerPoint with Java REST API.

 English