Free Support Forum - aspose.com

Insert table into a table cell

Hi, I was trying to insert a table into another table cell. I was able to do it by creating a new row, but not able to insert into existing table cell. Here is the code I was using:

Document docTemplate = new Document(“E:\template.docx”);
Document docOutput = new Document();

Bookmark bkPainProblemHeader = docTemplate.Range.Bookmarks[“maintable”];
Aspose.Words.Tables.Table tblPainProblemHeader = (Aspose.Words.Tables.Table)bkPainProblemHeader.BookmarkStart.GetAncestor(NodeType.Table);
NodeImporter nodePainProblemHeader = new NodeImporter(docTemplate, docOutput, ImportFormatMode.KeepSourceFormatting);
Node newPainProblemHeader = nodePainProblemHeader.ImportNode(tblPainProblemHeader, true);
docOutput.FirstSection.Body.AppendChild(newPainProblemHeader);

Node childTable = tblPainProblemHeader.GetChild(NodeType.Table, 0, true);
childTable = nodePainProblemHeader.ImportNode(childTable, true);

//docOutput.FirstSection.Body.AppendChild(childTable1);
//I can able to do it using this code
Aspose.Words.Tables.Row row = new Aspose.Words.Tables.Row(docOutput);
((Aspose.Words.Tables.Table)newPainProblemHeader).AppendChild(row);
Aspose.Words.Tables.Cell cell1 = new Aspose.Words.Tables.Cell(docOutput);
row.AppendChild(cell1);
cell1.AppendChild(childTable);
//((Aspose.Words.Tables.Table)newPainProblemHeader).Rows[1].Cells[0].LastParagraph.AppendChild(new Run(docOutput, “Row 1, Cell 1 Text”));
//************ but, this line of code is failing, how can I fix it. I wanted to insert the table into existing table cell instead of creating one. //((Aspose.Words.Tables.Table)newPainProblemHeader).Rows[1].Cells[0].LastParagraph.AppendChild(childTable1);

docOutput.Save(“E:\outputdoc.docx”, SaveFormat.Docx);

Can you help me out on this?

Hi,

I was also trying the same example by modifying the template slightly, where last row, last two columns are merged. But the output document have the issues with merged cells. I don’t know whether this is a Bug or some issue with my template? I have attached both, input and output files here…

Hi Srinu,

Thanks for your inquiry. I have tested the scenario using latest version of Aspose.Words for .NET 15.5.0 and have not found the shared issue. Please use Aspose.Words for .NET 15.5.0. I have attached the output documents with this post for your kind reference.

Please let us know if you have any more queries.

Hi,

Thanks for your response. Did you able to do the same with this comment line,

//************ but, this line of code is failing, how can I fix it. I
wanted to insert the table into existing table cell instead of creating
one.
//((Aspose.Words.Tables.Table)newPainProblemHeader).Rows[1].Cells[0].LastParagraph.AppendChild(childTable1);

I wanted to know why the component is throwing here and what is the fix for it?