Convert table to text

Is there any way using Aspose to easily convert a table to text, as with MS Word’s convert table to text feature?
Thanks.

Hello
Thanks for your request. You should just set PreserveTableLayout option.

Document doc = new Document("in.docx");
TxtSaveOptions opt = new TxtSaveOptions();
opt.PreserveTableLayout = true;
// Save to txt
doc.Save("out.txt", opt);

Best regards,

I want to keep everything in the document the same & just find certain tables & convert them to text. I used the following code, which accomplishes what I need, although it may not be written well.

Thank you.

for (int tableInd = 0; tableInd <tables.Length; tableInd++)
{
    Aspose.Words.Tables.Table table = (Aspose.Words.Tables.Table) tables[tableInd];
    NodeCollection firstRowNodes = table.FirstRow.GetChildNodes(NodeType.Run, true);
    Run firstRun = (Run) firstRowNodes[0];
    if (firstRun.Text != "Sports")
    {
        DB.MoveTo(tables[tableInd].NextSibling);
        foreach(Aspose.Words.Tables.Row row in table.Rows)
        {
            NodeCollection rowNodes = row.GetChildNodes(NodeType.Run, true);
            string rowText = "";
            foreach(Node node in rowNodes)
            {
                rowText += ((Run) node).Text + " ";
            }
            DB.Writeln(rowText);
        }
        table.Remove();
    }
}

Hello
Thanks for your inquiry. Your code looks correct, you can use it. But as a variant you can try using the following code:

Document doc = new Document("C:\\Temp\\in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
Node[] tables = doc.GetChildNodes(NodeType.Table, true).ToArray();
foreach(Table table in tables)
{
    // Create new paragraph for string table
    Paragraph par = new Paragraph(doc);
    // Inser this paragraph after table
    table.ParentNode.InsertAfter(par, table);
    builder.MoveTo(par);
    builder.Font.Name = "Courier New";
    // Insert string table
    builder.Writeln(ConvertTable(table));
    table.Remove();
}
// Save
doc.Save("C:\\Temp\\out.doc");
///
/// Method converts table to string
///
/// input table
/// String that represents content of input table
private static string ConvertTable(Table tab)
{
    string output = string.Empty;
    // Calculate max string length of each table column
    ArrayList columnWidhs = new ArrayList();
    int tableWidth = 0;
    string horizontalBorder = string.Empty;
    // Loop through all rows in table
    foreach(Row row in tab.Rows)
    {
        // Loop througth all cells in current row
        foreach(Cell cell in row.Cells)
        {
            int cellIndex = row.Cells.IndexOf(cell);
            if (columnWidhs.Count> cellIndex)
            {
                if ((int) columnWidhs[cellIndex] <cell.ToTxt().Length)
                {
                    columnWidhs[cellIndex] = cell.ToTxt().Length;
                }
            }
            else
            {
                columnWidhs.Add(cell.ToTxt().Length);
            }
        }
    }
    // Calculate width of table
    for (int index = 0; index <columnWidhs.Count; index++)
    {
        tableWidth += (int) columnWidhs[index];
    }
    tableWidth += columnWidhs.Count;
    // Build horizontal border
    for (int index = 0; index <tableWidth; index++)
    {
        horizontalBorder += "-";
    }
    horizontalBorder += "\r\n";
    // Insert "Top Border"
    output += horizontalBorder;
    // Loop through all rows in table
    foreach(Row row in tab.Rows)
    {
        string currentRow = "|";
        // Loop througth all cells in current row
        foreach(Cell cell in row.Cells)
        {
            int cellIndex = row.Cells.IndexOf(cell);
            // Remove line breaks from cell text
            string curentCell = cell.ToTxt().Replace("\r", " ").Replace("\n", " ");
            // Insert white spaces to the end of cell text
            while (curentCell.Length <(int) columnWidhs[cellIndex])
            {
                curentCell += " ";
            }
            // Insert "Vertical border"
            currentRow += curentCell + "|";
        }
        output += currentRow + "\r\n";
        // Insert "horizontal Border"
        output += horizontalBorder;
    }
    return output;
}

Best regards,

A post was split to a new topic: Convert Table in Word document to Text