How to: Change the fill color of a shape in a presentation (Open XML SDK)
Last modified: July 27, 2012
Applies to: Office 2013 | Open XML
In this article
Getting a Presentation Object
The Structure of the Shape Tree
How the Sample Code Works
Sample Code
This topic shows how to use the classes in the Open XML SDK 2.5 to change the fill color of a shape on the first slide in 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 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, the file parameter is a string that represents the 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 ppt.
The basic document structure of a PresentationML document consists of a number of parts, among which is the Shape Tree (sp Tree) element.
The following text from the ISO/IEC 29500 specification introduces the overall form of a PresentationML package.
<p:sld>
<p:cSld>
<p:spTree>
<p:nvGrpSpPr>
..
</p:nvGrpSpPr>
<p:grpSpPr>
..
</p:grpSpPr>
<p:sp>
..
</p:sp>
</p:spTree>
</p:cSld>
..
</p:sld>
The following table lists the child elements of the Shape Tree along with the description of each.
Element | Description |
|---|---|
cxnSp | Connection Shape |
extLst | Extension List with Modification Flag |
graphicFrame | Graphic Frame |
grpSp | Group Shape |
grpSpPr | Group Shape Properties |
nvGrpSpPr | Non-Visual Properties for a Group Shape |
pic | Picture |
sp | Shape |
The following XML Schema fragment defines the contents of this element.
<complexType name="CT_GroupShape">
<sequence>
<element name="nvGrpSpPr" type="CT_GroupShapeNonVisual" minOccurs="1" maxOccurs="1"/>
<element name="grpSpPr" type="a:CT_GroupShapeProperties" minOccurs="1" maxOccurs="1"/>
<choice minOccurs="0" maxOccurs="unbounded">
<element name="sp" type="CT_Shape"/>
<element name="grpSp" type="CT_GroupShape"/>
<element name="graphicFrame" type="CT_GraphicalObjectFrame"/>
<element name="cxnSp" type="CT_Connector"/>
<element name="pic" type="CT_Picture"/>
</choice>
<element name="extLst" type="CT_ExtensionListModify" minOccurs="0" maxOccurs="1"/>
</sequence>
</complexType>
After opening the presentation file for read/write access in the using statement, the code gets the presentation part from the presentation document. Then it gets the relationship ID of the first slide, and gets the slide part from the relationship ID.
Note
|
|---|
|
The test file must have a filled shape as the first shape on the first slide. |
The code then gets the shape tree that contains the shape whose fill color is to be changed, and gets the first shape in the shape tree. It then gets the style of the shape and the fill reference of the style, and assigns a new fill color to the shape. Finally it saves the modified presentation.
Following is the complete sample code that you can use to change the fill color of a shape in a presentation. In your program, you can invoke the method SetPPTShapeColor to change the fill color in the file "Myppt3.pptx" by using the following call.
After running the program, examine the file "Myppt3.pptx" to see the change in the fill color.
|
Contribute to this article Want to edit or suggest changes to this content? You can edit and submit changes to this article using GitHub. |
Note