Working with slide layouts

This topic discusses the Open XML SDK for Office SlideLayout class and how it relates to the Open XML File Format PresentationML schema.

Slide Layouts in PresentationML

The ISO/IEC 29500 specification describes the Open XML PresentationML <sldLayout> element used to represent slide layouts in a PresentationML document as follows:

This element specifies an instance of a slide layout. The slide layout contains in essence a template slide design that can be applied to any existing slide. When applied to an existing slide all corresponding content should be mapped to the new slide layout.

The <sldLayout> element is the root element of the PresentationML Slide Layout part. For more information about the overall structure of the parts and elements that make up a PresentationML document, see Structure of a PresentationML Document.

The following table lists the child elements of the <sldLayout> element used when working with slide layouts and the Open XML SDK classes that correspond to them.

PresentationML Element Open XML SDK Class
<clrMapOvr> ColorMapOverride
<cSld> CommonSlideData
<extLst> ExtensionListWithModification
<hf> HeaderFooter
<timing> Timing
<transition> Transition

The following table from the ISO/IEC 29500 specification describes the attributes of the <sldLayout> element.

Attributes Description
matchingName (Matching Name) Specifies a name to be used in place of the name attribute within the cSld element. This is used for layout matching in response to layout changes and template applications.
The possible values for this attribute are defined by the W3C XML Schema string datatype.
preserve (Preserve Slide Layout) Specifies whether the corresponding slide layout is deleted when all the slides that follow that layout are deleted. If this attribute is not specified then a value of false should be assumed by the generating application. This would mean that the slide would in fact be deleted if no slides within the presentation were related to it.
The possible values for this attribute are defined by the W3C XML Schema boolean datatype.
showMasterPhAnim (Show Master Placeholder Animations) Specifies whether or not to display animations on placeholders from the master slide.
The possible values for this attribute are defined by the W3C XML Schema boolean datatype.
showMasterSp (Show Master Shapes) Specifies if shapes on the master slide should be shown on slides or not.
The possible values for this attribute are defined by the W3C XML Schema boolean datatype.
type (Slide Layout Type) Specifies the slide layout type that is used by this slide.
The possible values for this attribute are defined by the ST_SlideLayoutType simple type (ยง19.7.15).
userDrawn (Is User Drawn) Specifies if the corresponding object has been drawn by the user and should thus not be deleted. This allows for the flagging of slides that contain user drawn data.
The possible values for this attribute are defined by the W3C XML Schema boolean datatype.

The Open XML SDK SlideLayout Class

The OXML SDK SlideLayout class represents the <sldLayout> element defined in the Open XML File Format schema for PresentationML documents. Use the SlideLayout class to manipulate individual <sldLayout> elements in a PresentationML document.

Classes that represent child elements of the <sldLayout> element and that are therefore commonly associated with the SlideLayout class are shown in the following list.

ColorMapOverride Class

The ColorMapOverride class corresponds to the <clrMapOvr> element. The following information from the ISO/IEC 29500 specification introduces the <clrMapOvr> element:

This element provides a mechanism with which to override the color schemes listed within the <ClrMap> element. If the <masterClrMapping> child element is present, the color scheme defined by the master is used. If the <overrideClrMapping> child element is present, it defines a new color scheme specific to the parent notes slide, presentation slide, or slide layout.

CommonSlideData Class

The CommonSlideData class corresponds to the <cSld> element. The following information from the ISO/IEC 29500 specification introduces the <cSld> element:

This element specifies a container for the type of slide information that is relevant to all of the slide types. All slides share a common set of properties that is independent of the slide type; the description of these properties for any particular slide is stored within the slide's <cSld> container. Slide data specific to the slide type indicated by the parent element is stored elsewhere.

The actual data in <cSld> describe only the particular parent slide; it is only the type of information stored that is common across all slides.

ExtensionListWithModification Class

The ExtensionListWithModification class corresponds to the <extLst>element. The following information from the ISO/IEC 29500 specification introduces the <extLst> element:

This element specifies the extension list with modification ability within which all future extensions of element type <ext> are defined. The extension list along with corresponding future extensions is used to extend the storage capabilities of the PresentationML framework. This allows for various new kinds of data to be stored natively within the framework.

