Create dynamic table with Aspose Word in Dot net

Hello Team,

We have a requirement to create a dynamic table with say 2 or 3 headers and their contents below that as rows. Main thing is that alignment of the rows should be perfect, say if the header 1 content is wrapped to two lines header 2 and 3’s content should also be aligned properly. Please refer the image screenshot for reference.

Image Sample:

Thanks,
Karthikeyan

@Karthik_Test_account You can create and configure table in the template and then use Mail Merge with Regions or LINQ Reporting Engine to fill it with data. In both case data row will be repeated for each item in your data source.

Thank you @alexey.noskov
Also is there a way to hide the border lines of a table?

@Karthik_Test_account Sure you can use CellFormat.Borders to set or reset borders of the table. Please see our documentation for more information:
https://docs.aspose.com/words/net/applying-formatting/#applying-borders-and-shading

@alexey.noskov - I have a scenario where there is a table as follows I will have to read the contents of the table and then modify the content and then update it back to the template. Please see the Actual vs Modified picture. Please share us the code to achieve this in Aspose dot net.

Note: This is only a sample table the actual only will be very complex, hence I need a dynamic code to handle any kind of table

Actual content:

Modified content:

Thanks,
Karthikeyan

@Karthik_Test_account The best way to fill the template with data is using Mail Merge or LINQ Reporting Engine. in both cases you should place special placeholders in the template - merge fields in case of using Mail Merge and <<[...]>> placeholders in case of LINQ Reporting Engine.

@alexey.noskov - Instead of mail merge or LINQ reporting engine, is there a way to read the table content from the Node collections and reading the table rows and columns?

@Karthik_Test_account There is no column concept in MS Word table. You can read table row by row and rows cell by cell. For example see the following code:

Document doc = new Document("in.docx");

// get tables
NodeCollection tables = doc.GetChildNodes(NodeType.Table, true);
foreach (Table t in tables)
{
    // loop through rows.
    foreach (Row r in t.Rows)
    {
        // loop through cells
        foreach (Cell c in r.Cells)
        {
            // Here you can remove old contnt of cell and insert new content.
            // ......
        }
    }
}

@alexey.noskov - I am using the following code to read the content cell by cell. But the variable para is null could you please help here with the code to read the content and update the content.

Document actualDoc; //Read content from file;

NodeCollection tables = actualDoc.GetChildNodes(NodeType.Table, true);
foreach (Table t in tables)
{
    foreach (Row r in t.Rows)
    {
        foreach (Cell c in r.Cells)
        {
            var para = c.ParentNode as Paragraph;
            var paraText = para.ToString(SaveFormat.Text).Trim();
        }
    }
}

@Karthik_Test_account Cell cannot be a child of paragraph. Cell can be a child of row. So c.ParentNode returns Row node. If you need to get content of cell you can use the following code:

var cellText = c.ToString(SaveFormat.Text).Trim();