如何在powerpoint presentation中得到所有的形状? (我需要在 OpenXml 中)

How to get all the shapes in powerpoint presentation? (I need in OpenXml)

我需要从所有幻灯片中获取活动演示文稿中的所有形状,包括 C# 中分组项目中的形状。

我需要 List 或 Array(Shape) 中返回的所有形状。

您可以通过Shapes property. Similarly you can enumerate child shapes via GroupItems 属性 枚举幻灯片的形状(仅适用于msoGroup 形状类型)。把它们放在一起:

public static IEnumerable<Shape> EnumerateShapes(Presentation presentation)
{
    return presentation.Slides.Cast<Slide>().SelectMany(slide =>
        EnumerateShapes(slide.Shapes.Cast<Shape>()));
}

public static IEnumerable<Shape> EnumerateShapes(IEnumerable<Shape> shapes)
{
    foreach (Shape shape in shapes)
    {
        yield return shape;
        if (shape.Type == Microsoft.Office.Core.MsoShapeType.msoGroup)
        {
            foreach (var subShape in EnumerateShapes(shape.GroupItems.Cast<Shape>()))
                yield return subShape;
        }
    }
}

请注意,这种递归是有代价的,也许将上述方法转换为 non-recursive one 是明智的。