使用C#代码在 PowerPoint 中添加或删除节

PowerPoint 中的“节”功能可以将幻灯片划分为不同的分组或模块,方便统一管理和结构梳理。为各个节设置独特的名称,不仅有助于快速定位特定幻灯片组,还能清晰呈现演示文稿的内容结构和主题脉络。

本文将介绍如何使用 Spire.Presentation for .NET 通过编程方式在 PowerPoint 文档中添加或删除节。

安装 Spire.Presentation for .NET

在开始之前,需要先将 Spire.Presentation for .NET 安装到您的 .NET 项目中,并添加相应的 DLL 文件引用。您可以通过官网下载程序包后手动添加 DLL 文件,或直接通过 NuGet 安装并自动完成引用配置。

PM> Install-Package Spire.Presentation

在 C# 和 VB.NET 中为 PowerPoint 文档末尾添加节

Spire.Presentation for .NET 提供了 Presentation.SectionList.Append(string sectionName) 方法,可在 PowerPoint 文档末尾添加一个指定名称的节。

具体操作步骤如下:

  1. 创建 Presentation 类的实例。
  2. 使用 Presentation.LoadFromFile() 方法加载 PowerPoint 文档。
  3. 调用 Presentation.SectionList.Append(string sectionName) 方法,在文档末尾添加新的节。
  4. 使用 Presentation.SaveToFile() 方法保存修改后的文档。

示例代码如下:

using Spire.Presentation;

namespace AppendSectionAtEnd
{
    class Program
    {
        static void Main(string[] args)
        {
            // 创建 Presentation 实例
            Presentation ppt = new Presentation();

            // 加载示例 PowerPoint 文档
            ppt.LoadFromFile("Test.pptx");

            // 在文档末尾添加一个节
            Section section = ppt.SectionList.Append("End Section");

            // 保存结果文档
            ppt.SaveToFile("AddSectionAtEnd.pptx", FileFormat.Pptx2013);
        }
    }
}

在 C# 和 VB.NET 中在指定节之前插入新节

如果希望在现有节之前插入一个新的节,以优化文档结构和逻辑顺序,可以使用 Spire.Presentation for .NET 提供的 Presentation.SectionList.Insert(int sectionIndex, string sectionName) 方法。该方法可根据节的索引位置,在指定节前插入新的节。

具体操作步骤如下:

  1. 创建 Presentation 类的实例。
  2. 使用 Presentation.LoadFromFile() 方法加载 PowerPoint 文档。
  3. 调用 Presentation.SectionList.Insert(int sectionIndex, string sectionName) 方法,在指定索引位置的节之前插入新的节。
  4. 使用 Presentation.SaveToFile() 方法保存修改后的文档。

示例代码如下:

using Spire.Presentation;

namespace InsertSectionAtSpecifiedPosition
{
    class Program
    {
        static void Main(string[] args)
        {
            // 创建 Presentation 实例
            Presentation ppt = new Presentation();

            // 加载示例 PowerPoint 文档
            ppt.LoadFromFile("Test.pptx");

            // 在第二个节之前插入一个新的节
            Section section = ppt.SectionList.Insert(1, "New Section");

            // 保存结果文档
            ppt.SaveToFile("InsertSectionAtSpecifiedPosition.pptx", FileFormat.Pptx2013);
        }
    }
}

在 C# 和 VB.NET 中在指定幻灯片之前添加节

如果需要将现有的 PowerPoint 幻灯片划分为不同的节,可以使用 Presentation.SectionList.Add(string sectionName, ISlide slide) 方法,在指定幻灯片之前添加一个新的节。通过这种方式,可以更清晰地组织幻灯片结构。

具体操作步骤如下:

  1. 创建 Presentation 类的实例。
  2. 使用 Presentation.LoadFromFile() 方法加载 PowerPoint 文档。
  3. 通过 Presentation.Slides 属性获取指定的幻灯片。
  4. 调用 Presentation.SectionList.Add(string sectionName, ISlide slide) 方法,在该幻灯片之前添加新的节。
  5. 使用 Presentation.SaveToFile() 方法保存修改后的文档。

示例代码如下:

using Spire.Presentation;

namespace AddSectionBeforeSlide
{
    class Program
    {
        static void Main(string[] args)
        {
            // 创建 Presentation 实例
            Presentation ppt = new Presentation();

            // 加载示例 PowerPoint 文档
            ppt.LoadFromFile("Test.pptx");

            // 获取文档中的第二张幻灯片
            ISlide slide = ppt.Slides[1];

            // 在第二张幻灯片之前添加一个新的节
            Section section = ppt.SectionList.Add("New Section", slide);

            // 保存结果文档
            ppt.SaveToFile("AddSectionBeforeSlide.pptx", FileFormat.Pptx2013);
        }
    }
}

在 C# 和 VB.NET 中删除 PowerPoint 文档中的节

如果不再需要某个节,可以使用 Presentation.SectionList.RemoveAt(int index) 方法将其删除。需要注意的是,删除节并不会删除该节中的幻灯片,幻灯片仍会保留在文档中。

具体操作步骤如下:

  1. 创建 Presentation 类的实例。
  2. 使用 Presentation.LoadFromFile() 方法加载 PowerPoint 文档。
  3. 调用 Presentation.SectionList.RemoveAt(int index) 方法删除指定索引位置的节。
    如果需要删除文档中的所有节,可使用 Presentation.SectionList.RemoveAll() 方法。
  4. 使用 Presentation.SaveToFile() 方法保存修改后的文档。

示例代码如下:

using Spire.Presentation;

namespace RemoveSection
{
    class Program
    {
        static void Main(string[] args)
        {
            // 创建 Presentation 实例
            Presentation ppt = new Presentation();

            // 加载示例 PowerPoint 文档
            ppt.LoadFromFile("Test.pptx");

            // 删除第二个节
            ppt.SectionList.RemoveAt(1);

            // 删除所有节
            //ppt.SectionList.RemoveAll();

            // 保存结果文档
            ppt.SaveToFile("RemoveSection.pptx", FileFormat.Pptx2013);
        }
    }
}

申请临时许可证

如果您希望移除生成文档中的评估提示信息,或解除功能限制,请申请一个为期 30 天的试用许可证。

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • """1.个性化消息: 将用户的姓名存到一个变量中,并向该用户显示一条消息。显示的消息应非常简单,如“Hello ...
    她即我命阅读 13,836评论 0赞 6
  • 1、expected an indented block 冒号后面是要写上一定的内容的(新手容易遗忘这一点); 缩...
    庵下桃花仙阅读 4,785评论 1赞 2
  • 一、工具箱(多种工具共用一个快捷键的可同时按【Shift】加此快捷键选取)矩形、椭圆选框工具 【M】移动工具 【V...
    墨雅丫阅读 5,029评论 0赞 0
  • 跟随樊老师和伙伴们一起学习心理知识提升自已,已经有三个月有余了,这一段时间因为天气的原因休课,顺便整理一下之前学习...
    学习思考行动阅读 4,600评论 0赞 2
  • 一脸愤怒的她躺在了床上,好几次甩开了他抱过来的双手,到最后还坚决的翻了个身,只留给他一个冷漠的背影。 多次尝试抱她...
    海边的蓝兔子阅读 3,187评论 1赞 4

友情链接更多精彩内容