Products Technologies Demo Docs Blog Support Company

Extension Method: Converting Tables to Tabs in C#

When you export documents to formats that don't support tables, such as ANSI text, you can convert those tables to tabs. This article describes how to convert a table object to tabs using a C# extension method.

Extension Method: Converting Tables to Tabs in C#

Extension methods add methods to existing members without inheriting, recompiling, or otherwise modifying the original type. We use this concept to create a method that extends the Table object in TX Text Control. The method ConvertToTabs converts a table to tab positions.

Table to Tabs

Table Extension Method

The following code shows the extension method:

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();
  }
}

This method can be called on any table with fewer than 15 columns and no nested tables. The following code calls the extension method on the table at the current input position:

textControl1.Tables.GetItem().ConvertToTabs(textControl1);

How does it Work?

The method basically loops through all table rows and columns to store the position of each table cell and the text it contains in the internal TX Text Control format. In a second loop through this created list, tab positions are applied and the text is loaded separated by a tab character.

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

Windows Forms

Text Control combines the power of a reporting tool and an easy-to-use WYSIWYG word processor - fully programmable and embeddable in your Windows Forms application. TX Text Control .NET for Windows Forms is a royalty-free, fully programmable rich edit control that offers developers a broad range of word processing features in a reusable component for Visual Studio.

See Windows Forms products

Related Posts

ASP.NETWindows FormsWPF

Inserting MergeBlocks with the DataSourceManager and Applying Table Styles in C#

This article shows how to insert MergeBlocks with the DataSourceManager and how to apply table styles to those tables. The article uses the DocumentServer class to insert MergeBlocks with the…


ASP.NETWindows FormsWPF

Useful Tricks for Working with Tables

When you are working with complex and long tables, it can be useful to know if a table is broken up across multiple pages. These code snippets will help you with various table related tasks.


ASP.NETWindows FormsWPF

Generating Hierarchical Tables from JSON Data in .NET C#

Using TX Text Control, you can generate complex hierarchical tables directly from JSON data. This article explains the code and logic behind it.


ASP.NETWindows FormsWPF

Formatting Numbers in Table Cells

Table cell text can be interpreted as plain text or as a number with an associated number format. This article explains how to use this format and gives examples for various string formatters.


ASP.NETJavaScriptWindows Forms

Auto-Sizing Tables Proportionally

This article shows how to resize a table to fit the complete page by adjusting each column proportionally.