DEV Community

Cover image for How to understand and align shapes in powerpoint
Antoine
Antoine

Posted on

How to understand and align shapes in powerpoint

Photo by Eaters Collective on Unsplash

The issue is simple.
I have a powerpoint whith shapes.

Alt Text

Using Open Xml SDK, i need to analyze a powerpoint slide containing shapes, and align them in an json.

Each shape has an offset property which defines its position (from the top left corner), and an extent property which defines its size.
Alt Text

Alt Text

We will use a column object to keep track of which shapes belongs to which column.

So, by just ordering shapes by their Offset.X property, we can sort the shapes on the X axis.
The first shape will be the current shape.
Then, using a loop, we can define if the next item is inside the current shape, if projected on X axis. If it's inside, it will be in the same column. If not, we can create a new column, and set the item as the current one.

In the example, A1 will be the current item. Then, A4 is the next one.

A1.Offset.X + A1.Extent.X > A4.Offset.X

Then A1 and A4 belongs to the same column. Then, A2 is the next item.

A1.Offset.X + A1.Extent.X < A2.Offset.X

Thus, we create a new column, and A2 is the current item, etc ... .

At the end we should have Alt Text

If we want to solve issues, when shapes are not here (for example, what if A4 does not exist ?). We can fill the column with fake shapes, and remove them when adding a shape.

Hope this helps !

Top comments (0)