Aspose.words get table size and resize ONLY if wider than the page

I cannot figure out how to get the width of a Table in words. I have some tables coming in via html that are much wider than the page. We need to reduce them AND keep them readable. the best method I can find to assist us with this is to reduce the font size when a table is much larger AND use the auto fit methods… I have used the AutoFitContents and AutoFitWindow and both of these render the data unclear. (it bleeds over lines), reducing the font size works great.

However, There is no point in me doing this when a table already fits to the page. How c an I obtain a table’s size prior to me using these techniques?

NodeCollection tables = doc.GetChildNodes(NodeType.Table, true);
foreach (Table table in tables)
{
    // IF TABLE IS TOO WIDE?? HOW?
    table.AutoFit(AutoFitBehavior.AutoFitToContents);
    foreach (Run run in table.GetChildNodes(NodeType.Run, true))
    {
        run.Font.Size = table.Style.Font.Size / 2;
    }
}

@rileyja Could you please attach your input HTML and code you use to import HTML into the document? This will help us to better understand the problem. We will check the issue and provide you more information.

The problem is, when I export a document sometimes (not always) some of the tables are too wide. If I use PreferedWidth the formatting of the table gets messed up. the input HTML, is not important, nor is how I get it. I have an extension method that converts string to stream.

Stream htmlStream = settings.Html.ToStream();
LoadOptions options = new LoadOptions
{
    PreserveIncludePictureField = false,
    ResourceLoadingCallback = settings.WordsResourceLoadingCallback,
    Encoding = Encoding.UTF8,
    LoadFormat = LoadFormat.Html,
    MswVersion = MsWordVersion.Word2016
};
Document doc = new Document(htmlStream, options);
DocumentBuilder builder = new DocumentBuilder(doc);

Now I get a list of the tables and NOTE my IF comment. that is what i want to do. IF TABLE IS WIDER THAN PAGE { }

NodeCollection tables = doc.GetChildNodes(NodeType.Table, true);
foreach (Table table in tables)
{
  // IF TABLE IS WIDER THAN PAGE {
    table.AutoFit(AutoFitBehavior.AutoFitToContents);
    foreach (Run run in table.GetChildNodes(NodeType.Run, true))
    {
        run.Font.Size = table.Style.Font.Size / 2;
    }
// END IF TABLE IS WIDER THAN PAGE  }
}

@rileyja The problem is that table width can be specified in different ways in both in HTML and in MS Word documents:
https://docs.aspose.com/words/net/applying-formatting/#specifying-table-and-cell-widths
In your case, you can try using LayoutCollector and LayoutEnumerator to get the calculated by Aspose.Words Layout Engine width of the table:

Document doc = new Document(@"C:\Temp\in.html");
LayoutCollector collector = new LayoutCollector(doc);
LayoutEnumerator enumerator = new LayoutEnumerator(doc);

NodeCollection tables = doc.GetChildNodes(NodeType.Table, true);
foreach (Table table in tables)
{
    // Skip nodes in the header and footer. LayoutCollector Does not work with them.
    if (table.GetAncestor(NodeType.HeaderFooter) != null)
        continue;

    // Move to first paragraph in the table
    enumerator.Current = collector.GetEntity(table.FirstRow.FirstCell.FirstParagraph);
    // And move to the row entity.
    while (enumerator.Type != LayoutEntityType.Row)
        enumerator.MoveParent();

    // Now we can get the calculated rectangle of the row.
    Console.WriteLine(enumerator.Rectangle);
}

This works perfectly! thank you

1 Like