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,
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
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?
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);<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
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?
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,
Hi Adam,
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,
- 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)
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,
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)