Can't add more than 10 empty slides to a presentation

Hi - I’m running the latest version of Slides for .NET but I’m getting an error when I attempt to generate more than 10 empty slides based on a random number of images in a selected folder, and then add them to a presentation.

The error I receive is an "Index was out of range. Must be non-negative and less than the size of the collection."

I’ve simplified the loop so its just a simply for loop from 1 to 20, but it always fails at 11 on the AddEmptySlide method.

The loop is:-

using (Presentation pres = new Presentation()){

ISlideCollection slds = pres.Slides;

for (int i = 0; i < 20; i++)
{
//Add an empty slide to the Slides collection
slds.AddEmptySlide(pres.LayoutSlides[i]);

}
}

Throw it in a button click event, try it and let me know if you get the same error, and what I can do to fix this. Also, I’ve checked and verified my license is correct so it’s not license related.

Thanks in advance
Jake

Hi Jake,

Thanks for inquiring Aspose.Slides.

I have observed the sample code shared and have observed the issue in your call of AddEmptySlide() method. You are actually, using a new Layout slide on every call of AddEmptySlide(). Actually, there are 11 layout slides inside default master of presentation. So, when the index exceeds 11, there is no more Layout slide available and you get exception. This is not an issue but understanding problem. You can create hundreds of slides using Aspose.Slides but every slide needs to have a layout slide. The layouts can be repeated for many slides. I hope this will clarify the concept. Please share, if I may help you further in this regard.

Many Thanks,

Thanks for getting back to me Mudassir ,
I’m confused - your examples in the help guide only mention the layoutslide when dealing with adding of slides - I can find no reference across your site as to what I should be using to add a new slide in a loop scenario. Do I need a layout slide first, then add new slides using this layout?
Can you point me in the direction as to where I may find either a reference or a help file?

Thanks
Jake

Hi Jake,

Let me re-explain the things. I have not said that you first add the layout slide. The layout slide is a template slide inside master as attached in the image on left side. When you add any new slide to presentation, you select its layout type. I hope you will be getting the point up till now. So, when you add a new empty slide, you need to set the layout type for that slide and you select the layout from the master you are using. This way, if you want to add 100 Blank slides, you will have to simply call AddEmptySlide() method and pass the Layout slide for blank slide in the arguments. The following code will help you in understanding the concept.

Presentation pres = new Presentation(“withActiveX.pptm”);
for (int i = 0; i < 100; i++)
{
pres.Slides.AddEmptySlide(pres.Masters[0].LayoutSlides.GetByType(SlideLayoutType.Blank));

}

pres.Save(“Test.Pptx”, SaveFormat.Pptx);

I hope this will elaborate the concept to you. Please share, if I may help you further in this regard.

Many Thanks,