Remove Slide from Presentation

Overview

Sometimes, developers may need to remove a slide from the presentation due to any reason. Aspose.Slides for .NET offers few methods to do so. In this topic, we will explore these methods to accomplish this task. We know that Presentation class in Aspose.Slides for .NET represents a presentation file. Presentation class encapsulates a ISlideCollection that acts as a repository of all slides that are the part of the presentation. Developers can remove a slide from this Slides collection in two ways:

  1. Using Slide Reference
  2. Using Slide Index

Remove Slide by Reference

To remove a slide using its reference, please follow the steps below:

  1. Create an instance of Presentation class.
  2. Obtain the reference of a slide by using its Id or Index.
  3. Remove the referenced slide from the presentation.
  4. Write the modified presentation file.
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_Slides_Presentations_CRUD();

// Instantiate a Presentation object that represents a presentation file
using (Presentation pres = new Presentation(dataDir + "RemoveSlideUsingReference.pptx"))
{

    // Accessing a slide using its index in the slides collection
    ISlide slide = pres.Slides[0];

    // Removing a slide using its reference
    pres.Slides.Remove(slide);

    //Writing the presentation file
    pres.Save(dataDir + "modified_out.pptx", Aspose.Slides.Export.SaveFormat.Pptx);
}

Remove Slide by Index

To remove a slide using its index position in the slides collection of the presentation, please follow the steps below:

  1. Create an instance of Presentation class.
  2. Remove the slide from the presentation by using its index position.
  3. Write the modified presentation file.
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_Slides_Presentations_CRUD();

// Instantiate a Presentation object that represents a presentation file
using (Presentation pres = new Presentation(dataDir + "RemoveSlideUsingIndex.pptx"))
{

    // Removing a slide using its slide index
    pres.Slides.RemoveAt(0);

    // Writing the presentation file
    pres.Save(dataDir + "modified_out.pptx", Aspose.Slides.Export.SaveFormat.Pptx);

}