How to Add Comments to PowerPoint in Java

How to Add Comments to PowerPoint in Java

This guide shows how to add, read, and remove comments in PowerPoint presentations using Aspose.Slides FOSS for Java. Call author.getComments().addComment() with a slide reference and a PointF position to attach a comment. Comments are visible in PowerPoint’s Review pane.


Prerequisites

Add the following dependency to your pom.xml to include the Aspose.Slides FOSS library:

<dependency>
  <groupId>org.aspose.slides.foss</groupId>
  <artifactId>aspose-slides-foss</artifactId>
  <version>1.0.0</version>
</dependency>

Add a Comment

Comments belong to an author object. Create an author first, then add comments through author.getComments():

import org.aspose.slides.foss.Presentation;
import org.aspose.slides.foss.ISlide;
import org.aspose.slides.foss.ICommentAuthor;
import org.aspose.slides.foss.ICommentCollection;
import org.aspose.slides.foss.IComment;
import org.aspose.slides.foss.export.SaveFormat;
import org.aspose.slides.foss.drawing.PointF;
import java.time.LocalDateTime;

try (Presentation prs = new Presentation()) {
    // Create a comment author with name and initials
    ICommentAuthorCollection authors = prs.getCommentAuthors();
    ICommentAuthor author = authors.addAuthor("Jane Smith", "JS");

    ISlideCollection slides = prs.getSlides();
    ISlide slide = slides.get(0);

    // Add a comment at position (2.0, 2.0) from the slide top-left corner
    author.getComments().addComment(
        "Please review the figures on this slide",
        slide,
        new PointF(2.0f, 2.0f),
        LocalDateTime.now()
    );

    prs.save("commented.pptx", SaveFormat.PPTX);
}

The PointF coordinates specify the comment anchor position. Multiple calls to addComment() create a threaded comment chain under the same author.


Multiple Authors and Comments

Multiple authors can each add their own comments to the same slide. Create separate ICommentAuthor instances and call addComment() on each:

import org.aspose.slides.foss.Presentation;
import org.aspose.slides.foss.ISlide;
import org.aspose.slides.foss.ICommentAuthor;
import org.aspose.slides.foss.ICommentCollection;
import org.aspose.slides.foss.IComment;
import org.aspose.slides.foss.export.SaveFormat;
import org.aspose.slides.foss.drawing.PointF;
import java.time.LocalDateTime;

try (Presentation prs = new Presentation()) {
    ICommentAuthorCollection authors = prs.getCommentAuthors();
    ICommentAuthor author1 = authors.addAuthor("Alice Brown", "AB");
    ICommentAuthor author2 = authors.addAuthor("Bob Davis", "BD");

    ISlideCollection slides = prs.getSlides();
    ISlide slide = slides.get(0);

    author1.getComments().addComment(
        "Initial draft: needs revision",
        slide, new PointF(1.0f, 1.0f), LocalDateTime.now()
    );
    author2.getComments().addComment(
        "Approved after changes",
        slide, new PointF(3.0f, 1.0f), LocalDateTime.now()
    );

    prs.save("multi-author.pptx", SaveFormat.PPTX);
}

Read Comments from an Existing File

Load an existing presentation and iterate over all comment authors and their comments using prs.getCommentAuthors():

import org.aspose.slides.foss.Presentation;
import org.aspose.slides.foss.ISlide;
import org.aspose.slides.foss.ICommentAuthor;
import org.aspose.slides.foss.ICommentCollection;
import org.aspose.slides.foss.IComment;
import org.aspose.slides.foss.export.SaveFormat;
import org.aspose.slides.foss.drawing.PointF;

try (Presentation prs = new Presentation("commented.pptx")) {
    ICommentAuthorCollection authors = prs.getCommentAuthors();
    for (int i = 0; i < authors.size(); i++) {
        ICommentAuthor author = authors.get(i);
        System.out.println("Author: " + author.getName()
            + " (" + author.getInitials() + ")");
        ICommentCollection comments = author.getComments();
        for (int j = 0; j < comments.size(); j++) {
            IComment comment = comments.get(j);
            System.out.println("  Slide " + comment.getSlide().getSlideNumber()
                + ": " + comment.getText());
        }
    }
}

Remove a Comment

Call author.getComments().removeAt(index) to delete a comment by its zero-based index within the author’s comment collection:

import org.aspose.slides.foss.Presentation;
import org.aspose.slides.foss.ISlide;
import org.aspose.slides.foss.ICommentAuthor;
import org.aspose.slides.foss.ICommentCollection;
import org.aspose.slides.foss.IComment;
import org.aspose.slides.foss.export.SaveFormat;
import org.aspose.slides.foss.drawing.PointF;

try (Presentation prs = new Presentation("commented.pptx")) {
    ICommentAuthorCollection authors = prs.getCommentAuthors();
    ICommentAuthor author = authors.get(0);
    if (author.getComments().size() > 0) {
        author.getComments().removeAt(0);
    }
    prs.save("cleaned.pptx", SaveFormat.PPTX);
}

See Also

 English