How to: Insert a new slide into a presentation (Open XML SDK)
Last modified: July 27, 2012
Applies to: Office 2013 | Open XML
In this article
Getting a PresentationDocument Object
Basic Presentation Document Structure
How the Sample Code Works
Sample Code
This topic shows how to use the classes in the Open XML SDK 2.5 to insert a new slide into a presentation programmatically.
The following assembly directives are required to compile the code in this topic.
In the Open XML SDK, the PresentationDocument class represents a presentation document package. To work with a presentation document, first create an instance of the PresentationDocument class, and then work with that instance. To create the class instance from the document call the Open(String, Boolean) method that uses a file path, and a Boolean value as the second parameter to specify whether a document is editable. To open a document for read/write, specify the value true for this parameter as shown in the following using statement. In this code segment, the presentationFile parameter is a string that represents the full path for the file from which you want to open the document.
The using statement provides a recommended alternative to the typical .Open, .Save, .Close sequence. It ensures that the Dispose method (internal method used by the Open XML SDK to clean up resources) is automatically called when the closing brace is reached. The block that follows the using statement establishes a scope for the object that is created or named in the using statement, in this case presentationDocument.
The basic document structure of a PresentationML document consists of the main part that contains the presentation definition. The following text from the ISO/IEC 29500 specification introduces the overall form of a PresentationML package.
A PresentationML package’s main part starts with a presentation root element. That element contains a presentation, which, in turn, refers to a slide list, a slide master list, a notes master list, and a handout master list. The slide list refers to all of the slides in the presentation; the slide master list refers to the entire slide masters used in the presentation; the notes master contains information about the formatting of notes pages; and the handout master describes how a handout looks.A handout is a printed set of slides that can be provided to an audience for future reference.As well as text and graphics, each slide can contain comments and notes, can have a layout, and can be part of one or more custom presentations. (A comment is an annotation intended for the person maintaining the presentation slide deck. A note is a reminder or piece of text intended for the presenter or the audience.)Other features that a PresentationML document can include the following: animation, audio, video, and transitions between slides.A PresentationML document is not stored as one large body in a single part. Instead, the elements that implement certain groupings of functionality are stored in separate parts. For example, all comments in a document are stored in one comment part while each slide has its own part.© ISO/IEC29500: 2008.The following XML code segment represents a presentation that contains two slides denoted by the Id’s 267 and 256.
<p:presentation xmlns:p="…" … >
<p:sldMasterIdLst>
<p:sldMasterId
xmlns:rel="http://…/relationships" rel:id="rId1"/>
</p:sldMasterIdLst>
<p:notesMasterIdLst>
<p:notesMasterId
xmlns:rel="http://…/relationships" rel:id="rId4"/>
</p:notesMasterIdLst>
<p:handoutMasterIdLst>
<p:handoutMasterId
xmlns:rel="http://…/relationships" rel:id="rId5"/>
</p:handoutMasterIdLst>
<p:sldIdLst>
<p:sldId id="267"
xmlns:rel="http://…/relationships" rel:id="rId2"/>
<p:sldId id="256"
xmlns:rel="http://…/relationships" rel:id="rId3"/>
</p:sldIdLst>
<p:sldSz cx="9144000" cy="6858000"/>
<p:notesSz cx="6858000" cy="9144000"/>
</p:presentation>
Using the Open XML SDK 2.5, you can create document structure and content using strongly-typed classes that correspond to PresentationML elements. You can find these classes in the DocumentFormat.OpenXml.Presentation namespace. The following table lists the class names of the classes that correspond to the sld, sldLayout, sldMaster, and notesMaster elements:
PresentationML Element | Open XML SDK 2.5 Class | Description |
|---|---|---|
sld | Presentation Slide. It is the root element of SlidePart. | |
sldLayout | Slide Layout. It is the root element of SlideLayoutPart. | |
sldMaster | Slide Master. It is the root element of SlideMasterPart. | |
notesMaster | Notes Master (or handoutMaster). It is the root element of NotesMasterPart. |
The sample code consists of two overloads of the InsertNewSlide method. The first overloaded method takes three parameters: the full path to the presentation file to which to add a slide, an integer that represents the zero-based slide index position in the presentation where to add the slide, and the string that represents the title of the new slide. It opens the presentation file as read/write, gets a PresentationDocument object, and then passes that object to the second overloaded InsertNewSlide method, which performs the insertion.
The second overloaded InsertNewSlide method creates a new Slide object, sets its properties, and then inserts it into the slide order in the presentation. The first section of the method creates the slide and sets its properties.
The next section of the second overloaded InsertNewSlide method adds a title shape to the slide and sets its properties, including its text
The next section of the second overloaded InsertNewSlide method adds a body shape to the slide and sets its properties, including its text.
The final section of the second overloaded InsertNewSlide method creates a new slide part, finds the specified index position where to insert the slide, and then inserts it and saves the modified presentation.
By using the sample code you can add a new slide to an existing presentation. In your program, you can use the following call to the InsertNewSlide method to add a new slide to a presentation file named "Myppt10.pptx," with the title "My new slide," at position 1.
After you have run the program, the new slide would show up as the second slide in the presentation.
The following is the complete sample code in both C# and Visual Basic.
|
Contribute to this article Want to edit or suggest changes to this content? You can edit and submit changes to this article using GitHub. |