使用C#代码添加或删除PPT页面

幻灯片是 PowerPoint 文档中最基本的组成部分。每个 PowerPoint 演示文稿都由一系列幻灯片构成,每张幻灯片可包含文本、形状、表格和图片等不同元素。在编辑 PowerPoint 文档时,添加和删除幻灯片往往是最常用的操作之一。本文将介绍如何使用 Spire.Presentation for .NET 通过编程方式添加或删除 PowerPoint 幻灯片。

安装 Spire.Presentation for .NET

首先,需要将 Spire.Presentation for .NET 包中包含的 DLL 文件添加为 .NET 项目的引用。

这些 DLL 文件可以通过以下两种方式获取:从指定链接下载,或通过 NuGet 进行安装。

cs 复制代码
PM> Install-Package Spire.Presentation

在 PowerPoint 文档末尾添加新幻灯片

通过 Spire.Presentation for .NET 提供的 Presentation.Slides.Append() 方法,可以在 PowerPoint 文档的最后一张幻灯片之后追加一张新的幻灯片。

示例代码如下:

cs 复制代码
using Spire.Presentation;

namespace AddNewSlideinPowerPoint
{
    class Program
    {
        static void Main(string[] args)
        {
            //初始化 Presentation 类的实例
            Presentation presentation = new Presentation();

            //加载示例 PowerPoint 文档
            presentation.LoadFromFile("Sample.pptx");

            //在文档末尾添加一张新幻灯片
            presentation.Slides.Append();

            //保存结果文档
            presentation.SaveToFile("AddSlide.pptx", FileFormat.Pptx2013);
        }
    }
}

在 PowerPoint 中在指定幻灯片前插入新幻灯片

有时,你可能需要在某张特定幻灯片之前插入一张新幻灯片,以添加额外的辅助信息。

示例代码如下:

cs 复制代码
using Spire.Presentation;

namespace InsertSlideinPowerPoint
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建 Presentation 对象
            Presentation presentation = new Presentation();

            //加载示例 PowerPoint 文档
            presentation.LoadFromFile("Sample.pptx");

            //在第二张幻灯片之前插入一张空白幻灯片
            presentation.Slides.Insert(1);

            //保存结果文档
            presentation.SaveToFile("InsertSlide.pptx", FileFormat.Pptx2013);
        }
    }
}

从 PowerPoint 文档中删除指定幻灯片

如果你想从文档中移除不需要的幻灯片,可以使用 Presentation.Slides.RemoveAt(int index) 方法。

示例代码如下:

cs 复制代码
using Spire.Presentation;

namespace DeletePowerPointSlide
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建 Presentation 对象
            Presentation presentation = new Presentation();

            //加载示例 PowerPoint 文档
            presentation.LoadFromFile("Sample.pptx");

            //删除第一张幻灯片
            presentation.Slides.RemoveAt(0);

            //保存结果文档
            presentation.SaveToFile("RemoveSlide.pptx", FileFormat.Pptx2013);
        }
    }
}

申请临时许可证

如果你希望从生成的文档中去除评估信息,或解除功能限制,可以联系官方申请一个为期 30 天的试用许可证。

相关推荐
小白不白1115 小时前
C# WinForm 与 VP 二次开发
开发语言·c#
程序猿乐锅5 小时前
【JAVASE | 第十七篇】Java 网络通信
java·开发语言
飞舞哲5 小时前
三维点云最小二乘拟合MATLAB程序
开发语言·算法·matlab
有点。5 小时前
C++(贪心算法二)
开发语言·c++·贪心算法
meilindehuzi_a5 小时前
透视 V8 底部:从物理内存到函数式哲学,重新解构 JavaScript 数组
开发语言·javascript·ecmascript
jllllyuz6 小时前
HVDC 高压直流输电系统 MATLAB/Simulink 仿真全集
开发语言·matlab
我命由我123456 小时前
Windows 操作系统 - Windows 查看防火墙是否开启、Windows 查看防火墙放行端口
java·运维·开发语言·windows·java-ee·操作系统·运维开发
天天进步20156 小时前
Python全栈项目--基于Python的数据库管理工具
开发语言·数据库·python
YHHLAI6 小时前
JavaScript 数据结构精讲:数组底层与实战避坑
开发语言·javascript·数据结构
有点。6 小时前
C++贪心算法一(练习题)
开发语言·c++·贪心算法