Ryan Abbott
Ryan Abbott

Reputation: 5417

How to get the Position & Dimension of a Shape in Powerpoint?

I'm playing around with OpenXmlSDK to see if it's a viable solution for our Powerpoint needs. One thing that is required is the ability to position shapes in the Powerpoint. I've been searching around for a way to get the position of a Shape, but have only come across is the MSDN "How To" http://msdn.microsoft.com/en-us/library/cc850828.aspx and a Position class (but no way to get it from a Shape) http://msdn.microsoft.com/en-us/library/office/documentformat.openxml.wordprocessing.position%28v=office.14%29.aspx.

How do I do something like:

PresentationDocument presentationDocument =  PresentationDocument.Open("C:\\MyDoc.pptx", true);
IdPartPair pp = presentationDocument.PresentationPart.SlideParts.First().Parts.FirstOrDefault();
var shape = pp.OpenXmlPart;
// How do I get the position and dimensions?

Upvotes: 5

Views: 4288

Answers (3)

Unknown Pjatk
Unknown Pjatk

Reputation: 11

Sometimes ShapeProperties is not displayed as a Shape property, you must write

var sP = ((DocumentFormat.OpenXml.Presentation.Shape)shape).ShapeProperties;

After you can use Transform2D and find coordinates as Deunz wrote.

Upvotes: 0

Deunz
Deunz

Reputation: 1941

You have 2 variables for the dimension of the shape : - Offset gives the position of the top corner of your shape - Extents gives the size off your shape

shape.ShapeProperties.Transform2D.Offset.X //gives the x position of top left corner
shape.ShapeProperties.Transform2D.Offset.Y //gives the y position of top left corner

shape.ShapeProperties.Transform2D.Extents.X //gives the x size of the shape : the width
shape.ShapeProperties.Transform2D.Extents.Y //gives the y size of the shape : the height

Upvotes: 6

Oliver Bock
Oliver Bock

Reputation: 5095

Go through the XML for the slide in question and look for xfrm elements, which should contain off (offset) and ext (extent) sub-elements. The measurements are in EMUs (see last page of Wouter van Vugt's document).

Upvotes: 1

Related Questions