Java: Add, Change or Delete Comments in PowerPoint

When you are collaborating on a PowerPoint document with your team members, adding comments is the most effective way to give feedbacks or communicate the changes you need to make on a particular slide. Once a comment has been added, you may also need to edit or delete it later. This article will demonstrate how to programmatically add, change or delete comments in a PowerPoint Document using Spire.Presentation for Java.

Install Spire.Presentation for Java

First of all, you're required to add the Spire.Presentation.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.presentation</artifactId>
        <version>8.1.2</version>
    </dependency>
</dependencies>
    

Add a Comment to a Presentation Slide in Java

Spire.Presentation for Java offers the ISlide.addComment() method to add comments to a specified PowerPoint slide. The following are the steps to add a PowerPoint comment.

  • Initialize an instance of Presentation class.
  • Load a PowerPoint document using Presentation.loadFromFile() method.
  • Add the author of the comment using Presentation.getCommentAuthors().addAuthor() method.
  • Get a specified slide using Presentation.getSlides().get() method, and then add a comment to the slide using ISlide.addComment(ICommentAuthor author, java.lang.String text, java.awt.geom.Point2D position, java.util.Date dateTime) method.
  • Save the result document using Presentation.saveToFile() method.
  • Java
import com.spire.presentation.*;
import java.awt.geom.Point2D;

public class AddComment {
    public static void main(String[] args) throws Exception{
        //Initialize an instance of Presentation class
        Presentation ppt = new Presentation();

        //Load a sample PowerPoint document
        ppt.loadFromFile("C:\\Users\\Administrator\\Desktop\\Sample.pptx");

        //Add the author of the comment
        ICommentAuthor author = ppt.getCommentAuthors().addAuthor("E-iceblue", "Comment:");

        //Add a comment to the specified slide
        ppt.getSlides().get(0).addComment(author, "The first comment", new Point2D.Float(350, 170), new java.util.Date());

        //Save the result document
        ppt.saveToFile("AddComment.pptx", FileFormat.PPTX_2013);
        ppt.dispose();
    }
}

Java: Add, Change or Delete Comments in PowerPoint

Change a Comment in a Presentation Slide in Java

To update or modify the content of an existing comment, you can use the ISlide.getComments().setText() method. The following are the steps to change a PowerPoint comment.

  • Initialize an instance of Presentation class.
  • Load a PowerPoint document using Presentation.loadFromFile() method.
  • Get a specified slide using Presentation.getSlides().get() method.
  • Replace a specified comment in the slide using ISlide.getComments().setText() method.
  • Save the result document using Presentation.saveToFile() method.
  • Java
import com.spire.presentation.*;

public class ReplaceComment {
    public static void main(String[] args) throws Exception{
        //Initialize an instance of Presentation class
        Presentation ppt = new Presentation();

        //Load a sample PowerPoint document
        ppt.loadFromFile("AddComment.pptx");

        //Get the first slide
        ISlide slide = ppt.getSlides().get(0);

        //Replace a specified comment in the slide
        slide.getComments()[0].setText("Summary of Spire.Presentation for Java functions");

        //Save the result document
        ppt.saveToFile("ReplaceComment.pptx", FileFormat.PPTX_2013);
        ppt.dispose();
    }
}

Java: Add, Change or Delete Comments in PowerPoint

Delete a Comment from a Presentation Slide in Java

Spire.Presentation for Java also allows you to remove comments from a specified slide using ISlide.deleteComment() method. The detailed steps are as follows.

  • Initialize an instance of Presentation class.
  • Load a PowerPoint document using Presentation.loadFromFile() method.
  • Get a specified slide using Presentation.getSlides().get() method.
  • Remove a specified comment from the slide using ISlide.deleteComment(Comment comment) method.
  • Save the result document using Presentation.saveToFile() method.
  • Java
import com.spire.presentation.*;

public class DeleteComment {
    public static void main(String[] args) throws Exception{
        //Initialize an instance of Presentation class
        Presentation ppt = new Presentation();

        //Load a sample PowerPoint document
        ppt.loadFromFile("AddComment.pptx");

        //Get the first slide
        ISlide slide = ppt.getSlides().get(0);

        //Remove a specified comment from the slide
        slide.deleteComment(slide.getComments()[0]);

        //Save the result document
        ppt.saveToFile("DeleteComment.pptx", FileFormat.PPTX_2013);
        ppt.dispose();
    }
}

Java: Add, Change or Delete Comments in PowerPoint

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.