Get the height of a row dynamically

I am using a word template and I need to know the height of a row to be able to calculate the height of the table

I always get the same height for a row : 0.  How can I get the real value of the height of a row after I add text inside ?


static void Main(string[] args)

{

    string jsonContent = File.ReadAllText(args[0]);

    dynamic obj = JsonConvert.DeserializeObject<ExpandoObject>(jsonContent);

    var config = (IDictionary<string, dynamic>) obj;


    string? filename = config.ContainsKey("filename") ? config["filename"] : null;

    string? templatePath = config.ContainsKey("templatePath") ? config["templatePath"] : null;

    IDictionary<string, dynamic> metadata = config.ContainsKey("metadata") ? config["metadata"] : new Dictionary<string, dynamic>();;

    IDictionary<string, dynamic>? replaceText = metadata.ContainsKey("replaceText") ? metadata["replaceText"] : null;

    IDictionary<string, dynamic>? tableConfig = metadata.ContainsKey("table2") ? metadata["table2"] : null;

    using (FileStream docStream = new FileStream(templatePath, FileMode.Open, FileAccess.Read, FileShare.Write))

    {

        using (WordDocument wordDocument = new WordDocument(docStream, FormatType.Docx))

        {

            if(tableConfig?.Count > 0)

            {

                WTableRow addRowTable(WTable currentTable, bool? isShowBorderRow = null)

                {

                    WTableRow newRow = currentTable.AddRow();

                    var listCell = newRow.Cells;

                    for (int i = 0; i < listCell.Count; i++)

                    {

                        var cellItem = listCell[i];

                        cellItem.CellFormat.Borders.Top.LineWidth = 0;

                        cellItem.CellFormat.Borders.Bottom.LineWidth = 0;

                        cellItem.CellFormat.BackColor = Color.Transparent;

                    }

                    return newRow;

                }


                WTableCell addCellContent(CellContentParameters args)

                {

                    var cellItem = args.CurrentCell;

                    var paragraph = cellItem.AddParagraph();

                    var cellFormat = paragraph.AppendText(args.Content);

                      return cellItem;

                }

                WSection section = wordDocument.Sections[0];

                if(section.Tables.Count > 0) {

                    WTable table = section.Tables[1] as WTable;

                    List<dynamic> listRows = tableConfig["body"].rows;

                    if(listRows.Count > 0)

                    {

                        for (int indexRow = 0; indexRow < listRows.Count; indexRow++)

                        {

                            WTableRow rowTable = addRowTable(table);

                            List<dynamic> dataRow = listRows[indexRow];

                            for (int indexCol = 0; indexCol < dataRow.Count; indexCol++)

                            {

                                var currentCell = rowTable.Cells[indexCol];

                                var cellData = dataRow[indexCol];

                                var content = ""; // add some text with breaking line inside the content 

                                addCellContent(new CellContentParameters(

                                    currentCell,

                                    content

                                ));

                            }

                        }

                        AdjustTableHeightToFillPage(table, section);

                    }

                }

            }

    }

 static void AdjustTableHeightToFillPage(WTable table, WSection section)

 {

     float pageHeight = section.PageSetup.PageSize.Height;

     float topMargin = section.PageSetup.Margins.Top;

     float bottomMargin = section.PageSetup.Margins.Bottom;

     float availableHeight = pageHeight - (topMargin + bottomMargin) - 170;

     float yOffset = 0f;

     int lastPageStartIndex = 0;

     var pageNumber = 1;


     for (int i = 0; i < table.Rows.Count; i++)

     {

         float rowHeight = table.Rows[i].Height > 0 ? table.Rows[i].Height : 0; 

//   **************  Height always have the same value here. The height doesn't adjust dynamically

        table.Rows[i].Height

 }

}


5 Replies

SR Sindhu Ramesh Syncfusion Team January 21, 2025 03:59 PM UTC

Hi Frederic,
We suspect that the issue is due to the row height not being specified in the input document. When creating a table, it is important to explicitly or programmatically define the height for each row. If the row height is not specified, it will default to 0.

image

You can get the row height and height type if they are explicitly mentioned in the document, or if they are set programmatically. Kindly check the below code

foreach (WTableRow row in document.LastSection.Body.Tables[0].Rows)
{
    Console.WriteLine(row.Height);
    Console.WriteLine(row.HeightType);
}


If you encounter any issues, kindly share the input document. This will help us better understand the problem and provide you with a more accurate solution.

Regards,
Sindhu Ramesh.



FR Frédéric January 21, 2025 05:01 PM UTC

The row's height is set at "0 at least" because the row's height should be set at the runtime. I do not know in advance how much text I will have in each line or how many breaking lines.

 

is it possible to have the height of each row dynamically after adding the text inside them?


Here is my input document 


Attachment: Template_55b01ed3.rar


SR Sindhu Ramesh Syncfusion Team January 22, 2025 01:31 PM UTC

Frédéric,
In Microsoft Word, if you specify (Atleast 0) as the row height, it indicates the minimum height of the row. Adding multiple paragraphs within the cell does not automatically change the row height in file level. When viewing the Word document in viewer (like Microsoft Word), it calculates the height of the content and preserves the row height for this case. This height information is not saved in file level for Word document.

DocIO is a non-UI component, which uses file level information to create DOM for Word document manipulations. So, it is not feasible to get row height information in DocIO based on dynamically added contents.

Also, calculating the exact row height is not feasible because the height depends on various factors, including:

  • Row properties (e.g., borders)
  • Cell properties (e.g., spacing, margins)
  • Text formatting (e.g., bold, line spacing, font, font size)

We suspect that your requirement is to calculate the table height. Could you please clarify the purpose of calculating the table height and end requirement (why calculating table height)? This information will help us to check and suggest possible approaches, if any, to achieve your end requirement.

Regards,
Sindhu Ramesh.



FR Frédéric January 22, 2025 02:01 PM UTC

The table needs to fill all the height of the table with the border.  

So I want to expand the last row of the table to make it bigger and like that the table will fill all the space and the table's border will show the full page.

And in the file attached here, that part should be always shown at the bottom of the table on the last page 


so if I have two pages,  the table will fill all the first page and on the second page, I will show the "footer of the table" under the table. and the table will take the rest of the space on the second page.



Attachment: bottom_table_406215ae.rar


SR Sindhu Ramesh Syncfusion Team January 23, 2025 08:03 AM UTC

Frédéric,
As mentioned in the previous update, it is not feasible to dynamically retrieve the row height based on the content. The table row height adjusts automatically to fit the content when heightType is set as Atleast.

Regards,
Sindhu Ramesh.


Loader.
Up arrow icon