使用C#代码添加或删除PPT页面
幻灯片是 PowerPoint 文档中最基本的组成部分。每个 PowerPoint 演示文稿都由一系列幻灯片构成,每张幻灯片可包含文本、形状、表格和图片等不同元素。在编辑 PowerPoint 文档时,添加和删除幻灯片往往是最常用的操作之一。本文将介绍如何使用 Spire.Presentation for .NET 通过编程方式添加或删除 PowerPoint 幻灯片。
安装 Spire.Presentation for .NET
首先,需要将 Spire.Presentation for .NET 包中包含的 DLL 文件添加为 .NET 项目的引用。
这些 DLL 文件可以通过以下两种方式获取:从指定链接下载,或通过 NuGet 进行安装。
PM> Install-Package Spire.Presentation在 PowerPoint 文档末尾添加新幻灯片
通过 Spire.Presentation for .NET 提供的 Presentation.Slides.Append() 方法,可以在 PowerPoint 文档的最后一张幻灯片之后追加一张新的幻灯片。
示例代码如下:
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 中在指定幻灯片前插入新幻灯片
有时,你可能需要在某张特定幻灯片之前插入一张新幻灯片,以添加额外的辅助信息。
示例代码如下:
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) 方法。
示例代码如下:
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 天的试用许可证。
