幻燈片是 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 天的試用許可證。