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

在审阅 PowerPoint 演示文稿中的内容时,评论用于撰写反馈。可以针对特定单词、短语或 PPT 幻灯片上的任何内容添加评论。在本文中,您将学习如何在 C# 中以编程方式向 PowerPoint PPT 幻灯片添加注释。此外,我们将介绍如何阅读或删除幻灯片评论并添加他们的回复。

C# .NET API 在 PowerPoint 中处理评论

为了处理 PowerPoint 演示文稿中的评论,我们将使用 Aspose.Slides for .NET。它是创建和操作 PowerPoint 演示文稿的强大 API。您可以 下载 API 的 DLL 或使用 NuGet 安装它。

PM> Install-Package Aspose.Slides.NET

在 C# 中为 PPT 幻灯片添加注释

在 PowerPoint 演示文稿中,每条评论都附有特定作者。然而,每条评论都包含一些附加信息,例如创建时间、添加它的幻灯片及其位置。以下是在 C# 中向 PPT 幻灯片添加注释的步骤。

以下代码示例展示了如何在 C# 中向 PPT 幻灯片添加注释。

// 加载演示文件或创建一个新文件
using (Presentation presentation = new Presentation())
{
    // 使用 Presentation.Slides 集合添加空幻灯片或获取现有幻灯片的引用
    presentation.Slides.AddEmptySlide(presentation.LayoutSlides[0]);

    // 添加作者
    ICommentAuthor author = presentation.CommentAuthors.AddAuthor("Usman", "UA");

    // 设置评论位置
    PointF point = new PointF();
    point.X = 0.2f;
    point.Y = 0.2f;

    // 在第一张幻灯片上添加幻灯片评论
    author.Comments.AddComment("Hello, this is slide comment", presentation.Slides[0], point, DateTime.Now);

    // 保存演示文稿
    presentation.Save("Comments_out.pptx", SaveFormat.Pptx);
}

以下是我们使用上述代码示例添加的评论的屏幕截图。

在 C# 中向 PPT 幻灯片插入注释

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

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

以下代码示例演示如何在 C# 中的 PPTX 演示文稿中添加对评论的回复。

// 加载演示文件或创建一个新文件
using (Presentation presentation = new Presentation())
{
    // 添加作者和评论
    ICommentAuthor author = presentation.CommentAuthors.AddAuthor("Usman", "MF");
    IComment comment = author.Comments.AddComment("Hello, this is slide comment.", presentation.Slides[0], new System.Drawing.PointF(0.2f, 0.2f), DateTime.Now);

    // 添加回复评论
    IComment reply = author.Comments.AddComment("This is the reply to the comment.", presentation.Slides[0], new System.Drawing.PointF(0.2f, 0.2f), DateTime.Now);
    reply.ParentComment = comment;

    // 添加回复评论
    IComment reply2 = author.Comments.AddComment("This is second reply.", presentation.Slides[0], new System.Drawing.PointF(0.2f, 0.2f), DateTime.Now);
    reply2.ParentComment = comment;

    // 保存演示文稿
    presentation.Save("Comments_out.pptx", SaveFormat.Pptx);
}

以下屏幕截图显示了上述代码示例的输出。

添加对PPT中的评论的回复

在 C# 中阅读 PPT 幻灯片中的评论

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

以下代码示例展示了如何在 C# 中阅读 PPT 幻灯片中的注释。

// 加载演示文件
using (Presentation presentation = new Presentation("Comments_out.pptx"))
{
    // 遍历作者
    foreach (var commentAuthor in presentation.CommentAuthors)
    {
        // 循环浏览作者的评论
        var author = (CommentAuthor)commentAuthor;
        foreach (var comment in author.Comments)
        {
            Console.WriteLine("ISlide :" + comment.Slide.SlideNumber + " has comment: " + comment.Text + " with Author: " + comment.Author.Name + " posted on time :" + comment.CreatedTime + "\n");
        }
    }
}

在 C# 中从 PowerPoint PPT 中删除评论

在上一节中,您已经了解了如何通过从评论集合中访问评论来阅读评论。同样,您可以在获得参考后删除评论。以下代码示例演示如何在 C# 中删除 PowerPoint 演示文稿中的注释。

// 加载演示文稿
using (Presentation presentation = new Presentation("Comments_out.pptx"))
{
    // 获取第一张幻灯片
    ISlide slide = presentation.Slides[0];

    // 获取评论
    var comments = slide.GetSlideComments(null);

    // 使用索引删除所需的评论
    comments[0].Remove();

    // 保存演示文稿
    presentation.Save("Comments_out.pptx", SaveFormat.Pptx);
}

获得免费许可证

您可以通过请求 临时许可证 来使用 Aspose.Slides for .NET,而不受评估限制。

结论

在本文中,您学习了如何使用 C# 在 PowerPoint PPT 幻灯片中添加评论。此外,我们还介绍了如何以编程方式添加对评论的回复。最后,我们演示了如何阅读或删除 PPT 幻灯片中的评论。您可以访问 文档 来探索更多关于 Aspose.Slides for .NET 的信息。此外,您可以将您的查询发布到我们的 论坛

也可以看看