如何:更改演示文稿中的形状的填充颜色
上次修改时间: 2010年10月14日
适用范围: Excel 2010 | Office 2010 | PowerPoint 2010 | Word 2010
本文内容
获取 Presentation 对象
形状树的结构
示例代码的工作方式
示例代码
本主题演示如何使用 Open XML SDK 2.0 for Microsoft Office 中的类以编程方式更改演示文稿中第一张幻灯片上的形状的填充颜色。
编译本主题中的代码需要使用以下程序集指令。
using DocumentFormat.OpenXml.Presentation;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml;
using Drawing = DocumentFormat.OpenXml.Drawing;
Imports DocumentFormat.OpenXml.Presentation
Imports DocumentFormat.OpenXml.Packaging
Imports DocumentFormat.OpenXml
Imports Drawing = DocumentFormat.OpenXml.Drawing
获取 Presentation 对象
在 Open XML SDK 中,PresentationDocument 类表示演示文稿文档包。若要处理演示文稿文档,请首先创建 PresentationDocument 类的实例,然后处理该实例。若要从文档中创建类实例,请调用使用文件路径的 Open 方法,并以布尔值作为第二个参数,来指定文档是否可编辑。若要打开文档进行读/写,请为此参数指定值 true,如以下的 using 语句所示。在该代码中,file 参数是一个字符串,表示要从中打开该文档的文件的路径。
using (PresentationDocument ppt = PresentationDocument.Open(docName, true))
{
// Insert other code here.
}
Using ppt As PresentationDocument = PresentationDocument.Open(docName, True)
' Insert other code here.
End Using
using 语句提供典型 .Open, .Save, .Close 序列的建议备选序列。它确保在遇到右大括号时会自动调用 Dispose 方法(Open XML SDK 用来清理资源的内部方法)。using 语句后面的块为 using 语句中创建或指定的对象设定范围,在此示例中这个范围就是 ppt。
形状树的结构
PresentationML 文档的基本文档结构由许多部件构成,其中包括形状树 (sp Tree) 元素。
ISO/IEC 29500(该链接可能指向英文页面) 规范中的以下文本介绍了 PresentationML 包的整体形式。
此元素指定幻灯片中的所有形状。此处包含可在给定幻灯片中引用的所有形状(分组或不分组)。由于幻灯片中的大多数对象都是形状,因此这代表幻灯片中的大多数内容。文本和效果会附加到 spTree 元素包含的形状中。
[示例:请考虑以下 PresentationML 幻灯片
<p:sld>
<p:cSld>
<p:spTree>
<p:nvGrpSpPr>
..
</p:nvGrpSpPr>
<p:grpSpPr>
..
</p:grpSpPr>
<p:sp>
..
</p:sp>
</p:spTree>
</p:cSld>
..
</p:sld>
在以上示例中,形状树为此幻灯片指定所有形状属性。示例结束]
© ISO/IEC29500: 2008。
下表列出了形状树的子元素以及每个元素的说明。
元素 |
说明 |
---|---|
cxnSp |
连接形状 |
extLst |
带有修改标记的扩展名列表 |
graphicFrame |
图片框 |
grpSp |
组形状 |
grpSpPr |
组形状属性 |
nvGrpSpPr |
组形状的非可视属性 |
pic |
图片 |
sp |
形状 |
以下 XML 架构片段定义此元素的内容。
<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>
示例代码的工作方式
在 using 语句中打开演示文稿文件进行读/写访问后,代码将从演示文稿文档中获取演示文稿部件。然后,它获取第一张幻灯片的关系 ID,然后从该关系 ID 中获取幻灯片部件。
备注
测试文件必须将填充形状作为第一张幻灯片上的第一个形状。
using (PresentationDocument ppt = PresentationDocument.Open(docName, true))
{
// Get the relationship ID of the first slide.
PresentationPart part = ppt.PresentationPart;
OpenXmlElementList slideIds = part.Presentation.SlideIdList.ChildElements;
string relId = (slideIds[0] as SlideId).RelationshipId;
// Get the slide part from the relationship ID.
SlidePart slide = (SlidePart)part.GetPartById(relId);
Using ppt As PresentationDocument = PresentationDocument.Open(docName, True)
' Get the relationship ID of the first slide.
Dim part As PresentationPart = ppt.PresentationPart
Dim slideIds As OpenXmlElementList = part.Presentation.SlideIdList.ChildElements
Dim relId As String = CType(slideIds(0), SlideId).RelationshipId
' Get the slide part from the relationship ID.
Dim slide As SlidePart = CType(part.GetPartById(relId), SlidePart)
接下来,代码获取包含要更改填充颜色的形状的形状树,并获取形状树中的第一个形状。然后,代码获取该形状的样式并填充该样式的引用,再为该形状分配一个新的填充颜色。最后,该代码保存修改过的演示文稿。
if (slide != null)
{
// Get the shape tree that contains the shape to change.
ShapeTree tree = slide.Slide.CommonSlideData.ShapeTree;
// Get the first shape in the shape tree.
Shape shape = tree.GetFirstChild<Shape>();
if (shape != null)
{
// Get the style of the shape.
ShapeStyle style = shape.ShapeStyle;
// Get the fill reference.
Drawing.FillReference fillRef = style.FillReference;
// Set the fill color to SchemeColor Accent 6;
fillRef.SchemeColor = new Drawing.SchemeColor();
fillRef.SchemeColor.Val = Drawing.SchemeColorValues.Accent6;
// Save the modified slide.
slide.Slide.Save();
}
}
If (Not (slide) Is Nothing) Then
' Get the shape tree that contains the shape to change.
Dim tree As ShapeTree = slide.Slide.CommonSlideData.ShapeTree
' Get the first shape in the shape tree.
Dim shape As Shape = tree.GetFirstChild(Of Shape)()
If (Not (shape) Is Nothing) Then
' Get the style of the shape.
Dim style As ShapeStyle = shape.ShapeStyle
' Get the fill reference.
Dim fillRef As Drawing.FillReference = style.FillReference
' Set the fill color to SchemeColor Accent 6;
fillRef.SchemeColor = New Drawing.SchemeColor
fillRef.SchemeColor.Val = Drawing.SchemeColorValues.Accent6
' Save the modified slide.
slide.Slide.Save()
End If
End If
示例代码
下面是可用于更改演示文稿中形状的填充颜色的完整代码示例。在您的程序中,您可以调用方法 SetPPTShapeColor,以使用以下调用来更改"Myppt3.pptx"文件中的填充颜色。
string docName = @"C:\Users\Public\Documents\Myppt3.pptx";
SetPPTShapeColor(docName);
Dim docName As String = "C:\Users\Public\Documents\Myppt3.pptx"
SetPPTShapeColor(docName)
运行程序后,可查看"Myppt3.pptx"文件中填充颜色的变化。
// Change the fill color of a shape.
// The test file must have a filled shape as the first shape on the first slide.
public static void SetPPTShapeColor(string docName)
{
using (PresentationDocument ppt = PresentationDocument.Open(docName, true))
{
// Get the relationship ID of the first slide.
PresentationPart part = ppt.PresentationPart;
OpenXmlElementList slideIds = part.Presentation.SlideIdList.ChildElements;
string relId = (slideIds[0] as SlideId).RelationshipId;
// Get the slide part from the relationship ID.
SlidePart slide = (SlidePart)part.GetPartById(relId);
if (slide != null)
{
// Get the shape tree that contains the shape to change.
ShapeTree tree = slide.Slide.CommonSlideData.ShapeTree;
// Get the first shape in the shape tree.
Shape shape = tree.GetFirstChild<Shape>();
if (shape != null)
{
// Get the style of the shape.
ShapeStyle style = shape.ShapeStyle;
// Get the fill reference.
Drawing.FillReference fillRef = style.FillReference;
// Set the fill color to SchemeColor Accent 6;
fillRef.SchemeColor = new Drawing.SchemeColor();
fillRef.SchemeColor.Val = Drawing.SchemeColorValues.Accent6;
// Save the modified slide.
slide.Slide.Save();
}
}
}
}
' Change the fill color of a shape.
' The test file must have a filled shape as the first shape on the first slide.
Public Sub SetPPTShapeColor(ByVal docName As String)
Using ppt As PresentationDocument = PresentationDocument.Open(docName, True)
' Get the relationship ID of the first slide.
Dim part As PresentationPart = ppt.PresentationPart
Dim slideIds As OpenXmlElementList = part.Presentation.SlideIdList.ChildElements
Dim relId As String = TryCast(slideIds(0), SlideId).RelationshipId
' Get the slide part from the relationship ID.
Dim slide As SlidePart = DirectCast(part.GetPartById(relId), SlidePart)
If slide IsNot Nothing Then
' Get the shape tree that contains the shape to change.
Dim tree As ShapeTree = slide.Slide.CommonSlideData.ShapeTree
' Get the first shape in the shape tree.
Dim shape As Shape = tree.GetFirstChild(Of Shape)()
If shape IsNot Nothing Then
' Get the style of the shape.
Dim style As ShapeStyle = shape.ShapeStyle
' Get the fill reference.
Dim fillRef As Drawing.FillReference = style.FillReference
' Set the fill color to SchemeColor Accent 6;
fillRef.SchemeColor = New Drawing.SchemeColor()
fillRef.SchemeColor.Val = Drawing.SchemeColorValues.Accent6
' Save the modified slide.
slide.Slide.Save()
End If
End If
End Using
End Sub