Search code examples
c#openxmlopenxml-sdkpresentationml

Adding SlideMasters and SlideLayouts to a Presentation


I am trying to create a presentation with six Slides, three Master Slides (MS-A, MS-B, and MS-C), and two Slide Layouts (SL-1 and SL-2). Think of the six Slides as two rows and three columns of Slides.

I want to create the first ‘row’ of slides all using a SlideMaster that reference Slide Layout 1. For example, Slide 1 (S1A) would be created using Master MS-A which references SL-1. Slide 2 (S1B) with MS-B that references SL-1, and Slide (S1C) with MS-C that references SL-1.

So far, this should be no real problem. However, I want to know if it is possible to build the second 'row' of Slides (S2A, S2B, and S2C) with Master Slides MS-A through MS-C where they reference Slide Layout SC-2, and without affecting the contents of the first row of Slides

If this is possible, can you provide me links / examples / pseudo code of how I would go about doing this?

I am using OpenXML 2.5, C#, Framework 4.5.


Solution

    • A presentation may have one or more slide masters (master).
    • Each master may have one or more slide layouts (layout).
    • Each layout is related to one-and-only-one master.
    • Each slide is related to one-and-only-one layout. There is no direct relationship of a slide to a master, only the indirect relationship through its layout.

    Not sure if that answers your question, I can't fully understand it the way you've formulated it. Slides are distinct from both masters and layouts. I'm not sure if you're saying you want six slides are six layouts, or both.

    Also I can't help you with implementation using C#, but its probably a good idea to get straight what you're trying to accomplish and whether it's possible first anyway :)

    If you wanted six slides, each based on one of six distinct layouts, each based pair-wise on one of three masters, it would go something like this in psuedo-code:

    m_1 = Presentation.add_master(M_1)
    m_2 = Presentation.add_master(M_2)
    m_3 = Presentation.add_master(M_3)
    
    l_1a = m_1.add_layout(L_1A)
    l_1b = m_1.add_layout(L_1B)
    l_2a = m_2.add_layout(L_2A)
    l_2b = m_2.add_layout(L_2B)
    l_3a = m_3.add_layout(L_3A)
    l_3b = m_3.add_layout(L_3B)
    
    slide_1 = Presentation.Slides.add_slide(based_on=l_1a)
    slide_2 = Presentation.Slides.add_slide(based_on=l_1b)
    slide_3 = Presentation.Slides.add_slide(based_on=l_2a)
    slide_4 = Presentation.Slides.add_slide(based_on=l_2b)
    slide_5 = Presentation.Slides.add_slide(based_on=l_3a)
    slide_6 = Presentation.Slides.add_slide(based_on=l_3b)