使用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 天的试用许可证。

相关推荐
hez201041 分钟前
在 .NET 上构建超大托管数组
c#·.net·.net core·gc·clr
雨落倾城夏未凉5 天前
第四章c#方法-参数数组和可选参数(16)
后端·c#
唐青枫6 天前
线程不是越多越快:C#.NET Thread 生命周期、同步与后台工作线程实战
c#·.net
唐青枫7 天前
别只会反射:C#.NET Emit 动态生成代码实战详解
c#·.net
咕白m6258 天前
.NET 环境下 Word 超链接批量提取方案
c#·.net
用户91721561902118 天前
C# 通信协议增量解析:用状态机处理半包和粘包
c#
小码编匠8 天前
C# 工控上位机必备:数据转换工具类与十个核心模块
后端·c#·.net
唐青枫10 天前
别再乱用 StartNew:C#.NET TaskFactory 任务调度实战详解
c#·.net
Artech11 天前
[MAF预定义的AIContextProvider-03]ChatHistoryMemoryProvider——赋予Agent从经验中学习的能力
ai·c#·agent·memory·maf
Scout-leaf12 天前
C#摸鱼实录——IoC与DI案例详解
c#