Replace tables with Paragraphs

I want to replace each table row with paragraph.Also i want the style and format of the content to remain as it is.Can you please help me. Thanks

Hi,

I want to replace tables with pragraph. i.e
For example: I have a table say

1 Male
2 Female

I want to replace it with two paragraphs i.e number of rows
1 Male
2 Female

Also i want the style and formatting of the content to be same.
can you please help me.

Thanks
Timothy Delixus

This message was posted using Page2Forum from Aspose.Words for .NET - Documentation

Hi
Thanks for your request. In your case, you should just move paragraphs from the table outside the table and then remove the table. Please see Aspose.Words DOM for more information:
https://docs.aspose.com/words/net/aspose-words-document-object-model/
Hope this helps. Please let us know if you need more assistance, we will be glad to help you.
Best regards,

Hi,

I tried by taking each cell content and storing it into string builder and removing the table but i am losing the formatting of the content.

Also i am checking if node type is Table , then node.remove() but it removes only that particular table not the remaining tables.

Can you please show some code for doing it, it will be very helpful.

Thanks
Timothy Delixus

Hi
Thanks for your request. Please try using code like the following:

// Open document.
Document doc = new Document(@"Test001\in.doc");
// Get collectionof table.
Node[] tables = doc.GetChildNodes(NodeType.Table, true).ToArray();
// Loop through all tables and replace them with paragraphs.
foreach(Table table in tables)
{
    // Loop through each row of the table.
    foreach(Row row in table.Rows)
    {
        Paragraph par = null;
        // Loop through all cells.
        foreach(Cell cell in row.Cells)
        {
            foreach(Paragraph paragraph in cell.Paragraphs)
            {
                if (par == null)
                {
                    par = paragraph;
                }
                else
                {
                    while (paragraph.HasChildNodes)
                        par.AppendChild(paragraph.FirstChild);
                }
            }
        }
        // Insert a paragraph before the table.
        table.ParentNode.InsertBefore(par, table);
    }
    // Remove the table.
    table.Remove();
}
// Save output
doc.Save(@"Test001\out.doc");

Hope this helps.
Best regards,

Hey Thanks it worked very well.

Now i am caught in another problem.

I have table say

Apple Ball Cat
11 22 33

Now i want it to be displayed as follows:
11 Apple
22 Ball
33 Cat

Can you please help me. It will be very helpful.

Thanks
Timothy Delixus

Hi Timothy,
Thanks for your request. The technique for getting content from cells is the same as in my code example. The only difference is how you combine the content. In my example, I combined content of all cells in the row. In your case, you should combine content of all cells with the same index within all rows.
Best regards,