Products Technologies Demo Docs Blog Support Company

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) {…

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

First of all, I would like to wish all of our users a happy and successful year 2009. I am sure that it will be a fantastic year with a lot of new features and improvements for the TX Text Control…


.NETSampleTables

Apply a Boxed Frame Style to Selected Cells

I have just made a quick sample to illustrate how to adjust the border of several selected table cells. This sample shows how to set a boxed border like in the shipped dialog box for tables. Read…


Windows FormsGitHubSample

Change Table Width to Fit Document Width - Automatically

You probably know this behavior from web browsers, such as FireFox or Microsoft Internet Explorer. You have a table that is 100% the width of a document. As you resized the window in which the…


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.


ASP.NETWindows FormsASP.NET Core

Splitting Tables at Bookmark Positions and Cloning Table Headers

This article shows how to split tables at bookmark positions and how to clone table headers in TX Text Control .NET for Windows Forms and TX Text Control .NET Server.