# Text to Table and Table to Text in TX Text Control and C#

> TX Text Control provides powerful table features and also full access to text formatting which can be used to create tables from text and vice versa. This article shows how to convert text to tables and tables to text.

- **Author:** Bjoern Meyer
- **Published:** 2024-06-26
- **Modified:** 2025-11-16
- **Description:** TX Text Control provides powerful table features and also full access to text formatting which can be used to create tables from text and vice versa. This article shows how to convert text to tables and tables to text.
- **3 min read** (479 words)
- **Tags:**
  - ASP.NET
  - Windows Forms
  - Tables
  - Tab Stops
- **Web URL:** https://www.textcontrol.com/blog/2024/06/26/text-to-table-and-table-to-text-in-tx-text-control-and-csharp/
- **LLMs URL:** https://www.textcontrol.com/blog/2024/06/26/text-to-table-and-table-to-text-in-tx-text-control-and-csharp/llms.txt
- **LLMs-Full URL:** https://www.textcontrol.com/blog/2024/06/26/text-to-table-and-table-to-text-in-tx-text-control-and-csharp/llms-full.txt

---

A typical task in document layout is to convert plain text tables created with tab stops into fully formatted tables. In this article we will show you how to convert plain text tables to tables by detecting the tab stops and converting them to table cells including the positions.

### Table to Text

Let's start with a simple table in TX Text Control:

