Replacing slide master in a presentation with another slide master RRS feed

  • Question

  • Hi

    I have a requirement where I need to replace the slide master of an presentation with the slide master of another presentation. Actually, we have a powerpoint document which we have standardised to use a particular slide master. However, there are quite a few old documents which have slide masters which are bit different to the standard template.

    I need to replace the slide masters of old documents with the one in new template. I need to do it programatically using Open XML. Any pointers on thsi would be helpful. As of now I have used following code but it does not works -

    // Apply a new theme to the presentation. 
            public static void ApplyThemeToPresentation()
                string presentationFile = @"C:\TestFile\POC\Destination.pptx";
                string themePresentation = @"C:\TestFile\POC\Source.pptx";
                using (PresentationDocument themeDocument = PresentationDocument.Open(themePresentation, false))
                using (PresentationDocument presentationDocument = PresentationDocument.Open(presentationFile, true))
                    ApplyThemeToPresentation(presentationDocument, themeDocument);
            // Apply a new theme to the presentation. 
            public static void ApplyThemeToPresentation(PresentationDocument presentationDocument, PresentationDocument themeDocument)
                // Get the presentation part of the presentation document.
                PresentationPart presentationPart = presentationDocument.PresentationPart;
                // Get the existing slide master part.
                SlideMasterPart slideMasterPart = presentationPart.SlideMasterParts.ElementAt(4);
                string relationshipId = presentationPart.GetIdOfPart(slideMasterPart);
                // Get the new slide master part.
                SlideMasterPart newSlideMasterPart = themeDocument.PresentationPart.SlideMasterParts.ElementAt(0);
                // Remove the existing theme part.
                // Remove the old slide master part.
                // Import the new slide master part, and reuse the old relationship ID.
                newSlideMasterPart = presentationPart.AddPart(newSlideMasterPart, relationshipId);
                // Change to the new theme part.
                Dictionary<string, SlideLayoutPart> newSlideLayouts = new Dictionary<string, SlideLayoutPart>();
                foreach (var slideLayoutPart in newSlideMasterPart.SlideLayoutParts)
                    newSlideLayouts.Add(GetSlideLayoutType(slideLayoutPart), slideLayoutPart);
                string layoutType = null;
                SlideLayoutPart newLayoutPart = null;
                // Insert the code for the layout for this example.
                string defaultLayoutType = "Title and Content";
                // Remove the slide layout relationship on all slides. 
                foreach (var slidePart in presentationPart.SlideParts)
                    layoutType = null;
                    if (slidePart.SlideLayoutPart != null)
                        // Determine the slide layout type for each slide.
                        layoutType = GetSlideLayoutType(slidePart.SlideLayoutPart);
                        // Delete the old layout part.
                    if (layoutType != null && newSlideLayouts.TryGetValue(layoutType, out newLayoutPart))
                        // Apply the new layout part.
                    //    newLayoutPart = newSlideLayouts[defaultLayoutType];
                    //    // Apply the new default layout part.
                    //    slidePart.AddPart(newLayoutPart);
            // Get the slide layout type.
            public static string GetSlideLayoutType(SlideLayoutPart slideLayoutPart)
                CommonSlideData slideData = slideLayoutPart.SlideLayout.CommonSlideData;
                // Remarks: If this is used in production code, check for a null reference.
                return slideData.Name;


    Any pointers would be much appreciated.

    Many Thanks

    Best Regards




    Monday, December 19, 2011 11:22 AM


  • Hi Amit,

    I see your code comes from this sample:

    The sample works for me. The reason the sample doesn't work for you I guess is that you retrieved a wrong SlideMasterPart.

    you should be aware of that the index shows in Master view isn't the same as in Open XML presentation part. You can confirm this by opening the presentation which has multiple masters in Open XML SDK. The way that guarantee you can retrieve the right SlideMasterPart is to specify the part via Name. Please check  the code below:

        SlideMasterPart slideMasterPart = presentationPart.SlideMasterParts.Where(s => s.ThemePart.Theme.Name == "Office Theme").First();

    Absolutely you can change the Name "Office Theme" to another master name to fit your requirement.

    I hope this helps.

    Calvin Gao[MSFT]
    MSDN Community Support | Feedback to us
    Tuesday, December 20, 2011 10:47 AM