none
Static method to copy slide from one pptx to another RRS feed

  • Question

  • I have 3 .pptx files and each contains only 1 slide each.

    The 1st is blank and is my template, the other 2 pptx docs have lots of content on each of those slides.

    I simply want to copy a slide from one file to another.

    I've read tons of articles and none of the methods work.

    Please help!

    Thanks,
    Jon

    UPDATE:

    I found a method while searching that works but every time I open the file I still get that corrupted message.

    I click OK to repair and my slides and content are there, but still getting that error?

    pptxDoc is an instance of the PresentationDocument class FYI. 

    public SlidePart CloneSlidePartWithImagesAndCharts(SlidePart slideTemplate) { PresentationPart presentationPart = pptxDoc.PresentationPart; int i = presentationPart.SlideParts.Count(); //Create a new slide part in the presentation. SlidePart newSlidePart = presentationPart.AddNewPart<SlidePart>("newSlide" + i); i++; //Add the source slide content into the new slide. newSlidePart.FeedData(slideTemplate.GetStream(FileMode.Open)); //Make sure the new slide references the proper slide layout. newSlidePart.AddPart(slideTemplate.SlideLayoutPart, slideTemplate.GetIdOfPart(slideTemplate.SlideLayoutPart)); // copy the image parts foreach (ImagePart ipart in slideTemplate.ImageParts) { ImagePart newipart = newSlidePart.AddImagePart(ipart.ContentType, slideTemplate.GetIdOfPart(ipart)); newipart.FeedData(ipart.GetStream()); } // copy the chart parts foreach (ChartPart cpart in slideTemplate.ChartParts) { ChartPart newcpart = newSlidePart.AddNewPart<ChartPart>(slideTemplate.GetIdOfPart(cpart)); newcpart.FeedData(cpart.GetStream()); // copy the embedded excel file EmbeddedPackagePart epart = newcpart.AddEmbeddedPackagePart(cpart.EmbeddedPackagePart.ContentType); epart.FeedData(cpart.EmbeddedPackagePart.GetStream()); // link the excel to the chart //(((newcpart.ChartSpace)).Exte).Id = newcpart.GetIdOfPart(epart); D.Charts.ExternalData externalData = new D.Charts.ExternalData(); externalData.Id = newcpart.GetIdOfPart(epart); newcpart.ChartSpace.Append(externalData); newcpart.ChartSpace.Save(); } //Get the list of slide ids. SlideIdList slideIdList = presentationPart.Presentation.SlideIdList; //Deternmine where to add the next slide (find max number of slides). uint maxSlideId = 1; SlideId prevSlideId = null; foreach (SlideId slideId in slideIdList.ChildElements) { if (slideId.Id > maxSlideId) { maxSlideId = slideId.Id; prevSlideId = slideId; } } maxSlideId++; //Add the new slide at the end of the deck. SlideId newSlideId = slideIdList.InsertAfter(new SlideId(), prevSlideId); //Make sure the id and relid are set appropriately. newSlideId.Id = maxSlideId; newSlideId.RelationshipId = presentationPart.GetIdOfPart(newSlidePart); newSlidePart.Slide.Save(); return newSlidePart; }




    • Edited by JonC123 Thursday, June 28, 2012 7:27 PM
    Thursday, June 28, 2012 5:43 PM

Answers

All replies

  • Hi JonC123,

    Thanks for posting in the MSDN Forum.

    It's based on my experience that such statement will let to error:

    newSlidePart.AddPart(slideTemplate.SlideLayoutPart, slideTemplate.GetIdOfPart(slideTemplate.SlideLayoutPart));

    Openxml can't simply clone xmlnodes via this way. You need create a new node in you new xml element and add content form source step by step.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Monday, July 2, 2012 7:09 AM
    Moderator
  • Hi Tom_Xu and thanks for your response and help.

    I've open that document and let PowerPoint repair the file then I saved this file as a different name, then I used the openxml diff tool against the 2 copies of the file and it appears the only thing that is changed when PowerPoint repairs the file is to add another slideMaster for the slide.

    So you have a code example that shows the correct way to do the above?

    Thanks,
    Jon

    Monday, July 2, 2012 1:24 PM
  • Hi JonC123,

    I think this might help you out, you will learn how to move a slide to new presentation, the similar action will used in your issue. http://msdn.microsoft.com/en-us/library/cc850839

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us


    Tuesday, July 3, 2012 7:14 AM
    Moderator