![Table in TX Text Control](https://s1-www.textcontrol.com/assets/dist/blog/2024/06/26/a/assets/table1.webp "Table in TX Text Control")

To convert the table, we need to loop through all the rows and columns and create line strings that contain the text of each table cell separated by tab stops. The following code shows how to convert a table to a plain text table with tab stops:

```
private void ConvertTableToText(TXTextControl.Table table, TXTextControl.TextControl textControl)
{
	if (table.NestedTables.Count >= 1)
	{
		// throw an exception
		throw new Exception("Nested tables are not supported.");
	}

	// Create a new StringBuilder object
	StringBuilder sb = new StringBuilder();

	var numRows = table.Rows.Count;
	var numCols = table.Columns.Count;

	int[] tabStopLocations = new int[14];

	// Loop through all rows
	for (int i = 1; i <= numRows; i++)
	{
		var offset = 0;

		// Loop through all columns
		for (int j = 1; j <= numCols; j++)
		{
			var position = table.Columns[j].Position;

			if (j == 1)
			{
				if (position > 0)
				{
					tabStopLocations[j - 1] = position;
				}
				else
				{
					offset = 1;
				}
			}
			else
			{
				tabStopLocations[j - 1 - offset] = position;
			}

			if (j > 1 || (j == 1 && position > 0))
			{
				sb.Append("\t");
			}

			sb.Append(table.Cells[i, j].Text);
		}

		// Append a new line character to the StringBuilder object
		sb.Append("\r\n");
	}

	// Remove table
	table.Select();
	textControl1.Tables.Remove();

	textControl1.Selection.ParagraphFormat.TabPositions = tabStopLocations;

	// Set the text of the TextControl
	textControl1.Selection.Text = sb.ToString();
}
```

The above code uses *StringBuilder* to create the plain text table. Each table row is represented by a line string that contains the text of each table cell separated by tab stops. The TableColumn.Position property is used to get the position of the table column in the table. This position is used to determine the tab stop position in the plain text table.

The following animated screenshot shows the result of the conversion:

![Table in TX Text Control](https://s1-www.textcontrol.com/assets/dist/blog/2024/06/26/a/assets/table1.gif "Table in TX Text Control")

In the next screenshot, you can see how different column sizes are handled and converted to a plain text table:

![Table in TX Text Control](https://s1-www.textcontrol.com/assets/dist/blog/2024/06/26/a/assets/table2.gif "Table in TX Text Control")

### Text to Table

Now, let's convert the plain text table back to a table in TX Text Control. The following code shows how to convert a plain text table with tab stops to a fully formatted table:

```
private void ConvertTextToTable(TXTextControl.TextControl textControl)
{
	if (textControl1.Selection.Length == 0)
	{
		throw new Exception("No text selected.");
	}

	int[] storedTabPositions = textControl1.Selection.ParagraphFormat.TabPositions;

	int startPosition = textControl1.Selection.Start;
	int endPosition = startPosition + textControl1.Selection.Length;

	var startLine = textControl1.Lines.GetItem(startPosition);
	var endLine = textControl1.Lines.GetItem(endPosition - 1);

	List<string> lineText = new List<string>();

	// Loop through all lines and add the text of each line to the list
	for (int i = startLine.Number; i <= endLine.Number; i++)
	{
		var line = textControl1.Lines[i];
		lineText.Add(line.Text);
	}

	// Calculate the number of rows and columns
	int numRows = endLine.Number - startLine.Number + 1;
	int numCols = lineText[0].Split('\t').Length;

	textControl1.Selection.Text = string.Empty;

	// Create a new table
	var tableInserted = textControl1.Tables.Add(numRows, numCols);

	// Set input position to the first cell
	textControl1.Selection.Start -= 1;

	// Loop through all rows and columns to populate the table
	for (int i = 1; i <= numRows; i++)
	{
		string[] splitText = lineText[i - 1].Split('\t');

		for (int j = 1; j <= numCols; j++)
		{
			var tableCell = textControl1.Tables.GetItem().Cells[i, j];

			// Set the text of the cell, and remove the last CRLF
			tableCell.Text = (splitText.Length >= j) ? splitText[j - 1].TrimEnd('\r', '\n') : string.Empty;

			// Set the tab position if it's not the first column and the tab position exists
			if (j > 1 && storedTabPositions.Length >= j - 1 && storedTabPositions[j - 1] != 0)
			{
				tableCell.Position = storedTabPositions[j - 2];
			}
		}
	}
}
```

This is basically the reverse operation of the first code snippet. The plain text table is split into lines and each line is split by tab stops. The tab stop positions are used to create table columns and the text is inserted into the table cells.

The plain text table to be converted must be selected first. The following animated screenshot shows the result of the conversion:

![Table in TX Text Control](https://s1-www.textcontrol.com/assets/dist/blog/2024/06/26/a/assets/table3.gif "Table in TX Text Control")

It is important to note that this only works for simple tables with tab stops. More complex tables with merged cells or different column widths require a more sophisticated algorithm to convert the plain text table into a fully formatted table. However, this simple approach is sufficient for most cases.

### Conclusion

This article showed how to convert plain text tables with tab stops to fully formatted tables in TX Text Control and how to convert tables to plain text tables. The key to this conversion is the detection of tab stops and the correct positioning of the table cells.

---

## About Bjoern Meyer

As CEO, Bjoern is the visionary behind our strategic direction and business development, bridging the gap between our customers and engineering teams. His deep passion for coding and web technologies drives the creation of innovative products. If you're at a tech conference, be sure to stop by our booth - you'll most likely meet Bjoern in person. With an advanced graduate degree (Dipl. Inf.) in Computer Science, specializing in AI, from the University of Bremen, Bjoern brings significant expertise to his role. In his spare time, Bjoern enjoys running, paragliding, mountain biking, and playing the piano.

- [LinkedIn](https://www.linkedin.com/in/bjoernmeyer/)
- [X](https://x.com/txbjoern)
- [GitHub](https://github.com/bjoerntx)

---

## Related Posts

- [Splitting Tables at Bookmark Positions and Cloning Table Headers](https://www.textcontrol.com/blog/2025/02/13/splitting-tables-at-bookmark-positions-and-cloning-table-headers/llms.txt)
- [Loading and Processing Excel XLSX Spreadsheet Tables into TX Text Control using .NET C#](https://www.textcontrol.com/blog/2024/10/16/loading-and-processing-excel-spreadsheet-tables-into-tx-text-control-using-net-csharp/llms.txt)
- [Inserting MergeBlocks with the DataSourceManager and Applying Table Styles in C#](https://www.textcontrol.com/blog/2024/04/09/inserting-mergeblocks-with-the-datasourcemanager-and-applying-table-styles-in-csharp/llms.txt)
- [Useful Tricks for Working with Tables](https://www.textcontrol.com/blog/2023/08/04/useful-tricks-for-working-with-tables/llms.txt)
- [Generating Hierarchical Tables from JSON Data in .NET C#](https://www.textcontrol.com/blog/2023/08/03/generating-hierarchical-tables-from-json-data-in-csharp/llms.txt)
- [Table Extension: Remove Empty Columns After Mail Merge](https://www.textcontrol.com/blog/2023/06/08/table-extension-remove-empty-columns-after-mail-merge/llms.txt)
- [Extension Method: Converting Tables to Tabs in C#](https://www.textcontrol.com/blog/2023/03/16/extension-method-converting-tables-to-tabs-in-csharp/llms.txt)
- [Using Accounting Double Underlining in MailMerge Templates](https://www.textcontrol.com/blog/2023/03/02/use-accounting-double-underlining-in-mailmerge-templates/llms.txt)
- [Formatting Numbers in Table Cells](https://www.textcontrol.com/blog/2022/06/21/formatting-numbers-in-table-cells/llms.txt)
- [Merging Merge Block Cells Vertically with Matching Content](https://www.textcontrol.com/blog/2021/10/20/vertically-merge-table-cells-of-merge-blocks-for-matching-content/llms.txt)
- [Auto-Sizing Tables Proportionally](https://www.textcontrol.com/blog/2021/08/05/auto-sizing-tables-proportionally/llms.txt)
- [Export Document Tables to CSV in .NET C#](https://www.textcontrol.com/blog/2026/06/19/export-document-tables-to-csv-in-dotnet-csharp/llms.txt)
- [TX Text Control 34.0 SP4 is Now Available: What's New in the Latest Version](https://www.textcontrol.com/blog/2026/05/20/tx-text-control-34-0-sp4-is-now-available/llms.txt)
- [TXTextControl.Markdown.Core 34.1.0-beta: Work with Full Documents, Selection, and SubTextParts](https://www.textcontrol.com/blog/2026/04/14/txtextcontrol-markdown-core-34-1-0-beta-work-with-full-documents-selection-and-subtextparts/llms.txt)
- [TX Spell .NET 11.0 SP1 is Now Available: What's New in the Latest Version](https://www.textcontrol.com/blog/2026/04/08/tx-spell-net-11-0-sp1-is-now-available/llms.txt)
- [TX Text Control 34.0 SP2 is Now Available: What's New in the Latest Version](https://www.textcontrol.com/blog/2026/02/18/tx-text-control-34-0-sp2-is-now-available/llms.txt)
- [Convert CSV to PDF in .NET C#](https://www.textcontrol.com/blog/2026/01/19/convert-csv-to-pdf-in-dotnet-csharp/llms.txt)
- [TX Text Control 34.0 SP1 is Now Available: What's New in the Latest Version](https://www.textcontrol.com/blog/2025/12/03/tx-text-control-34-0-sp1-is-now-available/llms.txt)
- [Introducing TX Text Control 34.0: Your Next Leap in Document Processing](https://www.textcontrol.com/blog/2025/11/10/introducing-tx-text-control-34-0-your-next-leap-in-document-processing/llms.txt)
- [Sneak Peek: TX Text Control 34.0 Coming November 2025](https://www.textcontrol.com/blog/2025/10/02/sneak-peek-tx-text-control-34-0-coming-november-2025/llms.txt)
- [TX Text Control 33.0 SP3 is Now Available: What's New in the Latest Version](https://www.textcontrol.com/blog/2025/08/14/tx-text-control-33-0-sp3-is-now-available/llms.txt)
- [TX Text Control 33.0 SP2 is Now Available: What's New in the Latest Version](https://www.textcontrol.com/blog/2025/06/18/tx-text-control-33-0-sp2-is-now-available/llms.txt)
- [Document Lifecycle Optimization: Leveraging TX Text Control's Internal Format](https://www.textcontrol.com/blog/2025/05/16/document-lifecycle-optimization-leveraging-tx-text-controls-internal-format/llms.txt)
- [Expert Implementation Services for Legacy System Modernization](https://www.textcontrol.com/blog/2025/05/07/expert-implementation-services-for-legacy-system-modernization/llms.txt)
- [Service Pack Releases: What's New in TX Text Control 33.0 SP1 and 32.0 SP5](https://www.textcontrol.com/blog/2025/05/07/service-pack-releases-whats-new-in-tx-text-control-33-0-sp1-and-32-0-sp5/llms.txt)
