Reputation: 946
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).
Upvotes: 0
Views: 1307
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