Documentbuilder table cell widths

I am creating a table with documentbuilder but using the rowformat.allowautofit doesn’t seem to make my cells auto fit to the text in them. here is the code i am using

DataTable dt = dsTable1.Tables[0];
foreach (DataRow row in dt.Rows)
{
    foreach (DataColumn col in dt.Columns)
    {
        builder.InsertCell();
        builder.Write(row[col].ToString());
    }
    builder.EndRow();
    builder.RowFormat.AllowAutoFit = true;
}
builder.EndTable();

Each cell in the table seems to be set to the default width value, which makes for a very ugly table. I would like the table to span the width of the whole page and any long text strings to preferably be on single lines within each cell. Any help would be greatly appreciated.

Hi
Thanks for your inquiry. I think that you should set WrapText = false. Try using the following code:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.CellFormat.WrapText = false;
foreach (DataRow row in dt.Rows)
{
    foreach (DataColumn col in dt.Columns)
    {
        builder.InsertCell();
        builder.Write(row[col].ToString());
    }
    builder.EndRow();
}

Hope this helps.
Best regards.

It tells me that:
Error 125 ‘Aspose.Word.CellFormat’ does not contain a definition for ‘WrapText’

Hi
Thanks for your inquiry. It seems that you are using old version of Aspose.Words. I use the latest version of Aspose.Words.
Which version of Aspose.Words are you using? What is expiration date of your license?
Best regards.

Thanks for the help, I was using an old version.

Just another quick query, is there a way to make the table stretch the width of the document while keeping the wraptext properties?
i am using this to set the cell widths of each cell:
builder.CellFormat.Width = pageWidth / dt.Columns.Count;
unfortunately this resets the auto fit properties and each cell ends up the same size again.

Hi
Thanks for your inquiry. Unfortunately there is no way to set width of the whole table using Aspose.Words. This is issue #907 in our defect database.
Best regards.

Hi again,
As a workaround you can try using the following code:

doc.UpdateTableLayout();
// Get table from document
Table tab = doc.FirstSection.Body.Tables[0];
// Calculate curent width of table
Double tableWidth = 0;
foreach (Cell cell in tab.FirstRow.Cells)
{
    tableWidth += cell.CellFormat.Width;
}
// Calculate width of page
PageSetup ps = doc.FirstSection.PageSetup;
double pageWidth = ps.PageWidth - ps.LeftMargin - ps.RightMargin;
// Resize each cell
foreach (Row row in tab.Rows)
{
    foreach (Cell cell in row.Cells)
    {
        // Get current width od cell
        double currentWidth = cell.CellFormat.Width;
        // Calculate abd set widht
        cell.CellFormat.Width = (currentWidth / tableWidth) * pageWidth;
    }
}

Hope this helps.
Best regards.

cheers, that gives me exactly what i wanted!

I am not using Table of Aspose.Words but simply DocumentBuilder to develop table. So how can I set width of that without using Table

Hi Monika,
Thanks for your request. You should simply set width of each cell in your table when you build it. Please see the code example provided in the following article:
https://reference.aspose.com/words/net/aspose.words/documentbuilder/starttable/
Best regards,

It does not show all the columns.
But shows as in attachment. How can I solve this problem

Hi Monika,
Thanks for your request. You should simply calculate widths of cells to your table fit the page. For instance, you can use code suggested in the following article to auto fit your table to page width:
https://docs.aspose.com/words/net/working-with-tables/
Best regards,

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

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