如何在C#/VB.NET程序中为幻灯片添加或删除批注

无论是在日常的学习还是工作中,PowerPoint都是一种非常实用的文档格式。在制作PPT时,我们有时需要在幻灯片中添加批注。该功能可以帮助我们及时补充有关信息或者注明修改意见。在不需要此项内容时,也可以将其删除。Free Spire.Presentation for .NET就可以帮助我们在C#和VB.NET程序中轻松实现这两个功能。具体操作步骤和相关代码如下:

准备工作:

安装 Free Spire.Presentation for .NET

方法一,通过NuGet安装Free Spire.Presentation for .NET

    依次选择工具>NuGet包管理器>程序包管理器控制台,然后执行以下命令:

PM> Install-Package FreeSpire.Presentation

方法二,在程序中手动引入Spire.Presentation.dll文件:

    将Free Spire.Presentation for .NET 下载到本地,解压并安装。安装完成后,打开 Visual Studio创建新项目,在右边的“解决方案资源管理器”中右键点击“引用”,再依次选择“添加引用”> “浏览”,找到安装路径下BIN文件夹中的dll文件,点击“确定”,将其添加引用至程序中。

为幻灯片添加批注

具体操作步骤:

  • 创建Presentation实例。
  • 使用Presentation.LoadFromFile()方法,加载Powerpoint示例文档。
  • 使用CommentAuthorList.AddAuthor()方法,添加批注作者。
  • 利用 Presentation.Slides[]属性,获取指定幻灯片。
  • 使用ISlide.AddComment(ICommentAuthor, String, PointF, DateTime)方法,为幻灯片添加批注。
  • 使用Presentation.SaveToFile()方法,保存结果文档。

相关代码:

C#:

using Spire.Presentation;
using System;

namespace AddComment
{

    class Program
    {

        static void Main(string[] args)
        {
            //创建Presentation实例
            Presentation presentation = new Presentation();

            //加载Powerpoint示例文档
            presentation.LoadFromFile(@"sample.pptx");

            //添加批注作者 
            ICommentAuthor author = presentation.CommentAuthors.AddAuthor("张三", "批注:");

            //为指定幻灯片添加批注
            presentation.Slides[0].AddComment(author, "世界卫生组织(WHO)是国际上最大的政府间卫生组织。", new System.Drawing.PointF(25, 22), DateTime.Now);
            
            //保存结果文档
            presentation.SaveToFile("comment.pptx", FileFormat.Pptx2010);
        }

    }
}

VB.NET:

Imports Spire.Presentation
Imports System

Namespace AddComment

    Friend Class Program

        Private Shared Sub Main(ByVal args As String())
            '创建Presentation实例
            Dim presentation As Presentation = New Presentation()

            '加载Powerpoint示例文档
            presentation.LoadFromFile("sample.pptx")

            '添加批注作者 
            Dim author As ICommentAuthor = presentation.CommentAuthors.AddAuthor("张三", "批注:")

            '为指定幻灯片添加批注
            presentation.Slides(0).AddComment(author, "世界卫生组织(WHO)是国际上最大的政府间卫生组织。", New Drawing.PointF(25, 22), Date.Now)

            '保存结果文档
            presentation.SaveToFile("comment.pptx", FileFormat.Pptx2010)
        End Sub

    End Class
End Namespace

为幻灯片删除批注

具体操作步骤:

  • 创建Presentation实例。
  • 使用Presentation.LoadFromFile()方法,加载Presentation示例文档。
  • 利用 Presentation.Slides[]属性,获取指定幻灯片。
  • 使用ISlide.DeleteComment(Comment)方法,删除指定幻灯片的批注。
  • 使用Presentation.SaveToFile()方法,保存结果文档。

相关代码:

C#:

using Spire.Presentation;

namespace RemoveComment
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建Presentation实例
            Presentation presentation = new Presentation();

            //加载Presentation示例文档
            presentation.LoadFromFile("comment.pptx");

            //获取指定幻灯片
            ISlide slide = presentation.Slides[0];

            //删除指定幻灯片的批注
            slide.DeleteComment(slide.Comments[0]);

            //保存结果文档
            presentation.SaveToFile("RemoveComment.pptx", FileFormat.Pptx2010);

        }
    }
}

VB.NET:

Imports Spire.Presentation

Namespace RemoveComment
    Friend Class Program
        Private Shared Sub Main(ByVal args As String())
            '创建Presentation实例
            Dim presentation As Presentation = New Presentation()

            '加载Presentation示例文档
            presentation.LoadFromFile("comment.pptx")

            '获取指定幻灯片
            Dim slide As ISlide = presentation.Slides(0)

            '删除指定幻灯片的批注
            slide.DeleteComment(slide.Comments(0))

            '保存结果文档
            presentation.SaveToFile("RemoveComment.pptx", FileFormat.Pptx2010)

        End Sub
    End Class
End Namespace

posted @ 2022-10-28 09:37  Gia-  阅读(152)  评论(0编辑  收藏  举报