Remove unused MasterSlides

After generating a quite lengthy pptx of 70 pages, i get the annoying “Too many unused MasterSlides” message when opening the file in Powerpoint.
It is clear, that for each slide that i took from another pptx with templates, there are duplicates of the MasterSlides and i guess much more.
Is there a way to remove them all at once in GemBox.Presentation?

Aspose has has a method for this.

If the functionality in powerpoint is accessible via interop or the like, that would be ok too. My use case is with powerpoint available on the machine.

Thanks,
Lars

image

Hi Lars,

When cloning slides, are you using CloneContext?
For instance, see the following example:

Nevertheless, can you send me your PPTX file so that I can use it for testing purposes?
I’ll create some snippet code for you to achieve this task.

Regards,
Mario

Hi Mario,
i created an anonymized example that reproduces the behaviour. I can provide it (but don’t know how to send for now).

I also tried to apply the cloning-example, but it did not work without throwing exceptions.

Anyway i figured out what caused it:
I “thought it was a good idea” to use PresentationDocument.Load to open a pptx file as the sink for the slides to be generated and removed all slides from it before filling it with the custom slides.

var target = PresentationDocument.Load("existing-output-file.pptx");
while (target.Slides.Count > 0)
    target.Slides.RemoveAt(0);

Just using

var target = new PresentationDocument();

fixed the problem for me.

Code:

var target = PresentationDocument.Load("existing-output-file.pptx");
while (target.Slides.Count > 0)
    target.Slides.RemoveAt(0);
// var target = new PresentationDocument();

var source = PresentationDocument.Load("custom-sources.pptx");

// Add intro [0], alternating content [1], [2] and final slide [3]
target.Slides.AddClone(source.Slides[0]);
for (var i = 0; i < 40; i++)
    target.Slides.AddClone(i % 2 == 0 ? source.Slides[1] : source.Slides[2]);
target.Slides.AddClone(source.Slides[3]);

target.Save("result.pptx");

Thanks,
Lars

For future reference, note that you can send us your project via email or support ticket, see the Contact page.

Yes, this approach is better, more efficient.

Regards,
Mario

1 Like