Skip to content

Instantly share code, notes, and snippets.

@bjoerntx
Created June 26, 2024 10:07
Show Gist options
  • Save bjoerntx/9a4ca98482e29df82af1a6f035c8cd93 to your computer and use it in GitHub Desktop.
Save bjoerntx/9a4ca98482e29df82af1a6f035c8cd93 to your computer and use it in GitHub Desktop.
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();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment