0

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?

Mario Z
  • 4,328
  • 2
  • 24
  • 38
hertzogth
  • 236
  • 1
  • 2
  • 19
  • seems presentation2 names are duplicating with presentation one, you actually adding slide by slide, create in presentation document with no slides and loop both presentation1 and presentation2 slide by slide you add it to the empty presentation – Dickens A S Mar 31 '20 at 02:58
  • @DickensAS unfortunately, that doesn't work. I can't even add slides from the `presentation1` into an empty `PresentationDocument`. It throws the same error. – hertzogth Mar 31 '20 at 03:03

1 Answers1

1

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.

Mario Z
  • 4,328
  • 2
  • 24
  • 38