C#: Change the Slide Order in a PowerPoint Document

Optimizing the order of slides in a PowerPoint presentation is a simple skill. By rearranging slides, you can refine the logic and flow of your presentation, group related points together, or move slides to more impactful locations. This flexibility enables you to create a cohesive, engaging narrative that captivates your audience.

This article demonstrates how to programmatically change the slide order in a PowerPoint document in C# by using the Spire.Presentation for .NET library.

Install Spire.Presentation for .NET

To begin with, you need to add the DLL files included in the Spire.Presentation for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.Presentation

Change the Slide Order in a PowerPoint Document in C#

To reorder slides in a PowerPoint presentation, create two Presentation objects - one to load the original document and another to create a new document. By copying the slides from the original document to the new one in the desired sequence, you can easily rearrange the slide order.

The steps to rearrange slides in a PowerPoint document using C# are as follows.

  • Create a Presentation object.
  • Load a PowerPoint document using Presentation.LoadFromFile() method.
  • Specify the slide order within an array.
  • Create another Presentation object for creating a new presentation.
  • Add the slides from the original document to the new presentation in the specified order using Presentation.Slides.Append() method.
  • Save the new presentation to a PPTX file using Presentation.SaveToFile() method.
  • C#
using Spire.Presentation;

namespace ChangeSlideOrder
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a Presentation object
            Presentation presentation = new Presentation();

            // Load a PowerPoint file
            presentation.LoadFromFile("C:\\Users\\Administrator\\Desktop\\input.pptx");

            // Specify the new slide order within an array
            int[] newSlideOrder = new int[] { 4, 2, 1, 3 };

            // Create another Presentation object
            Presentation new_presentation = new Presentation();

            // Remove the default slide
            new_presentation.Slides.RemoveAt(0);

            // Iterate through the array
            for (int i = 0; i < newSlideOrder.Length; i++)
            {
                // Add the slides from the original PowerPoint file to the new PowerPoint document in the new order
                new_presentation.Slides.Append(presentation.Slides[newSlideOrder[i] - 1]);
            }

            // Save the new presentation to file
            new_presentation.SaveToFile("NewOrder.pptx", FileFormat.Pptx2019);

            // Dispose resources
            presentation.Dispose();
            new_presentation.Dispose();
        }
    }
}

C#: Change the Slide Order in a PowerPoint Document

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.