Free Support Forum - aspose.com

Save as text with tables

When I save a document to text tables in the document are folded to a single column of headers and cell content rather than tabular format. Is there a way to maintain the tables in some resemblance of a table? Word 2007 seems to do the same however prior versions of Word maintained the table.

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. This is known issue #4736.

Issue #4736 – Implement export of tables preserving table layout

As a workaround you can try using custom method for converting document to txt format. For example see the following code:

public void Example004()

{

Document doc = new Document(@"C:\Temp\in.doc");

string text = CustomToTxtConvertor.Convert(doc);

Stream file = new FileStream(@"C:\Temp\out.txt", FileMode.Create);

StreamWriter writer = new StreamWriter(file);

writer.Write(text);

writer.Close();

file.Close();

}

public class CustomToTxtConvertor

{

///

/// Method converts document to string

///

/// Input document

/// String that represents content of input document

public static string Convert(Document doc)

{

string output = string.Empty;

//Loop through all sections in the document

foreach (Section currentSection in doc.Sections)

{

//If section contains primary header we will convert it to txt

if (currentSection.HeadersFooters[HeaderFooterType.HeaderPrimary] != null)

{

CompositeNode primaryHeader = (CompositeNode)currentSection.HeadersFooters[HeaderFooterType.HeaderPrimary];

output += ConvertCompositeNode(primaryHeader);

}

//Now we should convert body content

output += ConvertCompositeNode((CompositeNode)currentSection.Body);

//If section contains primary footer we will convert it to txt

if (currentSection.HeadersFooters[HeaderFooterType.FooterPrimary] != null)

{

CompositeNode primaryFooter = (CompositeNode)currentSection.HeadersFooters[HeaderFooterType.FooterPrimary];

output += ConvertCompositeNode(primaryFooter);

}

//Insert few empty lines between sections

output += "\r\n\r\n\r\n\r\n";

}

return output;

}

///

/// Method converts composite node to text

///

/// CompositeNode like HeaderFooter or Body

/// String that represents content of CompositeNode

private static string ConvertCompositeNode(CompositeNode container)

{

string output = string.Empty;

//Loop through all child nodes of composite node

foreach (Node child in container.ChildNodes)

{

//Convert chield nodes to Text

switch (child.NodeType)

{

case NodeType.Paragraph:

output += child.ToTxt();

break;

case NodeType.Table:

output += ConvertTable((Table)child);

break;

default:

output += child.ToTxt();

break;

}

}

return output;

}

///

/// 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;

}

}

Hope this helps.

Best regards.

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Sorry, I missed that in the latest version of Aspose.Words there is a SaveOptions.TxtExportTableLayout option. So you can use the following code:

Document doc = new Document(@"C:\Temp\in.doc");

doc.SaveOptions.TxtExportTableLayout = true;

doc.Save(@"C:\Temp\out1.txt", SaveFormat.Text);

Best regards.