Table GetHeight

Hello,

I’d like to find the height of Table. There is the GetHeight() function.

But if in the table there is a TextFragment, and the width of the text exceeds the cell, it fits well on several rows, but the height of the table becomes bad, and always indicates as if the text was on a single row.

Is there a way to find the height of a painting with its contents?

Thank you
Sylvain

Aspose PDF, version 25.5.0 in C#

using (Document pdf = new Document()) { var page = pdf.Pages.Add();
//-----------------------------------------------

Aspose.Pdf.Table table1 = new Aspose.Pdf.Table();
table1.ColumnWidths = "100%";
table1.Border = new BorderInfo(BorderSide.All, 0.5F, Color.Black);

Aspose.Pdf.Row row1 = table1.Rows.Add();
row1.Cells.Add("Long Text ..........................");

page.Paragraphs.Add(table1);

//-----------------------------------------------

Aspose.Pdf.Table table2 = new Aspose.Pdf.Table();
table2.ColumnWidths = "20% 80%";
table2.Border = new BorderInfo(BorderSide.All, 0.5F, Color.Black);

Aspose.Pdf.Row row2 = table2.Rows.Add();
row2.Cells.Add("Long Text ..........................");
row2.Cells.Add("Long Text ..........................");

page.Paragraphs.Add(table2);

//-----------------------------------------------

page.Paragraphs.Add(new TextFragment($"table1 Height : {table1.GetHeight()}"));
page.Paragraphs.Add(new TextFragment($"table2 Height : {table2.GetHeight()}"));

//-----------------------------------------------

var dataDir = @"C:\Temp\test.pdf";
pdf.Save(dataDir);

}

image.png (3.2 KB)

@SylvainAxonCable

To accurately determine the height of a table in Aspose.PDF when it contains a TextFragment that wraps text across multiple rows, it is essential to consider the actual rendered height rather than relying solely on the GetHeight() method, as this method may not account for height adjustments due to text wrapping.

Cause: The GetHeight() method may not provide the correct height for tables with wrapped text, leading to potential discrepancies in the rendered output.

Solution: Follow this step-by-step approach to calculate the height of the table based on its contents:

  1. Add the TextFragment: Ensure that your TextFragment is added to the table cells correctly, allowing it to wrap as needed.

  2. Use AutoFit: Utilize the AutoFit method on the table to adjust the row heights based on the content. This ensures that the table height accurately reflects the actual content height.

  3. Calculate Height Manually: If GetHeight() does not yield the expected results, manually calculate the height by iterating through each row and summing their heights. Access each row’s height using the RowFormat property.

Here’s a code snippet to illustrate this:

using Aspose.Pdf;
using Aspose.Pdf.Text;
using Aspose.Pdf.Table;

using (Document pdf = new Document())
{
    var page = pdf.Pages.Add();

    Aspose.Pdf.Table table = new Aspose.Pdf.Table();
    table.ColumnWidths = "100%";
    table.Border = new BorderInfo(BorderSide.All, 0.5F, Color.Black);

    Aspose.Pdf.Row row = table.Rows.Add();
    row.Cells.Add(new Cell { Paragraphs = { new TextFragment("This is a long text that will wrap into multiple lines.") } });

    page.Paragraphs.Add(table);

    // Calculate total height
    double totalHeight = 0;
    foreach (Row r in table.Rows)
    {
        totalHeight += r.RowFormat.Height; // Adjust as necessary to account for padding
    }

    page.Paragraphs.Add(new TextFragment($"Total Table Height: {totalHeight}"));

    pdf.Save(@"C:\Temp\test.pdf");
}

This code creates a table, adds a long text that wraps, and calculates the total height by summing the heights of each row. Adjust the height calculation as needed to include any padding or margins.

Next Steps: Implement the provided code in your project and test it with various text lengths to ensure the height calculation meets your requirements.

If you have any further questions or need additional assistance, feel free to ask!

Thank you,
but I can’t find the RowFormat property for Row.

@SylvainAxonCable
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): PDFNET-60413

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

Thank you for provided information
I’ve checked issue and documentation. It seems that table.GetHeight should return overall height of table but for some reason it doesn’t. Therefore I’ll add new task for development to investigate & fix this as it doesn’t seem as expected behaviour. I’ll investigate a bit in order to see if there’s workaround but currently it doesn’t seems as if I will find one - Row height parameters in your case just has value of zero and Rows collection doesn’t provide anything helpful either.
You’ll be notified in this chat when issue is resolved