Products Technologies Demo Docs Blog Support Company

TX Text Control .NET for Windows Forms: Converting a Table to Text

TX Text Control .NET for Windows Forms exposes Table and TableCell classes that simplify converting tables to plain text. A C# method iterates every cell, accumulates row strings with a configurable separator such as tab, removes the table, then inserts the resulting lines as text.

TX Text Control .NET for Windows Forms: Converting a Table to Text

I just found an interesting solution from our support staff while reviewing our support cases of the last week. A user was trying to convert a table into normal text.

Thanks to TX Text Control's easy-to-use Table and TableCell classes, this task is quite easy to solve. The following method converts every 1-dimensional (not nested) table to normal text. The table and the separator must be passed in the parameters of the method.

private void ConvertTableToText(Table Table, string Separator)
{
    if (Table == null)
        return;

    string sRowText = String.Empty;
    string[] sTableText = new string[Table.Rows.Count];
    int iCurRow = 1;

    // loop through all cells to get the text for each line
    foreach (TableCell tcCell in textControl1.Tables.GetItem(13).Cells)
    {
        if (tcCell.Row != iCurRow)
        {
            sTableText.SetValue(sRowText, iCurRow - 1);
            sRowText = "";
        }

        // add the cell\'s text including a separator to the line\'s text
        sRowText += tcCell.Text + Separator;
        iCurRow = tcCell.Row;
    }

    // last row
    sTableText.SetValue(sRowText, iCurRow - 1);

    // remove the table
    textControl1.Tables.Remove();

    // insert each line of text
    foreach (string sLine in sTableText)
    {
        textControl1.Selection.Text = sLine + "
";
    }
}

This method can be called as shown below to convert the table at the current input position. The separator between the cell's text is a tab character in this case to simulate a table layout:

ConvertTableToText(textControl1.Tables.GetItem(), "        ");{/literal}

Technically, a loop through all table cells is used to create the strings for each line of the table. At the end, the table is removed and replaced with the text of each table cell.

This particular user wanted to save the content as plain text, so that the formatting of the text could be ignored. To save the formatting, an array of formatted text snippets should be created by selecting the table cell's text and using the Save method to save in an appropriate format.

Feel free to leave your comments or questions on this.

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

Related Posts

.NETSampleTables

Revised Sample: Converting Text to a Table

TX Text Control .NET for Windows Forms supports splitting plain text into a table using delimiter characters such as commas or hyphens. This sample, migrated from Visual Studio 2003 to 2005,…


.NETSampleTables

Apply a Boxed Frame Style to Selected Cells

This sample demonstrates how to programmatically apply a boxed border style to several selected table cells in TX Text Control. The code adjusts each individual cell border to replicate the…


Windows FormsGitHubSample

Change Table Width to Fit Document Width - Automatically

This sample application shows how to automatically adjust a table width to match the full document width in TX Text Control, similar to fluid layouts in web browsers. The code uses resize events…


ASP.NETASP.NET CoreCSV

Convert CSV to PDF in .NET C#

Learn how to convert CSV data to a table in C# using the ServerTextControl library with this step-by-step tutorial. Easily generate PDF documents from CSV files in your .NET applications.


ASP.NETASP.NET CorePDF

Why Table Control in Templates is Important for Professional PDF Creation in C#

Controlling how tables behave at page breaks is an important factor in creating professional-looking documents. This article discusses the importance of table control in templates for PDF generation.

Share on this blog post on: