Created
March 16, 2023 18:45
-
-
Save bjoerntx/1b9ba4a6a9d98c655873c3efd03d56b9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class TableExtender { | |
public class TabRow { | |
public List<CellInfo> cellInfos = new List<CellInfo>(); | |
} | |
public class CellInfo { | |
public byte[] Text { get; set; } | |
public int Left { get; set; } | |
} | |
public static void ConvertToTabs(this Table table, TextControl textControl) { | |
textControl.BeginUndoAction("Table to tab positions"); | |
// throw exceptions when conditions are not met | |
if (table.NestedTables.Count > 0) | |
throw (new Exception("Nested tables cannot be convertetd.")); | |
if (table.Columns.Count > 14) | |
throw (new Exception("Tables with more than 14 columns cannot be converted to tab positions.")); | |
// create a new list of tab position rows to store positions and texts | |
List<TabRow> tabRows = new List<TabRow>(); | |
// loop through all rows and create a TabRow object for each row | |
for (int row = 1; row <= table.Rows.Count; row++) { | |
TabRow tabRow = new TabRow(); | |
// loop through each column to store the text and position | |
for (int col = 1; col <= table.Columns.Count; col++) { | |
TableCell curCell = table.Cells.GetItem(row, col); | |
byte[] cellText; | |
curCell.Select(); | |
textControl.Selection.Length -= 1; | |
textControl.Selection.Save(out cellText, BinaryStreamType.InternalUnicodeFormat); | |
tabRow.cellInfos.Add(new CellInfo() { Text = cellText, Left = curCell.Position }); | |
} | |
tabRows.Add(tabRow); | |
} | |
// set input position after table | |
TableCell lastCell = table.Cells[table.Rows.Count, table.Columns.Count]; | |
textControl.InputPosition = new InputPosition(lastCell.Start + lastCell.Length); | |
// loop through the created list to add the text at a new tab position | |
foreach (TabRow row in tabRows) { | |
int[] tabPositions = textControl.Selection.ParagraphFormat.TabPositions; | |
int colCounter = 0; | |
foreach (CellInfo cell in row.cellInfos) { | |
// tab positions cannot be 0 | |
tabPositions[colCounter] = cell.Left != 0 ? cell.Left : 1; | |
// apply new tab position | |
textControl.Selection.ParagraphFormat.TabPositions = tabPositions; | |
textControl.Selection.Text = "\t"; // add tab character | |
// load the stored text | |
textControl.Selection.Load(cell.Text, BinaryStreamType.InternalUnicodeFormat); | |
colCounter++; | |
} | |
// new row | |
textControl.Selection.Text = "\r\n"; | |
} | |
// remove the table | |
textControl.InputPosition = new InputPosition(table.Cells[1, 1].Start); | |
textControl.Tables.Remove(); | |
textControl.EndUndoAction(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment