如何:更改演示文稿中的形状的填充颜色 您所在的位置:网站首页 图标形状怎么设置颜色填充 如何:更改演示文稿中的形状的填充颜色

如何:更改演示文稿中的形状的填充颜色

2024-07-16 05:29| 来源: 网络整理| 查看: 265

更改演示文稿中的形状的填充颜色 项目11/16/2023

本主题演示如何使用 Open XML SDK 中的类以编程方式更改演示文稿中第一张幻灯片上形状的填充颜色。

获取 Presentation 对象

在 Open XML SDK 中,PresentationDocument 类表示演示文稿文档包。 若要处理演示文稿文档,请首先创建 PresentationDocument 类的实例,然后处理该实例。 若要从文档中创建类实例,请调用使用文件路径的 Open 方法,并以布尔值作为第二个参数,来指定文档是否可编辑。 若要打开文档进行读/写,请为此参数指定值 true,如以下的 using 语句所示。 在该代码中,file 参数是一个字符串,表示要从中打开该文档的文件的路径。

C# Visual Basic 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** 幻灯片

.. .. .. ..

在以上示例中,形状树为此幻灯片指定所有形状属性。 示例结束]

© ISO/IEC29500: 2008.

下表列出了形状树的子元素以及每个元素的说明。

元素 说明 cxnSp 连接形状 extLst 带有修改标记的扩展名列表 graphicFrame 图片框 grpSp 组形状 grpSpPr 组形状属性 nvGrpSpPr 组形状的非可视属性 Pic 图片 Sp 形状

以下 XML 架构片段定义此元素的内容。

示例代码的工作方式

在 using 语句中打开演示文稿文件进行读/写访问后,代码将从演示文稿文档中获取演示文稿部件。 然后,它获取第一张幻灯片的关系 ID,然后从该关系 ID 中获取幻灯片部件。

注意

[!注释] 测试文件必须将填充形状作为第一张幻灯片上的第一个形状。

C# Visual Basic 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)

接下来,代码获取包含要更改填充颜色的形状的形状树,并获取形状树中的第一个形状。 然后,代码获取该形状的样式并填充该样式的引用,再为该形状分配一个新的填充颜色。 最后,该代码保存修改过的演示文稿。

C# Visual Basic 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(); 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"文件中的填充颜色。

C# Visual Basic string docName = @"C:\Users\Public\Documents\Myppt3.pptx"; SetPPTShapeColor(docName); Dim docName As String = "C:\Users\Public\Documents\Myppt3.pptx" SetPPTShapeColor(docName)

运行程序后,可查看"Myppt3.pptx"文件中填充颜色的变化。

C# Visual Basic using DocumentFormat.OpenXml.Packaging; using DocumentFormat.OpenXml.Presentation; using Drawing = DocumentFormat.OpenXml.Drawing; SetPPTShapeColor(args[0]); // Change the fill color of a shape. // The test file must have a filled shape as the first shape on the first slide. static void SetPPTShapeColor(string docName) { using (PresentationDocument ppt = PresentationDocument.Open(docName, true)) { // Get the relationship ID of the first slide. PresentationPart presentationPart = ppt.PresentationPart ?? ppt.AddPresentationPart(); SlideIdList slideIdList = presentationPart.Presentation.SlideIdList ?? presentationPart.Presentation.AppendChild(new SlideIdList()); SlideId? slideId = slideIdList.GetFirstChild(); if (slideId is not null) { string? relId = slideId.RelationshipId; if (relId is not null) { // Get the slide part from the relationship ID. SlidePart slidePart = (SlidePart)presentationPart.GetPartById(relId); if (slidePart is not null && slidePart.Slide is not null && slidePart.Slide.CommonSlideData is not null && slidePart.Slide.CommonSlideData.ShapeTree is not null) { // Get the shape tree that contains the shape to change. ShapeTree tree = slidePart.Slide.CommonSlideData.ShapeTree; // Get the first shape in the shape tree. Shape? shape = tree.GetFirstChild(); if (shape is not null && shape.ShapeStyle is not null && shape.ShapeStyle.FillReference is not 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. slidePart.Slide.Save(); } } } } } } Imports DocumentFormat.OpenXml Imports DocumentFormat.OpenXml.Packaging Imports DocumentFormat.OpenXml.Presentation Imports Drawing = DocumentFormat.OpenXml.Drawing Module Program Sub Main(args As String()) End Sub ' 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 End Module 另请参阅 Open XML SDK 类库参考


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有