How to Delete/Add Multiple Slides at a Time Using Aspose.Slides for .NET?

Hi
I am trying to write a program that can enable users to delete multiple slides at a time.
I’ve created a listbox where users can add the indices of the slides that they want to delete.
When they click the “Start” button, the program will automatically loop through the listbox and delete the slides by using the indices provided.
However,what troubles me is that the indices of the slides in a presentation isn’t fixed,they’ll change when there’re slides deleted. So,when there’re more than 1 slide needed to be deleted,things get really complicated and tricky, since I need to change every index of the rest slides needed to be deleted to a specific number according to the amount of slides thant have been deleted.
So could you please tell me a way of deleting multiple slides at a time without dealing with the tricky changing indices?
By the way, is there also a way to add multiple slides at a time without dealing with the changing indices?
Thanks and Regards :heartbeat: :heartbeat: :heartbeat:

@Loui,
Thank you for describing the issue.

I think the easiest way to remove presentation slides at selected indexes could be as follows:

var slideIndices = new List<int> { 1, 3 }; // for example

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

// Collect the slides to delete.
var slidesToDelete = new List<ISlide>(slideIndices.Count);
slideIndices.ForEach(index => slidesToDelete.Add(presentation.Slides[index]));

// Delete the selected slides.
slidesToDelete.ForEach(slide => presentation.Slides.Remove(slide));

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

We need to know more details to help you with this. In any case, it has to do with algorithms.

Thank you so much! :heartbeat: :heartbeat:
Your code works perfectly! :heartbeat: :heartbeat:

I’m writing a piece of code that can insert multiple slides into multiple positions of a presentation.
However when I insert these slides, I am confronted with the same issue. Due to the changing indices, when there’re more than 1 postion where slides are inserted,things get really tricky. The rest slides needed to be inserted won’t be inserted into the set position, but into an unwanted position.
May I ask you whether there’s a way to insert multiple slides into multiple positions at a time?
Thanks and Regards :heartbeat: :heartbeat: :heartbeat:

P.S. The slides needed to be inserted include empty slides and clone form other presentations.

@Loui,
Aspose.Slides library provides InsertClone and InsertEmptySlide methods for such actions. But you are right, the solution for your task is not obvious. I’ve added a ticket with ID SLIDESNET-43510 to our issue tracking system. Our development team will consider implementing such a feature. You will be notified when the issue is resolved.

API Reference: ISlideCollection interface

1 Like

You could insert these in reverse starting with the last slide and then working to the first slide this way slide positions would not be affected for the next inserted slides

1 Like

@bowespublishing,
Thank you for your help.

Thank you so much for replying me. :heartbeat: :heartbeat: :heartbeat:
I’ve solved this issue by applying the method posted by bowespublishing

Thank you so much for informing me of this method! :heartbeat: :heartbeat: :heartbeat:
It’s really a GREAT way to solve this issue.
I’ve tried it out and it works perfectly!

@Loui,
We are glad to know that the issue has been resolved on your end. Thank you for using Aspose.Slides.