填充特定形状 - 带颜色的 ShapeStyle C# PowerPoint OpenXML

Fill a specific Shape - ShapeStyle with color C# PowerPoint OpenXML

我想更改幻灯片中几个形状的填充颜色。

我在 msdn 上找到了这个教程,但它只提到了一个非常具体的场景(第一张幻灯片的第一个形状,在只有一个形状的演示文稿中)=> Link To The MSDN Tutorial

所以,也许我并不真正理解整个事情,但在我看来,当您尝试动态访问 ShapeTree 元素时,您会得到 "nullReference exception" 或 "not define as an instance of an object",在尝试访问 ShapeStyle 属性 时(实际上使用 FirstChild() 示例)。

所以我尝试通过遍历元素按如下方式访问它,但都没有成功。

 ShapeTree tree = slide.Slide.CommonSlideData.ShapeTree;
            for (var i = 0; i <= 30; i++)
            {
                try
                {
                    DRAW.FillReference fillRef = tree.ElementAt(i).GetFirstChild<DocumentFormat.OpenXml.Presentation.Shape>().ShapeStyle.FillReference;


                    fillRef.SchemeColor = new DRAW.SchemeColor();
                    fillRef.SchemeColor.Val = DRAW.SchemeColorValues.Accent6;

                }
                catch (Exception e)
                {
                //Null reference Excpetion on the fillRef initialization, does the same if I cast with DocumentFormat.OpenXml.Drawing.Shape        
                }

           }

我的目标很简单,我想通过识别它的 innerText 来更改形状的填充颜色。

提前致谢!

干杯

问题已在此处得到解答 -> https://social.msdn.microsoft.com/Forums/en-US/292dc670-7a9c-4df5-aa9f-d221b73b54d9/fill-a-specific-shape-shapestyle-with-color-c-powerpoint-openxml?forum=oxmlsdk