Chandraprakash
Chandraprakash

Reputation: 946

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

I need to get all the shapes in active presentation from all slides, including the shapes in grouped items in C#.

I need all shapes returned in List or Array(Shape).

enter image description here

Upvotes: 0

Views: 1307

Answers (1)

Peter Wolf
Peter Wolf

Reputation: 3830

You can enumerate shapes of a slide via Shapes property. Similarly you can enumerate child shapes via GroupItems property (only for msoGroup shape type). To put that together:

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;
        }
    }
}

Note that this kind of recursion comes at its cost and maybe it would be wise to convert the above method to non-recursive one.

Upvotes: 1

Related Questions