Adjust a table's width and height so that it a table always fits in a single page

Hi Team
I have a table where the width could exceed the page’s width and height also could exceed. In this case how can i autofit the the entire table so that if and only if the width and height exceed, then I can resize the table by some ratio (also all content in it too so will have to resize font too).

I am working with .Net C#.

I tired like below including DocumentVisitor, but the updatetablelayout is ruining the existing widths and the table exceeds the page width anyway:

builder.Document.UpdateTableLayout();
double tableWidth = 0;
foreach (Aspose.Words.Tables.Cell c in parentTable.FirstRow.Cells)
    tableWidth += c.CellFormat.Width;

Aspose.Words.Section tableParentSection = (Aspose.Words.Section)parentTable.GetAncestor(NodeType.Section);
if (tableWidth >= (tableParentSection.PageSetup.PageWidth - 100))
{
    double fontRatio = (tableParentSection.PageSetup.PageWidth - 100) / (tableWidth);
    FontSizeVisitor visitor = new FontSizeVisitor(fontRatio);
    parentTable.Document.Accept(visitor);
}

Is this possible?

@SumithaS create a generic solution for your scenario could be particularly hard to achieve, because you need to take into account too many possible scenarios. I recommend creating a solution that match with your base use case and then expand it.

In any case I recommend the use of the classes LayoutCollector and LayoutEnumerator to get the real dimensions of the table.

If you provide an example base document and the expected output, I will create for you a base solution that you can expand based on the requirements.

Hi @eduardo.canal,

I have attached a document that i was able to achieve with this code:

private void AdjustFontSizeIfTableTooWide(Aspose.Words.Tables.Table parentTable, DocumentBuilder builder)
{
    builder.Document.UpdateTableLayout();

    double tableWidth = 0;
    foreach (Aspose.Words.Tables.Cell c in parentTable.FirstRow.Cells)
        tableWidth += c.CellFormat.Width;

    Aspose.Words.Section tableParentSection = (Aspose.Words.Section)parentTable.GetAncestor(NodeType.Section);
    if (tableWidth >= (tableParentSection.PageSetup.PageWidth - 100))
    {
        Aspose.Words.PageSetup ps = builder.PageSetup;
        ps.Orientation = Aspose.Words.Orientation.Landscape;
        if (tableWidth >= (tableParentSection.PageSetup.PageWidth - 100))
        {
            double fontRatio = (tableParentSection.PageSetup.PageWidth - 100) / (tableWidth * 2);

            FontSizeVisitor visitor = new FontSizeVisitor(fontRatio);
            parentTable.Document.Accept(visitor);
        }
    }
}

But you can see that still the tables run out of the page. ParentTable here has nested tables.

Expected output would be that the tables stay within the page margins and font ratio should reduce accordingly when trying to autofit them. In the first page, you can see that i have changed the page orientation to landscape if the table width does not fit the portrait page. if it still does not fit, then i try to reduce the font size by ratio so that the table fits, but unfortunately it still does not fit.

If i dont use builder.updatetablelayout then the table on the second page does indeed fit correctly. but the one on first page still does not. I have attached both scenarios.
I wen through LayoutCollector and LayoutEnumerator documentation but was not successful in creating a working code to calculate width and height of the table. Could you pls provide a working sample code for the same?

Please do let me know if you need anymore info.Sample_WITHOUT_AutoFit.pdf (141.0 KB)
Sample.pdf (140.9 KB)

@eduardo.canal also, all the cells have dynamic widths and we are not setting cellFormat.width anywhere.

I have tried with a lot of table.autofit options but as my table structure gets more complex (more nested fields and some rows have only 1 single cell etc.) it keeps ruining the overall tableAlignments… Please help!

Hi @eduardo.canal,

Please do let me know if there is any update here. Thanks in advance!

@SumithaS Could you please attach your source document here for testing? Also. please note that Document.UpdateTableLayout method is deprecated and is not recommended to use. This method was a first attempt to implement the table grid algorithm, that works only for very specific cases. The method will be removed in one of future versions of Aspose.Words.