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

相关推荐
矶鹬笛手13 分钟前
(2.1) 信息技术及其发展
sql·计算机网络·c#
u***27611 小时前
C#数据库操作系列---SqlSugar完结篇
网络·数据库·c#
p***h6431 小时前
JavaScript在Node.js中的异步编程
开发语言·javascript·node.js
散峰而望1 小时前
C++数组(二)(算法竞赛)
开发语言·c++·算法·github
Porunarufu1 小时前
Java·关于List
java·开发语言
子不语1802 小时前
Python——函数
开发语言·python
ndjnddjxn2 小时前
Rust学习
开发语言·学习·rust
月光技术杂谈2 小时前
实战:C驱动框架嵌入Rust模块的互操作机制与完整流程
c语言·开发语言·rust·ffi·跨语言·bindgen·互操作
t198751282 小时前
基于MATLAB的指纹识别系统完整实现
开发语言·matlab
笑非不退3 小时前
C# c++ 实现程序开机自启动
开发语言·c++·c#