Hi,
Thanks for your inquiry.
The code you provided isn’t complete, so I created a simple input word file and test demo to test according to your messages.
There are two solutions for you, I put them below and attach my input test word file.
1) Set table AutoFit to AutoFitBehaviorType.AutoFitToWindow
- Code: Select all
table.AutoFit(AutoFitBehaviorType.AutoFitToWindow);
I put complete code below for this solution:
- Code: Select all
// Create a new Document object
Document strDocument = new Document();
// Load the document from the specified file path
strDocument.LoadFromFile(@"../../data/testInsertTable.docx");
// Get the first table in the first section of the document
Table strPrincipalTable = strDocument.Sections[0].Tables[0] as Spire.Doc.Table;
// Get the second row of the principal table
TableRow strDataRowPrincipal = strPrincipalTable.Rows[1];
// Clear the paragraphs in the first cell of the second row
strDataRowPrincipal.Cells[0].Paragraphs.Clear();
// Create a new table with the same document and set its layout to true
Table table = new Table(strDocument, true);
// Reset the cells of the table to have 2 rows and 6 columns
table.ResetCells(2, 6);
// Iterate through each row of the table
for (int i = 0; i < (table.Rows.Count); i++)
{
// Set the width of each cell in the row
table.Rows[i].Cells[0].SetCellWidth(10f, CellWidthType.Auto);
table.Rows[i].Cells[1].SetCellWidth(30f, CellWidthType.Auto);
table.Rows[i].Cells[2].SetCellWidth(30f, CellWidthType.Auto);
table.Rows[i].Cells[3].SetCellWidth(10f, CellWidthType.Auto);
table.Rows[i].Cells[4].SetCellWidth(10f, CellWidthType.Auto);
table.Rows[i].Cells[5].SetCellWidth(30f, CellWidthType.Auto);
}
// Add the newly created table to the first cell of the second row of the principal table
strDataRowPrincipal.Cells[0].ChildObjects.Add(table);
// Auto-fit the table to the window size
table.AutoFit(AutoFitBehaviorType.AutoFitToWindow);
// Save the modified document to the specified file path
strDocument.SaveToFile(@"../../output/resultInsertTable_Percentage.docx", FileFormat.Docx);
2) Set tableCell width to
CellWidthType.Percentage, I put complete code below for this solution.
- Code: Select all
// Create a new Document object
Document strDocument = new Document();
// Load the document from the specified file path
strDocument.LoadFromFile(@"../../data/testInsertTable.docx");
// Get the first table in the first section of the document
Table strPrincipalTable = strDocument.Sections[0].Tables[0] as Spire.Doc.Table;
// Get the second row of the principal table
TableRow strDataRowPrincipal = strPrincipalTable.Rows[1];
// Clear the paragraphs in the first cell of the second row
strDataRowPrincipal.Cells[0].Paragraphs.Clear();
// Create a new table with the same document and set its layout to true
Table table = new Table(strDocument, true);
// Reset the cells of the table to have 2 rows and 6 columns
table.ResetCells(2, 6);
// Set the preferred width of the table to be 100% of the available width
table.PreferredWidth = new PreferredWidth(WidthType.Percentage, 100);
// Iterate through each row of the table
for (int i = 0; i < (table.Rows.Count); i++)
{
// Set the width of each cell in the row
table.Rows[i].Cells[0].SetCellWidth(10, CellWidthType.Percentage);
table.Rows[i].Cells[1].SetCellWidth(10, CellWidthType.Percentage);
table.Rows[i].Cells[2].SetCellWidth(30, CellWidthType.Percentage);
table.Rows[i].Cells[3].SetCellWidth(10, CellWidthType.Percentage);
table.Rows[i].Cells[4].SetCellWidth(10, CellWidthType.Percentage);
table.Rows[i].Cells[5].SetCellWidth(30, CellWidthType.Percentage);
}
// Add the newly created table to the first cell of the second row of the principal table
strDataRowPrincipal.Cells[0].ChildObjects.Add(table);
// Save the modified document to the specified file path
strDocument.SaveToFile(@"../../output/resultInsertTable_Percentage.docx", FileFormat.Docx);
If the two solutions above don’t help you, please offer your complete code that can reproduce your issue and input word file to help us do further investigation. You can attach here or send it via email(
[email protected]).
Sincerely
Abel
E-iceblue support team