Free Support Forum - aspose.com

Help Needed Cloning Tables into existing single Table Cell

Hi,

I need help cloning existing tables (OffenseHeader.docx and OffenseDetail.docx) a variable number of times (1 to n) into a specific cell of an existing table (OffenseTab.docx) with the result of the entire operation expected to look like OffenseTabResult.docx.

I would also like to control the row lengths of all the rows in the subtable.

Hi Douglas,


Thanks for your inquiry.

Aspose.Words provides functionality for easy copying and moving fragments between Microsoft Word documents. This is known as ‘importing nodes’. Before you can insert a fragment from one document into another, you need to ‘import’ it. Importing creates a deep clone of the original node, ready to be inserted into the destination document. Please use the following code to achieve this:

Document docOffenseHeader = new Document(@“C:\temp\OffenseHeader.docx”);

Document docOffenseDetail = new Document(@"C:\temp\OffenseDetail.docx");

Document doc = new Document(@"C:\temp\OffenseTab.docx");

Table tab1 = docOffenseHeader.FirstSection.Body.Tables[0];

Table tab2 = docOffenseDetail.FirstSection.Body.Tables[0];

NodeImporter importer1 = new NodeImporter(docOffenseHeader, doc, ImportFormatMode.KeepSourceFormatting);

NodeImporter importer2 = new NodeImporter(docOffenseDetail, doc, ImportFormatMode.KeepSourceFormatting);

Node dstNode1 = importer1.ImportNode(tab1, true);

Node dstNode2 = importer2.ImportNode(tab2, true);

Cell targetCell = doc.FirstSection.Body.Tables[0].FirstRow.Cells[1];

targetCell.AppendChild(dstNode1);

targetCell.AppendChild(dstNode2);

targetCell.AppendChild(dstNode1.Clone(true));

targetCell.AppendChild(dstNode2.Clone(true));

targetCell.AppendChild(dstNode1.Clone(true));

targetCell.AppendChild(dstNode2.Clone(true));

doc.Save(@"C:\Temp\more\out.docx");


I hope, this helps.

Best regards,

Awais,

Thanks for your help, this is what I needed!

Doug

Hi Doug,


Thanks for your feedback. Please let us know any time you have any further queries. We are always glad to help you.

Best regards,

Hi Awais,

I ran into a problem when the number of tables I was cloning into the cell gets longer than what will fit on a page.

In the attached example, I have added 8 offenses which results in 16 tables getting inserted into the cell. The table content is confirmed when I use the Document Explorer. In the attached file, you will see that when I open the file or try to print it, the data gets cut off after the 6th offense.

Can you please help with a technique to get the table to split and span pages when too long for a single page?

Regards, Doug

Hi Doug,


Thanks for your inquiry. Sure, you can specify true to RowFormat.AllowBreakAcrossPages property and allow the text in a table row to split across a page. Here is the code:

Document docOffenseHeader = new Document(@“C:\temp\OffenseHeader.docx”);

Document docOffenseDetail = new Document(@“C:\temp\OffenseDetail.docx”);<o:p></o:p>

Document doc = new Document(@“C:\temp\OffenseTab.docx”);<o:p></o:p>

Table tab1 = docOffenseHeader.FirstSection.Body.Tables[0];<o:p></o:p>

Table tab2 = docOffenseDetail.FirstSection.Body.Tables[0];<o:p></o:p>

NodeImporter importer1 = new NodeImporter(docOffenseHeader, doc, ImportFormatMode.KeepSourceFormatting);<o:p></o:p>

NodeImporter importer2 = new NodeImporter(docOffenseDetail, doc, ImportFormatMode.KeepSourceFormatting);<o:p></o:p>

Node dstNode1 = importer1.ImportNode(tab1, true);<o:p></o:p>

Node dstNode2 = importer2.ImportNode(tab2, true);<o:p></o:p>

Cell targetCell = doc.FirstSection.Body.Tables[0].FirstRow.Cells[1];<o:p></o:p>

<o:p></o:p>

targetCell.AppendChild(dstNode1);<o:p></o:p>

targetCell.AppendChild(dstNode2);<o:p></o:p>

<o:p></o:p>

targetCell.AppendChild(dstNode1.Clone(true));<o:p></o:p>

targetCell.AppendChild(dstNode2.Clone(true));<o:p></o:p>

targetCell.AppendChild(dstNode1.Clone(true));<o:p></o:p>

targetCell.AppendChild(dstNode2.Clone(true));<o:p></o:p>


targetCell.ParentRow.RowFormat.AllowBreakAcrossPages = true;

<o:p></o:p>

doc.Save(@“C:\Temp\out.docx”);


I hope, this helps.

Best regards,

Hi Awais,

I ran into a problem with the text orientation changing when I saved the document as a PDF. I added this statement to my program

doc.Save("C:\temp\OffenseTabResult.pdf", SaveFormat.Pdf)

and found that the orientation of the text in Cell 1 of the first row changed from the document saved as a docx or printed. Please see attached .pdf file.

This behavior only appears when the table spans to the next page. The text prints and saves as a .pdf just fine when it does not span.

Doug

Hi Doug,


Thanks for your inquiry.

While using the latest version of Aspose.Words i.e. 13.2.0, I managed to reproduce this issue on my side. I have logged this issue in our bug tracking system. The issue ID is WORDSNET-8042. Your request has also been linked to this issue and you will be notified as soon as it is resolved.

Sorry for the inconvenience.

Best regards,