We have some code that imports tables rows from one document into another and appends them to a table. Both tables have the same total width. However they have a different number of columns.
We’re doing it like this:
static void AppendTable(Document targetDoc, string docWithTable)
{
Table targetTable = (Table)targetDoc.FirstSection.GetChild(NodeType.Table, 0, true);
Document sourceDoc = new Document(docWithTable);
Table sourceTable = (Table)sourceDoc.FirstSection.GetChild(NodeType.Table, 0, true);
//Import this table into the destination document
var sourceTableImported = (Table)targetDoc.ImportNode(sourceTable, true, ImportFormatMode.KeepDifferentStyles);
//Insert table into the destination document
while (sourceTableImported.HasChildNodes)
{
targetTable.Rows.Add(sourceTableImported.FirstRow);
}
}
In the past the width of each column was kept. Leading to merge cells. For example if you have a layout like this:
targetDoc
col0 | col1 | col2 | col3
docToBeImported
col0 | col1 | col2 | col3 | col4 | col5
and col1, col2 and col3 of docToBeImported in total have the same width as col1 of targetDoc the following table layout was created:
meaning col1 of the first row spans three columns.
It seems the behavior changed with Aspose.Words 25.10. When we now run the code it results in the following layout.
with the first row having 4 columns and the second row having 6.
I attached example code and documents. We’d like to revert back to the former behavior with the column span (cellmerge) applied. How can we achieve that?
Thanks in advance and best regards,
Gerrot Aspose2598.7z (6.3 KB)
The change you see is caused by the way AutoFit is applied to tables in 25.10. In recent releases the default behavior switched to AutoFit to contents, which recalculates column widths after rows are added and therefore removes the implicit column‑span that was created when the total width of the source columns matched a single target column.
How to restore the previous layout
Disable AutoFit for the target table before you start appending rows
// targetDoc – the document that already contains the destination table
Table targetTable = (Table)targetDoc.FirstSection.GetChild(NodeType.Table, 0, true);
targetTable.AllowAutoFit = false; // or
targetTable.AutoFit(AutoFitBehavior.FixedColumnWidths);
(Optional) Preserve the exact column widths of the source table
If the source table uses fixed column widths, copy them to the target table once, e.g.:
foreach (TableColumn srcCol in sourceTable.Columns)
{
targetTable.Columns[srcCol.Index].PreferredWidth = srcCol.PreferredWidth;
}
Proceed with the import as you already do
Document sourceDoc = new Document(docWithTable);
Table sourceTable = (Table)sourceDoc.FirstSection.GetChild(NodeType.Table, 0, true);
Table sourceTableImported = (Table)targetDoc.ImportNode(sourceTable, true,
ImportFormatMode.KeepDifferentStyles);
while (sourceTableImported.HasChildNodes)
targetTable.Rows.Add(sourceTableImported.FirstRow);
With AllowAutoFit = false (or AutoFit(AutoFitBehavior.FixedColumnWidths)) the column widths remain unchanged after each row insertion, and the first row will again span the three columns as it did in earlier versions.
What we need to confirm
The exact Aspose.Words version you used before the change (e.g., 25.9 or earlier).
The version you are currently testing (you mentioned 25.10).
A minimal reproducible document (if the above steps do not resolve the issue) so we can verify the behavior on our side.
Please try the steps above and let us know the results, along with the version details and a sample file if the problem persists.
@gbette The problem is that table grid is defined per table. So When you copy rows form one table to another table, table grid of the target table is used. You can try modifying the code like this to work the problem around:
static void AppendTable(Document targetDoc, string docWithTable)
{
Table targetTable = (Table)targetDoc.FirstSection.GetChild(NodeType.Table, 0, true);
Document sourceDoc = new Document(docWithTable);
Table sourceTable = (Table)sourceDoc.FirstSection.GetChild(NodeType.Table, 0, true);
//Import this table into the destination document
var sourceTableImported = (Table)targetDoc.ImportNode(sourceTable, true, ImportFormatMode.KeepSourceFormatting);
targetTable.ParentNode.InsertAfter(sourceTableImported, targetTable);
}
Upon saving the document Aspose.Words merges the adjacent tables taking table grids into account, just like MS Word does. out.zip (4.7 KB)