Copy PowerPoint Slide with Java REST API

Follow this article to copy PowerPoint slide with Java REST API. You will learn how to duplicate slides in PowerPoint with Java REST Interface using the Java-based Cloud SDK. It will share details to duplicate slides in the same presentation or copy the slide to another presentation.

Prerequisite

Steps to Copy Slide with Java Low Code API

  1. Create the SlidesApi object with a user client ID and secret for copying a slide
  2. Upload the source presentation file into the Cloud storage for slide duplication
  3. Copy a slide to the destination index by invoking the CopySlide method
  4. Display the URLs of all the slides in the response object if required
  5. Download the updated presentation after copying a slide and save on the disk

These steps summarize how to copy a PowerPoint slide with Java REST API. Upload the presentation to Cloud storage and call the CopySlide() method by setting the uploaded file name, the source slide index, and the destination index for the copied slide. Display the returned slide URLs and download the updated presentation if required.

Code to Duplicate PowerPoint Slide 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_CopyPresentationSlides {
protected static SlidesApi presentationApi;
public Example_CopyPresentationSlides() {
if (presentationApi == null) {
presentationApi = new SlidesApi("appSid", "appKey");
}
}
public void copySlide() throws ApiException, IOException {
String localPath = "/home/downloads/";
String fileName = "Sample.pptx";
String storageFolderName = "TempTests";
presentationApi.uploadFile(storageFolderName+"/"+fileName, readFileToByteArray(localPath + fileName),null);
// Copy a presentation slide
Slides response = presentationApi.copySlide(fileName,1, 2,fileName, null, null, 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 new slide added 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 copy slides from one PPT to another with Java Low Code API. You may call the other overloaded method CopySlide() by setting the uploaded file name, source slide index, destination slide index, and name of the destination presentation if it is different from the source presentation. Note that you need to upload the destination presentation also if you want to copy slides to a different presentation.

This article has taught us how to copy PowerPoint slide to another presentation with Java REST API. If you want to add an empty slide to a presentation, refer to the article on add a new slide in PowerPoint with Java REST API.

 English