I'm looking for the best way to copy the slide layouts from one presentation to an existing presentation. I looked on the internet but found nothing on how to do this...
Hi,
You can use below function for your purpose.
here:
presentationPart: is your pptx file presentation part
layoutName: Its the layout name which you want ot insert. Please take a note that this a layout with "layoutName" should exist in your Slide Master layout list.
public static Slide InsertSlide(PresentationPart presentationPart, string layoutName) { SlideLayout sldlayout = new SlideLayout { layOutName = layoutName }; if (presentationPart.Presentation.SlideIdList == null) { presentationPart.Presentation.SlideIdList = new SlideIdList(); } uint num = 0x100; num += Convert.ToUInt32(presentationPart.Presentation.SlideIdList.Count<OpenXmlElement>()); Slide slide = new Slide(new OpenXmlElement[] { new CommonSlideData(new OpenXmlElement[] { new ShapeTree() }) }); SlidePart openXmlPart = presentationPart.AddNewPart<SlidePart>(); slide.Save(openXmlPart); SlideMasterPart part2 = presentationPart.SlideMasterParts.First<SlideMasterPart>(); SlideLayoutPart part = part2.SlideLayoutParts.SingleOrDefault<SlideLayoutPart>(new Func<SlideLayoutPart, bool>(sldlayout.IslayoutExist)); openXmlPart.AddPart<SlideLayoutPart>(part); openXmlPart.Slide.CommonSlideData = (CommonSlideData)part2.SlideLayoutParts.SingleOrDefault<SlideLayoutPart>(new Func<SlideLayoutPart, bool>(sldlayout.IslayoutExist)).SlideLayout.CommonSlideData.Clone(); SlideId id = presentationPart.Presentation.SlideIdList.AppendChild<SlideId>(new SlideId()); id.Id = num; id.RelationshipId = presentationPart.GetIdOfPart(openXmlPart); return GetSlide(presentationPart, id.RelationshipId); }private class SlideLayout { public string layOutName; public bool IslayoutExist(SlideLayoutPart layout) { return layout.SlideLayout.CommonSlideData.Name.Value.Equals(this.layOutName); } }
I'm having trouble seeing how the sample code above will help copy a slidelayout from
presentation A (template) to presentation B (blank or existing presentation). If you could give me some explanation it would help a lot.
Attached is the sample application for adding 3 slides.
Hope this will help.
Pranay
This post might also be helpful for you.