
在 PowerPoint 演示文稿中,评论用于撰写有关幻灯片内容的反馈。在处理 PowerPoint PPT/PPTX 演示文稿时,您可能需要以编程方式添加注释。在本文中,您将学习如何在 Java 中为 PowerPoint PPT 幻灯片添加评论。此外,我们将介绍如何阅读或删除幻灯片评论并添加他们的回复。
用于处理 PowerPoint 中的注释的 Java API
Aspose.Slides for Java 是一种流行的演示文稿操作 API,可让您创建和修改 PowerPoint PPT/PPTX 文件。我们将使用此 API 来处理 PowerPoint 演示文稿中的评论。您可以 下载 API 的 JAR 或使用以下 Maven 配置安装它。
存储库:
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>https://repository.aspose.com/repo/</url>
</repository>
依赖:
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-slides</artifactId>
<version>22.2</version>
<classifier>jdk16</classifier>
</dependency>
在 Java 中为 PowerPoint PPT 幻灯片添加评论
在 PowerPoint 演示文稿中,每条评论都附加到特定的作者。然而,每条评论都包含一些附加信息,例如创建时间、添加它的幻灯片及其位置。以下是在 Java 中为 PPT 幻灯片添加注释的步骤。
- 首先,使用 Presentation 类加载演示文件或创建一个新文件。
- 使用 Presentation.getCommentAuthors().addAuthor(String, String) 方法添加新作者。
- 获取对象中新创建作者的引用。
- 定义评论的位置。
- 使用 ICommentAuthor.getComments().addComment(String, ISlide, Point2D.Float, Date) 方法添加评论。
- 最后,使用 Presentation.save(String, SaveFormat) 方法保存演示文稿。
以下代码示例展示了如何在 Java 中为 PPT 幻灯片添加注释。
// 创建或加载演示文稿
Presentation presentation = new Presentation("presentation.pptx");
try {
// 添加空幻灯片或获取现有幻灯片的参考
presentation.getSlides().addEmptySlide(presentation.getLayoutSlides().get_Item(0));
// 添加作者
ICommentAuthor author = presentation.getCommentAuthors().addAuthor("Usman", "UA");
// 设置评论的位置
Point2D.Float point = new Point2D.Float(0.2f, 0.2f);
// 在第一张幻灯片上添加幻灯片评论
author.getComments().addComment("Hello, this is slide comment", presentation.getSlides().get_Item(0), point, new Date());
// 保存演示文稿
presentation.save("add-comment.pptx", SaveFormat.Pptx);
} finally {
if (presentation != null)
presentation.dispose();
}
以下是我们使用上述代码示例添加的评论的屏幕截图。

在 Java 中的 PPT 幻灯片中添加评论回复
Aspose.Slides 还允许您添加对评论的回复。回复本身是作为现有评论的子项出现的评论。那么让我们看看如何用Java在PowerPoint PPT幻灯片中添加对评论的回复。
- 首先,加载演示文件或使用 Presentation 类创建一个新文件。
- 使用 Presentation.getCommentAuthors().addAuthor(String, String) 方法添加新作者。
- 使用 ICommentAuthor.getComments().addComment(String, ISlide, Point2D.Float, Date) 方法添加评论并获取返回的对象。
- 以相同的方式插入另一个注释并在对象中获取它的引用。
- 使用 IComment.setParentComment(IComment) 方法设置第二条评论的父级。
- 最后,使用 Presentation.save(String, SaveFormat) 方法保存演示文稿。
以下代码示例展示了如何在 Java 中的 PPTX 演示文稿中添加对评论的回复。
// 创建或加载演示文稿
Presentation presentation = new Presentation("presentation.pptx");
try {
// 添加空幻灯片或获取现有幻灯片的参考
presentation.getSlides().addEmptySlide(presentation.getLayoutSlides().get_Item(0));
// 添加作者
ICommentAuthor author = presentation.getCommentAuthors().addAuthor("Usman", "UA");
// 设置评论的位置
Point2D.Float point = new Point2D.Float(0.2f, 0.2f);
// 在第一张幻灯片上添加幻灯片评论
IComment comment = author.getComments().addComment("Hello, this is slide comment", presentation.getSlides().get_Item(0), point, new Date());
// 添加回复评论
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);
// 添加回复评论
IComment reply2 = author.getComments().addComment("This is second reply.", presentation.getSlides().get_Item(0), new Point2D.Float(10, 10), new Date());
reply2.setParentComment(comment);
// 保存演示文稿
presentation.save("add-comment-reply.pptx", SaveFormat.Pptx);
} finally {
if (presentation != null)
presentation.dispose();
}
以下屏幕截图显示了上述代码示例的输出。

在 Java 中阅读 PPT 幻灯片中的评论
使用 Aspose.Slides,您还可以阅读特定作者或所有作者的评论。以下是用Java阅读PPT幻灯片中的评论的步骤。
- 使用 Presentation 类加载演示文件。
- 使用 Presentation.getCommentAuthors() 集合遍历作者列表。
- 对于每个作者,使用 ICommentAuthor.getComments() 方法遍历其评论。
- 阅读并打印评论详细信息。
以下代码示例展示了如何在 Java 中阅读 PPT 幻灯片中的评论。
// 加载演示文稿
Presentation presentation = new Presentation("add-comment.pptx");
try {
// 遍历作者
for (ICommentAuthor commentAuthor : presentation.getCommentAuthors())
{
// 访问每位作者
CommentAuthor author = (CommentAuthor) commentAuthor;
// 循环浏览作者的评论
for (IComment comment1 : author.getComments())
{
// 阅读评论
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();
}
从 Java 中的 PowerPoint PPT 中删除评论
在上一节中,您已经了解了如何通过从评论集合中访问评论来阅读评论。同样,您可以在获得参考后删除评论。以下代码示例显示了如何在 Java 中删除 PowerPoint 演示文稿中的注释。
// 加载演示文稿
Presentation presentation = new Presentation("add-comment.pptx");
try {
// 获取第一张幻灯片
ISlide slide = presentation.getSlides().get_Item(0);
// 获取评论
IComment[] comments = slide.getSlideComments(null);
// 使用索引删除所需的评论
comments[0].remove();
// 保存演示文稿
presentation.save("remove-comments.pptx", SaveFormat.Pptx);
} finally {
if (presentation != null)
presentation.dispose();
}
获得免费许可证
您可以通过请求 临时许可 来使用 Aspose.Slides for Java,而不受评估限制。
结论
在本文中,您学习了如何使用 Java 在 PowerPoint PPT 幻灯片中添加评论。此外,我们还介绍了如何以编程方式添加对评论的回复。最后,我们演示了如何阅读或删除 PPT 幻灯片中的评论。您可以访问 文档 来探索更多关于 Aspose.Slides for Java 的信息。此外,您可以将您的查询发布到我们的 论坛。