private static uint CreateId(SlideIdList slideIdList)
{
uint currentId = 0;
foreach (SlideId slideId in slideIdList)
{
if (slideId.Id > currentId)
{
currentId = slideId.Id;
}
}
return ++currentId;
}
private static uint CreateId(SlideMasterIdList slideMasterIdList)
{
uint currentId = 0;
foreach (SlideMasterId masterId in slideMasterIdList)
{
if (masterId.Id > currentId)
{
currentId = masterId.Id;
}
}
return ++currentId;
}
public static void Copy(string sourcePresentationStream, uint copiedSlidePosition, string destPresentationStream)
{
using (var destDoc = PresentationDocument.Open(destPresentationStream, true))
{
var sourceDoc = PresentationDocument.Open(sourcePresentationStream, false);
var destPresentationPart = destDoc.PresentationPart;
var destPresentation = destPresentationPart.Presentation;
var sourcePresentationPart = sourceDoc.PresentationPart;
var sourcePresentation = sourcePresentationPart.Presentation;
int copiedSlideIndex = (int)--copiedSlidePosition;
int countSlidesInSourcePresentation = sourcePresentation.SlideIdList.Count();
if (copiedSlideIndex < 0 || copiedSlideIndex >= countSlidesInSourcePresentation)
throw new ArgumentOutOfRangeException(nameof(copiedSlidePosition));
SlideId copiedSlideId = sourcePresentationPart.Presentation.SlideIdList.ChildElements[copiedSlideIndex] as SlideId;
SlidePart copiedSlidePart = sourcePresentationPart.GetPartById(copiedSlideId.RelationshipId) as SlidePart;
SlidePart addedSlidePart = destPresentationPart.AddPart<SlidePart>(copiedSlidePart);
NotesSlidePart noticePart = addedSlidePart.GetPartsOfType<NotesSlidePart>().FirstOrDefault();
if (noticePart != null)
{
addedSlidePart.DeletePart(noticePart);
}
SlideMasterPart addedSlideMasterPart = destPresentationPart.AddPart(addedSlidePart.SlideLayoutPart.SlideMasterPart);
// Create new slide ID
SlideId slideId = new SlideId
{
Id = CreateId(destPresentation.SlideIdList),
RelationshipId = destDoc.PresentationPart.GetIdOfPart(addedSlidePart)
};
destPresentation.SlideIdList.Append(slideId);
// Create new master slide ID
uint masterId = CreateId(destPresentation.SlideMasterIdList);
SlideMasterId slideMaterId = new SlideMasterId
{
Id = masterId,
RelationshipId = destDoc.PresentationPart.GetIdOfPart(addedSlideMasterPart)
};
destDoc.PresentationPart.Presentation.SlideMasterIdList.Append(slideMaterId);
destDoc.PresentationPart.Presentation.Save();
// Make sure that all slide layouts have unique ids.
foreach (SlideMasterPart slideMasterPart in destDoc.PresentationPart.SlideMasterParts)
{
foreach (SlideLayoutId slideLayoutId in slideMasterPart.SlideMaster.SlideLayoutIdList)
{
masterId++;
slideLayoutId.Id = masterId;
}
slideMasterPart.SlideMaster.Save();
}
destDoc.PresentationPart.Presentation.Save();
destDoc.Close();
sourceDoc.Close();
}
//sourcePresentationStream.Close();
// destPresentationStream.Close();
}