在 Java 中添加或删除 PowerPoint PPT 中的注释

在 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 幻灯片添加注释的步骤。

以下代码示例展示了如何在 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 幻灯片插入评论

在 Java 中的 PPT 幻灯片中添加评论回复

Aspose.Slides 还允许您添加对评论的回复。回复本身是作为现有评论的子项出现的评论。那么让我们看看如何用Java在PowerPoint PPT幻灯片中添加对评论的回复。

以下代码示例展示了如何在 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中的评论的回复

在 Java 中阅读 PPT 幻灯片中的评论

使用 Aspose.Slides,您还可以阅读特定作者或所有作者的评论。以下是用Java阅读PPT幻灯片中的评论的步骤。

以下代码示例展示了如何在 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 的信息。此外,您可以将您的查询发布到我们的 论坛

也可以看看