Wednesday, 14 October 2020

[JAVA] Add, Replace and Delete Comments in PowerPoint

Comments are a great tool for collaborating in PowerPoint. When you're preparing for a big presentation, using comments and leaving them for your colleagues to trade feedback is a great option. In this article, I’ll introduce a quick way to add comments in Java programmatically, and demonstrate how to replace or delete comments in PowerPoint.

Add Spire.Presentation.jar as a dependency

Before typing codes, you need to add Spire.Presentation.jar to your project as a dependency. There are two ways to do it. One is that download Free Spire.Presentation for Java package from the link, unzip it and manually add Spire.Presentation.jar in the “lib” folder to your project. The other is that create a Maven project, write down the following code in the pom.xml file, and click the button “Import Changes”.

<repositories>

        <repository>

            <id>com.e-iceblue</id>

            <name>e-iceblue</name>

            <url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>

        </repository>

</repositories>

<dependencies>

    <dependency>

        <groupId>e-iceblue</groupId>

        <artifactId>spire.presentation.free</artifactId>

        <version>3.9.0</version>

    </dependency>

</dependencies>

Using the code

Add Comments to PowerPoint
Free Spire.Presentation for Java allows users to insert comments in PowerPoint slides with several lines 
of core code. The following are four steps to do it.
Step 1: Create a Presentation instance and load a sample file.
Step 2: Call the ICommentAuthor.addAuthor () method to add comment author.

Step 3: Call the ppt.getSlides().addComment() method to add a comment on a specific slide. The 
Comment class includes author who added slide comment, time of creation, the position of comment on 
slide and the comment text.
import com.spire.presentation.*;
import java.awt.geom.Point2D;
public class AddComment {     public static void main(String[] args) throws Exception {
       
//load a sample PowerPoint file
       
Presentation ppt = new Presentation();
        ppt.loadFromFile(
"C:\\Users\\Test1\\Desktop\\Sample.pptx");
       
//Name the comment author
       
ICommentAuthor author = ppt.getCommentAuthors().addAuthor("Tina", "comment:");
       
//Add comments and set their location
       
ppt.getSlides().get(0).addComment(author, "The first comment", new Point2D.Float(21, 13), new java.util.Date());
        ppt.getSlides().get(
0).addComment(author, "The second added comment", new Point2D.Float(24, 28), new java.util.Date());
       
//Save the resulting document
       
ppt.saveToFile("output/AddComment.pptx", FileFormat.PPTX_2010);
        ppt.dispose();
    }
}

Output


Replace and Delete Comments in PowerPoint

import com.spire.presentation.FileFormat;
import com.spire.presentation.Presentation;
public class ReplaceAndDeleteComment {
   
public static void main(String[] args) throws Exception {
       
//load the sample PowerPoint file
       
Presentation ppt = new Presentation();
        ppt.loadFromFile(
"C:\\Users\\Test1\\Desktop\\AddComment.pptx");
       
//Replace the first comment
       
ppt.getSlides().get(0).getComments()[0].setText("Replace comment");
       
//Delete the second comment
       
ppt.getSlides().get(0).deleteComment(ppt.getSlides().get(0).getComments()[1]);
       
//Save the resulting document
       
ppt.saveToFile("output/ReplaceAndDeleteComment.pptx", FileFormat.PPTX_2010);
        ppt.dispose();
    }
}
Output

Conclusion

Free Spire.Presentation for Java is especially designed for developers to manipulate PowerPoint

documents in Java without installing Microsoft Office. In addition to the above function of adding, replacing and deleting comments, it supports large numbers of other features.You can know more

detailed information from the link and feel free to take a note on Forum if there is any problem.

No comments:

Post a Comment

Change PDF Versions in Java

In daily work, you might need to change the version of a PDF document you have in order to ensure compatibility with another version which a...