Insert, Read and Delete Table from Word Textbox in C#

In Word, textbox can contain multiple elements such as text, image and table. This article demonstrates how to insert table into word textbox, and read and delete existing table from word textbox using Spire.Doc.

Insert table

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
namespace InsertTable
{
    class Program
    {

        static void Main(string[] args)
        {

            //Create a Document instance
            Document document = new Document();

            //Add a section
            Section section = document.AddSection();
            //Add a paragraph to the section
            Paragraph paragraph = section.AddParagraph();

            //Add textbox to the paragraph
            TextBox textbox = paragraph.AppendTextBox(300, 100);

            //Add text to textbox
            Paragraph textboxParagraph = textbox.Body.AddParagraph();
            TextRange textboxRange = textboxParagraph.AppendText("Table 1");
            textboxRange.CharacterFormat.FontName = "Arial";

            //Insert table to textbox
            Table table = textbox.Body.AddTable(true);
            //Specify the number of rows and columns of the table
            table.ResetCells(4, 4);

            string[,] data = new string[,]  
{  
{"Name","Age","Gender","ID" },  
{"John","28","Male","0023" },  
{"Steve","30","Male","0024" },  
{"Lucy","26","female","0025" }  
};

            //Add data to table 
            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    TextRange tableRange = table[i, j].AddParagraph().AppendText(data[i, j]);
                    tableRange.CharacterFormat.FontName = "Arial";
                }
            }

            //Apply style to table
            table.ApplyStyle(DefaultTableStyle.LightGridAccent3);

            //Save the document
            document.SaveToFile("Output.docx", FileFormat.Docx2013);

        }
    }
}

Insert, Read and Delete Table from Word Textbox in C#

Read table

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.IO;
using System.Text;
namespace ReadTable
{
    class Program
    {

        static void Main(string[] args)
        {
            //Create a Document instance and load the word document
            Document document = new Document("Output.docx");

            //Get the first textbox
            TextBox textbox = document.TextBoxes[0];

            //Get the first table in the textbox
            Table table = textbox.Body.Tables[0] as Table;

            StringBuilder sb = new StringBuilder();

            //Loop through the paragraphs of the table and extract text to a .txt file
            foreach (TableRow row in table.Rows)
            {
                foreach (TableCell cell in row.Cells)
                {
                    foreach (Paragraph paragraph in cell.Paragraphs)
                    {
                        sb.AppendLine(paragraph.Text);
                    }
                }
            }
            File.WriteAllText("text.txt", sb.ToString());

        }
    }
}

Insert, Read and Delete Table from Word Textbox in C#

Delete table

using Spire.Doc;
using Spire.Doc.Fields;
namespace DeleteTable
{
    class Program
    {

        static void Main(string[] args)
        {
            //Create a Document instance and load the word document
            Document document = new Document("Output.docx");

            //Get the first textbox
            TextBox textbox = document.TextBoxes[0];

            //Remove the first table from the textbox
            textbox.Body.Tables.RemoveAt(0);

            //Save the document
            document.SaveToFile("RemoveTable.docx", FileFormat.Docx2013);


        }
    }
}

Insert, Read and Delete Table from Word Textbox in C#