Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Thu Apr 21, 2016 3:48 pm

Spire.Doc is working to create and populate a table with our data, but with no explicit formatting the first two columns are taking up the majority of the table. The first and second columns are taking up about 40% of the table each only leaving about 20% left for the last column. How do I set the third column to take up the right 50% of the table leaving the left 50% for the other two columns. How do I explicitly set the width of the columns in a filled out table?

Here is the method that is generating our document using spire.

public static void CreateDocumentWithMergeFields(Dictionary<string, string> fieldNames, string filePath)
{
//get filePath from dialog.
var spireStringDocPath = filePath;
//Create a word document
Document document = new Document();
Section section = document.AddSection();
Table table = section.AddTable(true);
//create the header
string[] header = { "FieldName", "MergeField", "Description" };

//add cells
table.ResetCells(fieldNames.Count+1, header.Length);
//setting header row as first row
TableRow headerRow = table.Rows[0];
headerRow.IsHeader = true;
headerRow.Height = 23;

Paragraph fieldNameHeaderCell = headerRow.Cells[0].AddParagraph();
TextRange fieldNameHeaderText = fieldNameHeaderCell.AppendText(header[0]);

Paragraph mergeFieldHeaderCell = headerRow.Cells[1].AddParagraph();
TextRange mergeFieldHeaderText = mergeFieldHeaderCell.AppendText(header[1]);

Paragraph descriptionHeaderCell = headerRow.Cells[2].AddParagraph();
TextRange descriptionHeaderText = descriptionHeaderCell.AppendText(header[2]);

headerRow.Cells[2].SetCellWidth(75, CellWidthType.Percentage);

//for each fieldName add a row with field and merge field
int loopIndex = 1;
foreach (KeyValuePair<string, string> kvp in fieldNames)
{
//add a new row to the table.
TableRow row = table.Rows[loopIndex];

Paragraph fieldNameCell = row.Cells[0].AddParagraph();
//append the field name to the 1st column
var fieldNameText = fieldNameCell.AppendText(kvp.Key);

Paragraph mergedFieldCell = row.Cells[1].AddParagraph();
//create merge field on the second column
var mergeFieldText = mergedFieldCell.AppendField(kvp.Key, FieldType.FieldMergeField) as MergeField;
//mergedFieldCell.AppendText(kvp.Key);

Paragraph descriptionCell = row.Cells[2].AddParagraph();
//appened description in the third column
var descriptionText = descriptionCell.AppendText(kvp.Value);

row.Cells[2].SetCellWidth(75, CellWidthType.Percentage);
loopIndex++;
}

//Save doc file
document.SaveToFile(spireStringDocPath, FileFormat.Doc);
}

Keith.Hubbard
 
Posts: 1
Joined: Thu Apr 21, 2016 3:40 pm

Fri Apr 22, 2016 3:13 am

Hi,

Thanks for your posting.
Please try the following code.
Code: Select all
 PreferredWidth width = new PreferredWidth(WidthType.Percentage, 100);
            table.PreferredWidth = width;
            for (int i = 0; i < table.Rows.Count;i++ )
            {
                table.Rows[i].Cells[0].SetCellWidth(25, CellWidthType.Percentage);
                table.Rows[i].Cells[1].SetCellWidth(25, CellWidthType.Percentage);
                table.Rows[i].Cells[2].SetCellWidth(50, CellWidthType.Percentage);
            }


Best Regards,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2766
Joined: Wed Jun 27, 2012 8:50 am

Tue Apr 26, 2016 8:42 am

Hi,

Has your issue been resolved?
Thanks for your feedback in advance.

Best Regards,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2766
Joined: Wed Jun 27, 2012 8:50 am

Return to Spire.Doc