Note

Using this extLst element allows the generating application to store whether this extension property has been modified.

HeaderFooter Class

The HeaderFooter class corresponds to the <hf> element. The following information from the ISO/IEC 29500 specification introduces the <hf> element:

This element specifies the header and footer information for a slide. Headers and footers consist of placeholders for text that should be consistent across all slides and slide types, such as a date and time, slide numbering, and custom header and footer text.

Timing Class

The Timing class corresponds to the <timing> element. The following information from the ISO/IEC 29500 specification introduces the <timing> element:

This element specifies the timing information for handling all animations and timed events within the corresponding slide. This information is tracked via time nodes within the <timing> element. More information on the specifics of these time nodes and how they are to be defined can be found within the Animation section of the PresentationML framework.

Transition Class

The Transition class corresponds to the <transition> element. The following information from the ISO/IEC 29500 specification introduces the <transition> element:

This element specifies the kind of slide transition that should be used to transition to the current slide from the previous slide. That is, the transition information is stored on the slide that appears after the transition is complete.

Working with the SlideLayout Class

As shown in the Open XML SDK code sample that follows, every instance of the SlideLayout class is associated with an instance of the SlideLayoutPart class, which represents a slide layout part, one of the required parts of a PresentationML presentation file package. Each SlideLayout class instance must also be associated with instances of the SlideMaster and Slide classes, which are in turn associated with similarly named required presentation parts, represented by the SlideMasterPart and SlidePartclasses.

The SlideLayout class, which represents the <sldLayout> element, is therefore also associated with a series of other classes that represent the child elements of the <sldLayout> element. Among these classes, as shown in the following code sample, are the CommonSlideData class, the ColorMapOverride class, the ShapeTree class, and the Shape class.

Open XML SDK Code Example

The following method from the article How to: Create a presentation document by providing a file name adds a new slide layout part to an existing presentation and creates an instance of an Open XML SDKSlideLayout class in the new slide layout part. The SlideLayout class constructor creates instances of the CommonSlideData class and the ColorMapOverride class. The CommonSlideData class constructor creates an instance of the ShapeTree class, whose constructor in turn creates additional class instances: an instance of the [NonVisualGroupShapeProperties (https://msdn.microsoft.com/library/office/documentformat.openxml.presentation.nonvisualgroupshapeproperties.aspx) class, an instance of the GroupShapeProperties class, and an instance of the Shape class.

The namespace represented by the letter P in the code is the [DocumentFormat.OpenXml.Presentation (https://msdn.microsoft.com/library/office/documentformat.openxml.presentation.aspx) namespace.

using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Drawing;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Presentation;
using P = DocumentFormat.OpenXml.Presentation;

static SlideLayoutPart CreateSlideLayoutPart(SlidePart slidePart1)
{
    SlideLayoutPart slideLayoutPart1 = slidePart1.AddNewPart<SlideLayoutPart>("rId1");
    SlideLayout slideLayout = new SlideLayout(
    new CommonSlideData(new ShapeTree(
      new P.NonVisualGroupShapeProperties(
      new P.NonVisualDrawingProperties() { Id = (UInt32Value)1U, Name = "" },
      new P.NonVisualGroupShapeDrawingProperties(),
      new ApplicationNonVisualDrawingProperties()),
      new GroupShapeProperties(new TransformGroup()),
      new P.Shape(
      new P.NonVisualShapeProperties(
        new P.NonVisualDrawingProperties() { Id = (UInt32Value)2U, Name = "" },
        new P.NonVisualShapeDrawingProperties(new ShapeLocks() { NoGrouping = true }),
        new ApplicationNonVisualDrawingProperties(new PlaceholderShape())),
      new P.ShapeProperties(),
      new P.TextBody(
        new BodyProperties(),
        new ListStyle(),
        new Paragraph(new EndParagraphRunProperties()))))),
    new ColorMapOverride(new MasterColorMapping()));
    slideLayoutPart1.SlideLayout = slideLayout;
    return slideLayoutPart1;
}