Hi,
How to remove one specific column in a existing table? is it possible? If so can you provide me a code snippet.
Hi,
How to remove one specific column in a existing table? is it possible? If so can you provide me a code snippet.
Hi
Thanks for your inquiry. Actually there is no “column” concept in the Word table. By Microsoft Word design rows in a table in a Microsoft Word document are completely independent. It means each row can have any number of cells of any width.
So if you need to remove column from MS Word table you should remove cell from each row in this table. For example see the following code:
// Open docuemnt
Document doc = new Document(@"Test301\in.doc");
// Get first table from document
Table tab = doc.FirstSection.Body.Tables[0];
int indexToRemove = 3; //Zero based index
// Remove cell from each table row
for (int rowIndex = 0; rowIndex < tab.Rows.Count; rowIndex++)
{
tab.Rows[rowIndex].Cells[indexToRemove].Remove();
}
// Save output docuemnt
doc.Save(@"Test301\out.doc");
If you need to add column to the existing table you should add cell to each row in this table.See the following code:
// Open docuemnt
Document doc = new Document(@"Test301\in.doc");
// Get first table from document
Table tab = doc.FirstSection.Body.Tables[0];
// Create new cell
Cell newCell = new Cell(doc);
Paragraph par = new Paragraph(doc);
Run text = new Run(doc, "this is new column");
par.AppendChild(text);
newCell.AppendChild(par);
newCell.CellFormat.Width = 50;
// Add cell to each table row
for (int rowIndex = 0; rowIndex < tab.Rows.Count; rowIndex++)
{
// Clone created cell
Cell clon = (Cell)newCell.Clone(true);
tab.Rows[rowIndex].AppendChild(clon);
}
// Save output docuemnt
doc.Save(@"Test301\out.doc");
Hope this helps.
Best regards.