Unable to Remove All Shapes from PowerPoint Presentation Slides in C#

Hi,
I’m trying to remove all shapes form a slide or multiple slides.
I tried the code below for multiple times,only to find that only few shapes were removed.
Shapes like text frame,picture frame,video frame and group shapes still exsit.

This is the code I used:

Presentation pres = new Presentation(filename);
for (int islide = 0; islide < pres.Slides.Count; slide++)
{
    ISlide slide = pres.Slides[islide];
    for (int ishp = 0; ishp < slide.Shapes.Count; ishp++)
    {
        slide.Shapes.RemoveAt(0);
    }
}

I’ve heard that the possible culprit that caused this is the changing indices.But I still can’t figure out how to fix this.
Please tell me how I can remove all shapes form slides.
Thanks and Regards :heartbeat:

@Loui,
Thank you for describing the issue.

I’ve reproduced the problem with removing shapes from slides and added a ticket with ID SLIDESNET-43504 to our issue tracking system. We apologize for any inconvenience. Our development team will investigate the case. You will be notified when a new release of Aspose.Slides with a fix is published.

It would be great if you could share the following additional data and information:

  • your presentation file to test
  • OS version on which the code was executed
  • .NET target platform in your app

Thanks for your reply. :heartbeat:
By the way,may I ask you if there is a way to fix this issue temporarily?
For example,can I just write code to delete different types of shapes respectively?

@Loui,
To remove a shape by a specified type, you can instantiate a collection of the shapes, enumerate it, and check the type of shapes as shown below:

using var pres = new Presentation("example.pptx");

foreach (var slide in pres.Slides)
{
    var shapeCollection = new List<IShape>(slide.Shapes);
                
    foreach (var shape in shapeCollection)
    {
        if (shape is IGroupShape groupShape) // for example
        {
            slide.Shapes.Remove(shape);
        }
    }
}

pres.Save("output.pptx", SaveFormat.Pptx);

You can also remove all shapes like this:

slide.Shapes.Clear();

Documents: Shape Manipulations
API Reference: ISlideCollection interface

1 Like

Thank you so much! :heartbeat: :heartbeat:
Your code works perfectly!
You’ve really helped me a lot :heartbeat: :heartbeat:

@Loui,
Thank you for using Aspose.Slides.