Get shape position relative to page using C#

Hi Team,

I am trying to identify and extract all the shape coordinates from the word documents.
Below is the code snippet i used.

static void ExtractImageFromWord(string inputDoc)
{
  Document doc = new Document(inputDoc);
  LayoutCollector layout = new LayoutCollector(doc);
  foreach (Shape shape in doc.GetChildNodes(NodeType.Shape, true))
  {   
	  var width = shape.Bounds.Width;
	  var height = shape.Bounds.Height;
	  var x = shape.Bounds.X;
	  var y = shape.Bounds.Y;
	  var pageNo = layout.GetStartPageIndex(shape);
  }
}

Width and height fields are correctly populated, whereas X and Y values are always set 0. I have tried with many sample documents and all the docs have same behavior.

Can you please let me know how to properly get the shape coordinates?

@senthilspi

The Aspose.Words.Layout namespace provides classes that allow to access information such as on what page and where on a page particular document elements are positioned, when the document is formatted into pages.

Please use the following code example to get the position of shape relative to the page top left corner. Hope this helps you.

Document doc = new Document(MyDir + "input.docx");
Shape shape = (Shape)doc.GetChild(NodeType.Shape, 0, true);

LayoutCollector collector = new LayoutCollector(doc);
LayoutEnumerator enumerator = new LayoutEnumerator(doc);

enumerator.Current = collector.GetEntity(shape);
Console.WriteLine(" --> Left : " + enumerator.Rectangle.Left + " Top : " + enumerator.Rectangle.Top);
1 Like