Split Slides with Java REST API

Follow this article to split slides with Java REST API. You will learn to automatically split PPTX with Java based API using a Java-based Cloud SDK. It shares all the properties required for the customization of the conversion process.

Prerequisite

Steps to Split PPT with Java Low Code API

  1. Create an object of the SlidesApi class by setting the client ID and secret
  2. Upload the source presentation into the Cloud storage by setting a unique name
  3. Call the Split() method using the uploaded file name, output image format, starting slide, and last slide number
  4. Parse all the slides in the response object
  5. Download each slide image using the image name in the Href string
  6. Save the downloaded image on the disk

These steps explain how to develop a PowerPoint splitter tool with Java REST Interface. Create a SlidesApi object, upload the source presentation file, and call the Split() method by providing the required information. Parse the API response and save all the images created against individual slides on the local disk.

Code for PowerPoint Splitter with Java RESTful Service

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_SplitPresentationSlides {
protected static SlidesApi presentationApi;
public Example_SplitPresentationSlides() {
if (presentationApi == null) {
presentationApi = new SlidesApi("appSid", "appKey");
}
}
public void SplitPresentation() throws ApiException, IOException {
String localPath = "/home/downloads/";
String fileName = "TestPresentation.pptx";
String storageFolderName = "TempTests";
presentationApi.uploadFile(storageFolderName+"/"+fileName, readFileToByteArray(storageFolderName+localPath + fileName),null);
ExportOptions options = new ExportOptions();
// Split the 2nd and 3rd slides and save them to PNG format.
SplitDocumentResult response = presentationApi.split(fileName, options, SlideExportFormat.PNG,720, 540,2, 3,
storageFolderName, null, storageFolderName, null, null);
for (ResourceUri slide : response.getSlides())
{
System.out.println(slide.getHref());
String imageName = slide.getHref().substring(slide.getHref().lastIndexOf('/') + 1);
File imageFile = presentationApi.downloadFile(storageFolderName + imageName, null, null);
// Copy the downloaded presentation to the local directory
copyFile(imageFile, new File(localPath, imageName));
}
System.out.println("Presentation splitted to image copied to: " + localPath);
}
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 develop a PowerPoint splitter online with Java REST Interface. The Split() method requires the uploaded file name, the output image format using the SlideExportFormat enumerator, from index and to index to define the range of slides. The Href of each item in the slides collection contains the reference to the online image created by the API and can be used for downloading the images.

This article has taught us developing a PPTX splitter with Java RESTful Service. If you want to merge presentations, refer to the article on Merge presentations with Java REST API.

 English