We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Change Column Width of each Column in a Table

Hello,
I am using the code below to build a table from datatable. How can I change the column width of each column. One of my column is description and needs more width. Other column needs less space.

Thanks

@kshah05 You can use DocumentBuilder.CellFormat.Width to specify width of cell upon building table.

....
builder.InsertCell();
builder.CellFormat.Width = 250;
....

I tried to implement

builder.CellFormat.Width = 250; 

refering the article below

 foreach (DataColumn column in dataTable.Columns)
        {
            builder.InsertCell();
            builder.Writeln(column.ColumnName);
        }

but it did not work. I want to change it for each column

@kshah05 just modify the code like the following:

foreach (DataColumn column in dataTable.Columns)
{
    builder.InsertCell();
    builder.CellFormat.Width = 250; 
    builder.Writeln(column.ColumnName);
}

Note, in MS Word table there is not columns, each row in the table is independent and can contain any number of cells of any width. So you need to specify width of each cell in the table.

I tried it and it did not work. Please provide a code sample with different width with the code above.

Thanks

@kshah05 Please see the following code example:

DataTable dataTable = new DataTable();
dataTable.Columns.Add("First Name");
dataTable.Columns.Add("Last Name");
dataTable.Columns.Add("Description");

dataTable.Rows.Add("Alexey", "Noskov", "Aspose.Words Support Lead.");
dataTable.Rows.Add("Vadim", "Saltykov", "Aspose.Words Support Lead / Aspose.Words Developer.");
dataTable.Rows.Add("Sergey", "Lobanov", "Aspose.Words Support Developer.");


Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

foreach (DataColumn column in dataTable.Columns)
{
    builder.InsertCell();
    builder.CellFormat.Shading.BackgroundPatternColor = Color.LightGray;
    // Make the 'Description' column wider than other.
    builder.CellFormat.Width = (column.ColumnName == "Description") ? 500 : 200;
    builder.Bold = true;
    builder.Write(column.ColumnName);
}

// Specify heading format to repeat header row on each page.
Row headerRow = builder.EndRow();
headerRow.RowFormat.HeadingFormat = true;

foreach (DataRow dr in dataTable.Rows)
{
    foreach (DataColumn column in dataTable.Columns)
    {
        builder.InsertCell();
        builder.CellFormat.ClearFormatting();
        // Make the 'Description' column wider than other.
        builder.CellFormat.Width = (column.ColumnName == "Description") ? 500 : 200;
        builder.Bold = false;
        builder.Write(dr[column].ToString());
    }
    builder.EndRow();
}
builder.EndTable();

// Save the result.
doc.Save(@"C:\Temp\out.docx");

Ans the document produced by this code: out.docx (7.3 KB)
Also, using DocumentBuilder is not the best option to put data from data base into the document. In your case you can try using Mail Merge with Regions feature. In this case you can define the table layout in MS Word and then fill the template with data.

Hi Alexey,
Thank you for the code above.
I had tried to Mail Merge with Regions feature initially but I kept getting errors with merging data table with Mail Merge. I am new to Aspose.Words and PDF API and trying to get familiar with the methods and properties.

Is it possible to provide me a sample code of using Mail Merge with Regions to build table with Datatable data from database.

My ultimate goal is to convert the Aspose Words table, with signature into PDF

Thanks

@kshah05 Sure, please find the attached template: in.docx (15.6 KB)
Here is a simple code to fill the template with data and save the output.

DataTable dataTable = new DataTable("MyTable");
dataTable.Columns.Add("FirstName");
dataTable.Columns.Add("LastName");
dataTable.Columns.Add("Description");

dataTable.Rows.Add("Alexey", "Noskov", "Aspose.Words Support Lead.");
dataTable.Rows.Add("Vadim", "Saltykov", "Aspose.Words Support Lead / Aspose.Words Developer.");
dataTable.Rows.Add("Sergey", "Lobanov", "Aspose.Words Support Developer.");

// Open template
Document doc = new Document(@"C:\Temp\in.docx");

// Execute mail merge with regions.
doc.MailMerge.ExecuteWithRegions(dataTable);

// Save the result.
doc.Save(@"C:\Temp\out.docx");
doc.Save(@"C:\Temp\out.pdf");

Here is output produced by this code:
out.docx (11.2 KB)
out.pdf (32.4 KB)

Thanks Alexey, How do I you create the Merge Template in Word? I am looking for instructions to create the template. Creating template is a better option then doing it programatically

@kshah05 Please see our documentation to learn more about Mail Merge with Regions. In the template merge fields are used, you can insert them in MS Word - select Insert tab, in the Text section select Quick Parts and select Field

In the list of field select MergeField and type the name of the field:

Field names should match column names in your data source. When using Mail Merge with Regions you should also enclose the repeating section (table row for example) between merge fields with special names TableStart:XXX and TableEnd:XXX, where XXX is the name of the table in your data source. See the template from my previous post.

I created the template.

ActivityLogTemplate.docx (15.9 KB) but keep getting the error : System.InvalidOperation: Found end of mail merge region ‘tblAuditlogs’ that does not match start of mail merge region ‘AuditLogs’

I am unable to find where the AuditLogs is coming from . If you see the template I am mentioning TableStart:tblAuditLogs and TableEnd:tblAuditLogs

Thanks

@kshah05 I have checked you template and see that table start has Auditlogs, but table end - tblAuditlogs. See the attached screenshot:

You can press Alt+F9 to see filed codes in the document.
Also here is the corrected template: updated_ActivityLogTemplate.docx (16.1 KB)

Thanks, I got it to work. I am trying to add another Table above tblAuditLogs and I am getting the error:

Mail merge region ‘tblAuditLogFilterCriteria’ is badly formed. TableStart and TableEnd should be in the same section, same table row or same table cell.ActivityLogTemplate.docx (16.4 KB)

Code:
image.png (60.3 KB)

Why am I getting the error for tblAuditLogFilterCriteria?

@kshah05 In your template there is a badly formatted region. TableStart and TableEnd of ‘tblAuditLogFilterCriteria’ region are in different table rows. TableStart and TableEnd of the region should be in the same table row. Please see the modified template: updated_ActivityLogTemplate.docx (16.6 KB)