Creating New Presentations from Slide Masters Using OpenXML (Revisited)

by

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.

Advertisements

Tags: ,

2 Responses to “Creating New Presentations from Slide Masters Using OpenXML (Revisited)”

  1. uday Says:

    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

  2. Rich Says:

    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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: