We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Combining tables disfeatures table layout

Hi,

when I try to combine two tables in a single Worddocument with the code you have provided in your programmer’s guide:

// Load the document.
Document doc = new Document(getMyDir() + “Table.Document.doc”);

// Get the first and second table in the document.
// The rows from the second table will be appended to the end of the first table.
Table firstTable = (Table)doc.getChild(NodeType.TABLE, 0, true);
Table secondTable = (Table)doc.getChild(NodeType.TABLE, 1, true);

// Append all rows from the current table to the next.
// Due to the design of tables even tables with different cell count and widths can be joined into one table.
while (secondTable.hasChildNodes())
firstTable.getRows().add(secondTable.getFirstRow());

// Remove the empty table container.
secondTable.remove();

doc.save(getMyDir() + “Table.CombineTables Out.doc”);

with either of the two table structures as seen in ‘TableOne.docx’ and ‘TableTwo.docx’ the resulting worddocuments are ‘TableOneResult.docx’ and ‘TableTwoResult.docx’. The desired output would have been ‘TableOneCorrect.docx’ and ‘TableTwoCorrect.docx’.

Is there are possibility to achieve this or is it a bug?
Thank you so much in advance.

Best regards
Hi Dolten,

Thanks for your inquiry. We have tested the scenario and have managed to reproduce the same issue at our side. For the sake of correction, we have logged this problem in our issue tracking system as WORDSNET-12938. You will be notified via this forum thread once this issue is resolved.

We apologize for your inconvenience.

Hi Dolten,

Thanks for your patience.

To fix the shared issue, please use Table.AutoFit(AutoFitBehavior.FixedColumnWidths) method. Hope this helps you.

Document doc = new Document(MyDir + "TableOne.docx");

Table firstTable = (Table)doc.GetChild(NodeType.Table, 0, true);

Table secondTable = (Table)doc.GetChild(NodeType.Table, 1, true);

while (secondTable.HasChildNodes)

firstTable.Rows.Add(secondTable.FirstRow);

firstTable.AutoFit(AutoFitBehavior.FixedColumnWidths);

// Remove the empty table container.

secondTable.Remove();

doc.Save(MyDir + "TableOne Out.docx");