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 + "\r\n"; } }
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(), "\t");{/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.