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

相关推荐
斑马1396 分钟前
Linux软件编程学习笔记(十)——网络(二)——TCP通信
开发语言·网络·php
多敲代码防脱发10 分钟前
Alibaba Sentinel(熔断、限流)(国内下载Sentinel)
java·开发语言·sentinel
艺杯羹12 分钟前
双轨大模型路由实战:从端侧Ollama边缘推理到云端大模型容灾降级架构深度拆解
开发语言·人工智能·ai·架构·php
quantdash_cc15 分钟前
量化数据管道为什么会出现历史 K 线缺口?从 API 请求到数据质量的完整排查方法
开发语言·python·数据分析·量化·股票数据·quantdash
JustCheng19 小时前
Dispacher(1/2)深入解析-详细使用
c#
Interview Aid1121 天前
TikTok OA 四题分享|半小时内 AC,题目基本都是实现题
java·开发语言·算法·面试·职场和发展
CoderIsArt1 天前
C#中UI 线程与 Dispatcher
开发语言·ui·c#
AI情绪识别开源1 天前
检信 ALLEMOTION OS 加密打包可执行程序 — 全面测试报告版本: v1.3功能测试 / 性能测试 /
开发语言·数据结构·人工智能·功能测试