Add or Remove Comments in PowerPoint PPT in Java

In PowerPoint presentations, the comments are used to write the feedback about the content in the slides. While manipulating PowerPoint PPT/PPTX presentations, you may need to add comments programmatically. In this article, you will learn how to add comments to PowerPoint PPT slides in Java. Moreover, we will cover how to read or remove slide comments and add their replies.

Java API to Work with Comments in PowerPoint

Aspose.Slides for Java is a popular presentation manipulation API that lets you create and modify PowerPoint PPT/PPTX files. We will use this API to manipulate comments in PowerPoint presentations. You can either download API’s JAR or install it using the following Maven configurations.

Repository:

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java API</name>
    <url>https://repository.aspose.com/repo/</url>
</repository>

Dependency:

<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-slides</artifactId>
    <version>22.2</version>
    <classifier>jdk16</classifier>
</dependency>

Add Comments to PowerPoint PPT Slides in Java

In PowerPoint presentations, every comment is attached to a particular author. Whereas, each comment contains some additional information such as time of creation, slide where it is added, and its position. The following are the steps to add comments to a PPT slide in Java.

The following code sample shows how to add comments to PPT slides in Java.

// Create or load presentation
Presentation presentation = new Presentation("presentation.pptx");
try {
// Add an empty slide or get reference of an existing slide
presentation.getSlides().addEmptySlide(presentation.getLayoutSlides().get_Item(0));
// Add an author
ICommentAuthor author = presentation.getCommentAuthors().addAuthor("Usman", "UA");
// Set the position for comments
Point2D.Float point = new Point2D.Float(0.2f, 0.2f);
// Add slide comment on first slide
author.getComments().addComment("Hello, this is slide comment", presentation.getSlides().get_Item(0), point, new Date());
// Save presentation
presentation.save("add-comment.pptx", SaveFormat.Pptx);
} finally {
if (presentation != null)
presentation.dispose();
}

The following is the screenshot of the comment we have added using the above code sample.

Insert Comments to PPT Slides in Java

Add Comment Replies in PPT Slides in Java

Aspose.Slides also allows you to add replies to the comments. A reply itself is a comment which appears as a child of an existing comment. So let’s see how to add replies to comments in PowerPoint PPT slides in Java.

The following code sample shows how to add replies to comments in a PPTX presentation in Java.

// Create or load presentation
Presentation presentation = new Presentation("presentation.pptx");
try {
// Add an empty slide or get reference of an existing slide
presentation.getSlides().addEmptySlide(presentation.getLayoutSlides().get_Item(0));
// Add an author
ICommentAuthor author = presentation.getCommentAuthors().addAuthor("Usman", "UA");
// Set the position for comments
Point2D.Float point = new Point2D.Float(0.2f, 0.2f);
// Add slide comment on first slide
IComment comment = author.getComments().addComment("Hello, this is slide comment", presentation.getSlides().get_Item(0), point, new Date());
// Add reply comment
IComment subReply = author.getComments().addComment("This is the reply to the comment.", presentation.getSlides().get_Item(0), new Point2D.Float(10, 10), new Date());
subReply.setParentComment(comment);
// Add reply comment
IComment reply2 = author.getComments().addComment("This is second reply.", presentation.getSlides().get_Item(0), new Point2D.Float(10, 10), new Date());
reply2.setParentComment(comment);
// Save presentation
presentation.save("add-comment-reply.pptx", SaveFormat.Pptx);
} finally {
if (presentation != null)
presentation.dispose();
}

The following screenshot shows the output of the above code sample.

Add reply to the comments in PPT in Java

Read Comments in PPT Slides in Java

Using Aspose.Slides, you can also read comments of a particular author or all the authors. The following are the steps to read comments in PPT slides in Java.

The following code sample shows how to read comments in PPT slides in Java.

// Load presentation
Presentation presentation = new Presentation("add-comment.pptx");
try {
// Loop through authors
for (ICommentAuthor commentAuthor : presentation.getCommentAuthors())
{
// Access each author
CommentAuthor author = (CommentAuthor) commentAuthor;
// Loop through author's comments
for (IComment comment1 : author.getComments())
{
// Read comment
Comment comment = (Comment) comment1;
System.out.println("ISlide :" + comment.getSlide().getSlideNumber() + " has comment: " + comment.getText() +
" with Author: " + comment.getAuthor().getName() + " posted on time :" + comment.getCreatedTime() + "\n");
}
}
} finally {
if (presentation != null)
presentation.dispose();
}

Remove Comments from PowerPoint PPT in Java

In the previous section, you have seen how to read a comment by accessing it from the comments collection. Similarly, you can remove a comment after getting its reference. The following code sample shows how to remove comments in PowerPoint presentations in Java.

// Load presentation
Presentation presentation = new Presentation("add-comment.pptx");
try {
// Get first slide
ISlide slide = presentation.getSlides().get_Item(0);
// Get comments
IComment[] comments = slide.getSlideComments(null);
// Remove desired comment using index
comments[0].remove();
// Save presentation
presentation.save("remove-comments.pptx", SaveFormat.Pptx);
} finally {
if (presentation != null)
presentation.dispose();
}

Get a Free License

You can use Aspose.Slides for Java without evaluation limitations by requesting a temporary license.

Conclusion

In this article, you have learned how to add comments in PowerPoint PPT slides in Java. Moreover, we have covered how to add replies to the comments programmatically. In the end, we have demonstrated how to read or remove comments from PPT slides. You can visit the documentation to explore more about Aspose.Slides for Java. Also, you can post your queries to our forum.

See Also