Here’s an update to a prior post, Creating New Presentations from Slide Masters Using OpenXML, about creating new presentation slides from the slide master. A reader commented about an error with layouts that had images in them and it came to my attention that my code completely ignored the images. Here’s an update:
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.
Tags: Office Open XML, PowerPoint
August 11, 2010 at 1:08 pm |
hi
iam using the above code ,but it shows an exception like “Sequence contains no matching element” please give me the explenation . it will be very helpfull for me.
thank you,
uday
March 13, 2012 at 6:35 pm |
Hi,
Thanks for this post. One issue I have with this is dealing with SmartArt objects. If any of my templates have any smartart objects the slide errors out due to missing objects.
I figure I need to add the Diagram* objects to the new slide but not sure how to go about doing it. Any help is appreciated.
Thanks,
Rich