Modifying the column width of a one-column table according to contents

Dear sirs,

I wonder if you could help me with the following: Assume a table created with DocumentBuilder with just one column and multiple rows. The number of rows is not known initially, and also their contents are variable, but always are a single line string, not several paragraphs, just a one line string.
I am creating the table with startTable(), and I proceed inserting a single cell with insertCell(), write in it and then call endRow(). The table is created perfectly, but a problem arises:
What if I want to have the column’s width fitted automatically to the longest string’s length, so that when the document is produced there will be no wrapping of the text inside the cells? I ve tried using Cell’s setFitText and setWrapText, both of them with false as an argument (I dont want the text to be compressed or wrapped) and I tried calling them from the affected cell’s instance both before and after the text was inserted. Unfortunately nothing happened and the table’s format hasn’t changed in the least.
Is there the ability to make the cell adjust it’s length to its contents, as long as it keeps them not-wrapped?

Thank you very much in advance,
Philip Alexander Hassialis

Hi,

Thanks for your inquiry. In MS Word you can do that by setting “Auto-fit to content” option. Unfortunately, currently, there is no way to set “Auto-fit to content” option programmatically using Aspose.Words. I will notify you as soon as this feature is available.
Best regards.

This is a feature I’m looking for as well. Any update on the status of this?

Thanks,
Matt

Hi Matt,
I’m afraid the issue is still unresolved. We will inform you of any developments regarding it.
In the mean time even though there is no one line API call to set this, you can still achieve the same functionality by using some code. Please see the example below which will fit the column with of any one column table in the document to the content.

NodeCollection nodes = doc.GetChildNodes(NodeType.Row, true);
foreach(Node node in nodes)
{
    Row row = (node as Row);
    if (row.Cells.Count == 1)
    {
        row.RowFormat.AllowAutoFit = true;
        foreach(Cell cell in row.Cells)
        {
            cell.CellFormat.Width = 1;
        }
    }
}

Thanks,

Like the OP, I’m using Aspose.Words for Java. Am I right in assuming that using the equivalent setters [row.getRowFormat().setAllowAutoFit(true) and cell.getCellFormat().setWidth(1)] should work?

–Matt

Hi Matt,
Thanks for the additional informaton. Yes this is correct, the conversion of the code should work the same. I have converted it for you, please see the code below.

for (int i = 0; i <nodes.getCount(); i++)
{
    Row row = (Row) nodes.get(i);
    if (row.getCells().getCount() == 1)
    {
        row.getRowFormat().setAllowAutoFit(true);
        for (Cell cell: row.getCells())
        {
            cell.getCellFormat().setWidth(1);
        }
    }
}

Thanks,

Hi Adam,

The cells still aren’t coming out wide enough (the text is wrapping even though I’ve called setWrapText(false) as well). I should have mentioned that in my case the table has more than one column. Does your setWidth(1) workaround not work if the table has more than one “column”?

By the way, I’m using a DocumentVisitor to get the relevant nodes, but otherwise I’m following the same approach you described.

Thanks,
Matt

Hi Adam,

I just tried the following silly approach and the output actually looked pretty good:

cellFormat.setWidth(cellFormat.getWidth() * 1.35);

–Matt

Hi Matt,
The code works even for tables with more than one column, the only reason it was set to do tables with one column was because that was the original specification above.
It’s great you got it working. If you want you can still attach your template here and I will have a look and see why the code was not working as expected for your table.
Thanks,

Hi Adam,

I’ve attached my source HTML and CSS, along with three different versions of the output, all in a zip file.

  • table_output_unchanged.doc - borders are removed from cells, but no changes are made to the cell width
  • table_output_width1.doc - same as above, except cellFormat.setWidth(1) is also called
  • table_output_extra35percent.doc - same as first, but also call cellFormat.setWidth(cellFormat.getWidth() * 1.35)

Thanks,
Matt

Hi Matt,
It appears to be a limitation of the Autofitting at the moment, most likely due to the merged cells. I think the workaround you are using seems to work pretty well right now unless there are any other problems with it?
Thanks,

Hi Adam,

That makes sense. I know that merged cells have caused problems in other respects. I’m okay with my workaround for now, although it does cause some cells to be wider than necessary.

Thanks,
Matt

Hi Matt,
It’s great that the work around can be used for now. We will look into implementing this in a future version.
Thanks,

The issues you have found earlier (filed as WORDSNET-2044) have been fixed in this .NET update and in this Java update.

This message was posted using Notification2Forum from Downloads module by aspose.notifier.
(101)