Merging presentations into single presentation

1k Views Asked by At

I'm using GemBox.Presentation and I need to merge multiple PPTX files, combine them into a single PPTX file.

I tried this:

PresentationDocument presentation1 = PresentationDocument.Load("PowerPoint1.pptx");
PresentationDocument presentation2 = PresentationDocument.Load("PowerPoint2.pptx");

foreach (Slide slide2 in presentation2.Slides)
    presentation1.Slides.Add(slide2);

But I get the following error:

Item is already contained in some collection. Remove it from that collection first.
Parameter name: item

How can I merge several presentations into one presentation?

1

There are 1 best solutions below

0
On BEST ANSWER

Use one of the AddCopy methods instead of Add.
In other words, try this:

var presentation1 = PresentationDocument.Load("PowerPoint1.pptx");
var presentation2 = PresentationDocument.Load("PowerPoint2.pptx");

// Merge "PowerPoint2.pptx" file into "PowerPoint1.pptx" file.
var context = CloneContext.Create(presentation2, presentation1);
foreach (var slide in presentation2.Slides)
    presentation1.Slides.AddClone(slide, context);

// Save resulting "Merged PowerPoints.pptx" file.
presentation1.Save("Merged PowerPoints.pptx");

Also, you can refer to Cloning example.