Delete PowerPoint Slide with Java REST API

Follow this article to delete PowerPoint slide with Java REST API. You will learn how to remove slide from PPT with Java Low Code API using a Java-based Cloud SDK. You can repeat the process to delete multiple slides from the presentation.

Prerequisite

Steps to Delete Slide with Java REST Interface

  1. Create the SlidesApi class object for deleting a slide from the presentation
  2. Upload the source presentation into the Cloud storage to remove a slide
  3. Call the DeleteSlide() method by providing the necessary parameters
  4. Display the remaining slide URLs from the API response object
  5. Download the updated presentation from the Cloud storage and save it on the disk

The above steps explain how to delete PowerPoint slide with Java based API. Create the SlidesApi class object with a user ID and secret, upload the presentation to the Cloud storage, and invoke the DeleteSlide() API call for deleting the slide. Display the remaining slides URLs from the API response and save the updated presentation on the disk.

Code to Delete Slides in PowerPoint with Java RESTful Service

import com.aspose.slides.ApiException;
import com.aspose.slides.api.SlidesApi;
import com.aspose.slides.model.ResourceUri;
import com.aspose.slides.model.Slides;
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_DeletePresentationSlides {
protected static SlidesApi presentationApi;
public Example_DeletePresentationSlides() {
if (presentationApi == null) {
presentationApi = new SlidesApi("appSid", "appKey");
}
}
public void deleteSlide() throws ApiException, IOException {
String localPath = "/home/downloads/";
String fileName = "Sample.pptx";
String storageFolderName = "TempTests";
presentationApi.uploadFile(storageFolderName+"/"+fileName, readFileToByteArray(localPath + fileName),null);
// Delete the target slide
Slides response = presentationApi.deleteSlide("Sample.pptx", 1, null, storageFolderName, null);
for (ResourceUri slide : response.getSlideList())
{
System.out.println(slide.getHref());
}
File presentationFile = presentationApi.downloadFile(storageFolderName+"/"+fileName, null, null);
// Copy the downloaded presentation with deleted slide to the local directory
copyFile(presentationFile, new File(localPath, fileName));
System.out.println("Presentation slide deleted and 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 remove slide from PPT with Java Low Code API. The DeleteSlide() method requires the uploaded presentation name and the slide index starting from 1 that you want to delete. The API response contains the list of remaining slides that are left in the uploaded presentation that you can display using the Href property.

This article has taught us to delete individual slides from a presentation. If you want to split slides from a presentation, refer to the article on Split Slides with Java REST API.

 English