Free Support Forum - aspose.com

Get the width of span of the merged table cells

Hi experts,
I am trying to read tables from word document, and got some question about how to get the height of a cell and width of span of a merged table cell.
See attached word document:
I noticed that from below method I can get the width of a table cell:
cell.getCellFormat().getWidth()
But I did not find any method to get the height of the cells.

The impact to me is I can not judge the width span of a cell in vertical direction. for example, in the sample.doc:
I can get the width of cell2,5, cell 2,6 and cell 3,6, I can judge that cell width span of 2.5 is cell 2,6 and cell3,6, since I can get the width of each cell, so I can calculate that width of cell 2,5 = cell 2,6 + cell 3,6
But in Vertical direction, for example, cell 0,1 and cell 0,2/cell1,2, I can not get the height of them , so I have no idea for how to the the height span of cell 0,1

Any ideas? or any other approach can achieve this?

Appreaciate.
Tonysample.zip (135.2 KB)

@yichunxia

Thanks for your inquiry. We suggest you please read the following article about cell and row height.
Specifying Row Heights

The Aspose.Words Layout API 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.

In your case, we suggest you following workaround to get the height of cell.

  1. Clone the table.
  2. Get the cell for which you want to get it height e.g. cell1
  3. Set its vertical text alignment as “Top” using CellFormat.VerticalAlignment property.
  4. Insert the bookmark before the text of cell.
  5. Get the next row of cell1.
  6. Do the same steps (3 and 4) for cell of next row.
  7. Please use LayoutCollector.GetEntity method for the inserted bookmarks to get their position.
  8. Get the difference of both positions. This will give you the cell height.

Following code example shows how to use layout API.

Document doc = new Document(MyDir + "in.docx");

Bookmark bookmark1 = doc.getRange().getBookmarks().get("bookmark1");
Bookmark bookmark2 = doc.getRange().getBookmarks().get("bookmark2");

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

Object renderObject = collector.getEntity(bookmark1.getBookmarkStart());
layoutEnumerator.setCurrent(renderObject);

Rectangle2D.Float rectange1 =  layoutEnumerator.getRectangle();

renderObject = collector.getEntity(bookmark2.getBookmarkStart());
layoutEnumerator.setCurrent(renderObject);

Rectangle2D.Float rectange2 =  layoutEnumerator.getRectangle();

System.out.println(rectange2.getY() - rectange2.getY());

Hope this helps you.

Hi Tahir,
Thanks for your help. I will try your proposal.
BTW,
Are there any approach to get the coordinates of tables, plain text etc. in a word file? just like what we did with PDF and OCR recognition.

Thanks
Tony

@yichunxia

Thanks for your inquiry.

LayoutCollector.GetEntity method returns an opaque position of the LayoutEnumerator which corresponds to the specified node. This method works for only Paragraph nodes, as well as indivisible inline nodes, e.g. BookmarkStart or Shape. It doesn’t work for Run, CellRow or Table nodes, and nodes within header/footer.

We suggest you please insert the bookmark at your desired location in the document and get its position.