5

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?
Ryan Abbott
  • 5,317
  • 7
  • 31
  • 34

3 Answers3

5

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
Deunz
  • 1,776
  • 19
  • 32
1

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).

Oliver Bock
  • 4,829
  • 5
  • 38
  • 62
0

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.