Add Comments to PowerPoint with Java REST API

Follow this article to add comments to PowerPoint with Java REST API. You will learn to automatically add PowerPoint comments with Java Low Code API using the Java-based Cloud SDK. It will share details to set various parameters of a comment before adding it to the slide.

Prerequisite

Steps to Comments on PowerPoint Presentation with Java based API

  1. Create the SlidesApi object by setting the client ID and secret for adding comments
  2. Set the input presentation file name and the target slide index
  3. Define the slide comment and the relevant child comments collection
  4. Add the comments to the slide using the CreateComment method
  5. Fetch the comments count to confirm the addition of the comments
  6. Download the updated presentation file with new comments in it

These steps describe how to add comments in PowerPoint with Java RESTful Service. Set the input presentation file name and slide index, create the slide comment and sub-comments, and call the CreateComment() method to insert the comments by setting the input file name, target index, and comments. This call uploads the presentation, modifies it in the Cloud, and returns the collection of comments.

Code to Add PowerPoint Presentation Comments with Java REST API

import com.aspose.slides.ApiException;
import com.aspose.slides.api.SlidesApi;
import com.aspose.slides.model.SlideComment;
import com.aspose.slides.model.SlideCommentBase;
import com.aspose.slides.model.SlideComments;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
public class Example_AddCommentsInPresentation {
protected static SlidesApi presentationApi;
public Example_AddCommentsInPresentation() {
if (presentationApi == null) {
presentationApi = new SlidesApi("appSid", "appKey");
}
}
public void addComments() throws ApiException, IOException {
String localPath = "/home/downloads/";
String fileName = "Sample.pptx";
String storageFolderName = "TempTests";
presentationApi.uploadFile(storageFolderName+"/"+fileName, readFileToByteArray(localPath + fileName),null);
SlideComment comment = new SlideComment ();
comment.setText("Master comment here.");
comment.setAuthor("Mr. John");
SlideComment subComment = new SlideComment ();
subComment.setText("Here is the sub-comment.");
subComment.setAuthor("Mr. Paul");
ArrayList<SlideCommentBase> subComments = new ArrayList<SlideCommentBase>();
subComments.add(subComment);
comment.childComments(subComments);
// Add slide comments
SlideComments comments = presentationApi.createComment(fileName, 2, comment, null, null, storageFolderName, null);
File presentationFile = presentationApi.downloadFile(storageFolderName+"/"+fileName, null, null);
// Copy the downloaded presentation with new comments to the local directory
copyFile(presentationFile, new File(localPath, fileName));
System.out.println("Presentation slide comment is set 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 has demonstrated how to add comments to PowerPoint presentation with Java RESTful Service. You can add multiple child comments under the main comment by adding a list of comments and setting it to the property ChildComments. You can customize the comments by setting other exposed properties using the SlideComment method.

This article has taught us to add comments to a slide. You can insert an image background to a presentation by following the article Use picture as background in PowerPoint with Java REST API.

 English