How I can clone one slide to new presentation ? RRS feed

  • Question

  • I prepared code :

    using (PresentationDocument presentationDocument = PresentationDocument.Open(OriginalPresentationFile, true)) { PresentationPart sourcePresPart = presentationDocument.PresentationPart; _slidesNumber = sourcePresPart.SlideParts.Count(); int j = 1;

    foreach (SlideId slideId in sourcePresPart.Presentation.SlideIdList) { int k = j - 1;

    PresentationDocument destinationPresentationDoc = PresentationDocument.Create(SlideNames[k], PresentationDocumentType.Presentation); PresentationPart destPresPart = destinationPresentationDoc.AddPresentationPart(); destPresPart.Presentation = new DocumentFormat.OpenXml.Presentation.Presentation(); if (destPresPart.Presentation.SlideIdList == null) destPresPart.Presentation.SlideIdList = new SlideIdList(); if (destPresPart.Presentation.SlideMasterIdList == null) destPresPart.Presentation.SlideMasterIdList = new SlideMasterIdList(); uint uniqueId = GetMaxSlideMasterId(destPresPart.Presentation.SlideMasterIdList); uint maxSlideId = GetMaxSlideId(destPresPart.Presentation.SlideIdList); SlidePart sp = (SlidePart)sourcePresPart.GetPartById(slideId.RelationshipId); relId = "rId" + j; // Add the slide part to the destination presentation. SlidePart destSlidePart = destPresPart.AddPart<SlidePart>(sp, relId); // The master part was added. Make sure the relationship is in place. SlideMasterPart destMasterPart = destSlidePart.SlideLayoutPart.SlideMasterPart; destPresPart.AddPart(destMasterPart); // Add slide master to slide master list uniqueId++; SlideMasterId newSlideMasterId = new SlideMasterId(); newSlideMasterId.RelationshipId = destPresPart.GetIdOfPart(destMasterPart); newSlideMasterId.Id = uniqueId++; // Add slide to slide list SlideId newSlideId = new SlideId(); newSlideId.RelationshipId = relId; newSlideId.Id = maxSlideId; destPresPart.Presentation.SlideMasterIdList.Append(newSlideMasterId); destPresPart.Presentation.SlideIdList.Append(newSlideId); destPresPart.Presentation.Save(); destinationPresentationDoc.Close(); j++; } // Close current presentation presentationDocument.Close(); // Close stream //stream.Close(); }

    It's work, but always I got broken presentations, and when I open it (one of presentations) on PowerPoint I get confirm for repair.
    Please, help, where I wrong (or maybe you can give me example) ?
    Tuesday, September 12, 2017 4:52 PM

All replies

  • Hi Dimitryus,

    you can try to refer code below to get an idea.

    public static class OpenXmlUtils
        public static IEnumerable<SlidePart> GetSlidePartsInOrder(this PresentationPart presentationPart)
            SlideIdList slideIdList = presentationPart.Presentation.SlideIdList;
            return slideIdList.ChildElements
                .Select(x => presentationPart.GetPartById(x.RelationshipId))
        public static SlidePart CloneSlide(this SlidePart templatePart)
            // find the presentationPart: makes the API more fluent
            var presentationPart = templatePart.GetParentParts()
            // clone slide contents
            Slide currentSlide = (Slide)templatePart.Slide.CloneNode(true);
            var slidePartClone = presentationPart.AddNewPart<SlidePart>();
            // copy layout part
            return slidePartClone;
        public static void AppendSlide(this PresentationPart presentationPart, SlidePart newSlidePart)
            SlideIdList slideIdList = presentationPart.Presentation.SlideIdList;
            // find the highest id
            uint maxSlideId = slideIdList.ChildElements
                .Max(x => x.Id.Value);
            // Insert the new slide into the slide list after the previous slide.
            var id = maxSlideId + 1;
            SlideId newSlideId = new SlideId();
            newSlideId.Id = id;
            newSlideId.RelationshipId = presentationPart.GetIdOfPart(newSlidePart);

    var presentationFile = "SomeFile.pptx";
    using (PresentationDocument presentationDocument 
        = PresentationDocument.Open(presentationFile, true))
        var presentationPart = presentationDocument.PresentationPart;
        var templatePart = presentationPart.GetSlidePartsInOrder().Last();
        var newSlidePart = templatePart.CloneSlide();

    it is just an example so you need to modify it according to your requirement.



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact

    Wednesday, September 13, 2017 3:28 AM