Articles in this section

How to Add, Insert, or Remove Columns from a Table in Word Document .NET Core?

Syncfusion® Essential® DocIO is a .NET Word library used to create, read, edit, and convert Word documents programmatically without Microsoft Word or interop dependencies. Using this library, you can add, insert, or remove columns from a table in Word document using C#.

To achieve this, you can iterate through the table rows to add a column at a specific position using the Insert() API with an index or add it at the end using the Add() API. Columns can be removed using the RemoveAt() API.

Steps to add, insert, or remove columns from a table in Word document:

  1. Create a new .NET Core console application project.
    Create console application in Visual Studio
  2. Install the Syncfusion.DocIO.Net.Core NuGet package as a reference to your project from NuGet.org.
    Add DocIO NuGet package reference to the project
Note:

Starting with v16.2.0.x, if you reference Syncfusion® assemblies from a trial setup or from the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering a Syncfusion® license key in your application to use the components without a trial message.

  1. Include the following namespaces in the Program.cs file.
    C#
using Syncfusion.DocIO.DLS;
using Syncfusion.DocIO; 
  1. Use the following code example to add, insert, or remove columns from a table in Word document
    C#
// Load the Word document
using (WordDocument document = new WordDocument(Path.GetFullPath(@"Data/Template.docx")))
{
    // Access the first table in the document
    WTable table = (WTable)document.Sections[0].Tables[0];
    // Add a new column at specific index
    InsertColumn(table, 1);
    // Add a column at the last index
    AddColumn(table);
    // Remove a column at the specific index 
    RemoveColumn(table, 3);
    // Save the modified document to a new file
    document.Save(Path.GetFullPath(@"../../../Output/Result.docx"), FormatType.Docx);
}
  1. Use the following code example to add a column at the end of each row.
    C#
 private static void AddColumn(WTable table)
 {
     // Loop through each row in the table
     for (int i = 0; i < table.Rows.Count; i++)
     {
         // Add a new cell to the current row (appends at the end)
         WTableCell cell = table.Rows[i].AddCell();
         // Set the width of the new cell to match the first cell in the row
         cell.Width = table.Rows[i].Cells[0].Width;
         // Add a paragraph to the new cell and insert the text
         cell.AddParagraph().AppendText("Using Add API");
     }                
 }
  1. Use the following code example to insert a column at a specific index.
    C#
private static void InsertColumn(WTable table, int indexToAdd)
{
    // Loop through each row in the table
    for (int i = 0; i < table.Rows.Count; i++)
    {
        // Check if the index is within the valid range for the current row
        if (indexToAdd >= 0 && indexToAdd <= table.Rows[i].Cells.Count)
        {
            // Create a new cell.
            WTableCell newCell = new WTableCell(table.Document);
            // Insert the new cell at the specified index in the current row
            table.Rows[i].Cells.Insert(indexToAdd, newCell);
            // Add a paragraph to the new cell and insert the text
            newCell.AddParagraph().AppendText("Using Insert API");
        }                
    }
}
  1. Use the following code example to remove a column at a specific index.
    C#
private static void RemoveColumn(WTable table, int indexToRemove)
{
    // Loop through each row in the table
    for (int i = 0; i < table.Rows.Count; i++)
    {
        // Check if the index is within the valid range for the current row
        if (indexToRemove >= 0 && indexToRemove < table.Rows[i].Cells.Count)
        {
            // Remove the cell at the specified index in the current row
            table.Rows[i].Cells.RemoveAt(indexToRemove);
        }
    }
}

You can download a complete working sample to add, insert, or remove columns from a table in Word document from GitHub.

Output Word document

Take a moment to peruse the documentation where you can find basic Word document processing options along with features like mail merge, merge, split, and compare Word documents, find and replace text in the Word document, protect the Word documents, and most importantly, the PDF and Image conversions with code examples.

Conclusion

I hope you enjoyed learning about how to add, insert, or remove columns from a table in Word document in a .NET Core Word document.

You can refer to our ASP.NET Core DocIO feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started with configuration specifications. You can also explore our ASP.NET Core DocIO example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Access denied
Access denied