private static void InsertSlide(PresentationPart pPart, string layoutName, UInt32 slideId)
{
Slide slide = new Slide(new CommonSlideData(new ShapeTree()));
SlidePart sPart = pPart.AddNewPart<SlidePart>();
slide.Save(sPart);
SlideMasterPart smPart = pPart.SlideMasterParts.First();
SlideLayoutPart slPart = smPart.SlideLayoutParts.Single(kaark => kaark.SlideLayout.CommonSlideData.Name == layoutName);
//Add the layout part to the new slide from the slide master
sPart.AddPart<SlideLayoutPart>(slPart);
sPart.Slide.CommonSlideData = (CommonSlideData)smPart.SlideLayoutParts.Single(kaark => kaark.SlideLayout.CommonSlideData.Name == layoutName).SlideLayout.CommonSlideData.Clone();
using (Stream stream = slPart.GetStream())
{
sPart.SlideLayoutPart.FeedData(stream);
}
//UPDATED: Copy the images from the slide master layout to the new slide
foreach (ImagePart iPart in slPart.ImageParts)
{
ImagePart newImagePart = sPart.AddImagePart(iPart.ContentType, slPart.GetIdOfPart(iPart));
newImagePart.FeedData(iPart.GetStream());
}
SlideId newSlideId = pPart.Presentation.SlideIdList.AppendChild<SlideId>(new SlideId());
newSlideId.Id = slideId;
newSlideId.RelationshipId = pPart.GetIdOfPart(sPart);
}
After feeding the slide layout part data into the new slide’s layout part, the code then adds the new image parts to the slide from the slide master layout.