Free Support Forum -

Find height of the table before & after the text insertion in the same table

I have a requirement, If table height is more than 10cm in docx file which contains dynamic text, need to change the color of the text.
Please help me to find any flags to measure the height of the table.


Thanks for your inquiry. The height of a table row/cell is controlled using height and height rule properties. Please refer to the following article for more detail.
Specifying Row Heights

In your case, we suggest you please use layout API to get the height of table. Please check the following code example. Hope this helps you.

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

Document cloneDoc = (Document)doc.Clone(true);

Table table = cloneDoc.FirstSection.Body.Tables[0];

Paragraph para = new Paragraph(cloneDoc);
table.LastRow.LastCell.InsertAfter(para, table.LastRow.LastCell.LastParagraph);

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

enumerator.Current = collector.GetEntity(table.FirstRow.FirstCell.FirstParagraph);
double position1 = enumerator.Rectangle.Top;

enumerator.Current = collector.GetEntity(para);
double position2 = enumerator.Rectangle.Top;
Console.WriteLine(position2 - position1);
double height = position2 - position1;

//Chagne the color of text
if (height > 340.157) // 340.157 = 12 cm
    foreach (Run run in doc.FirstSection.Body.Tables[0].GetChildNodes(NodeType.Run, true))
        run.Font.HighlightColor = Color.Yellow;

doc.Save(MyDir + "18.6.docx");