ImportNode changes layout of table

Hi all,

We have some code that splits Word documents on sections. For this we iterate over all sections of the original document and use

Node node = workingDoc.ImportNode(sec, true, ImportFormatMode.KeepDifferentStyles);
workingDoc.Sections.Add(node);

to import them into a new document. This works nicely. However, we recently came across a situation where this approach changes the layout of a table. This table has a row where the first columns are not populated (at least this is how it appears on paper). After importing into the new document the populated columns shift to the left, so that the first columns are populated and the last columns remain unpopulated.

I attached a demo containing the problematic document as well as the example code. We’re using Aspose.Words 25.1. In prior versions this worked just fine.

Please advise.

Best regards,
Gerrit
Aspose2572.7z (26.7 KB)

@gbette

It appears that the issue you’re experiencing with the layout of the table after using ImportNode may be related to how the table’s formatting is handled during the import process. Since you mentioned that this worked fine in prior versions, it could be a change in how Aspose.Words processes table layouts.

Here are a few suggestions to troubleshoot and potentially resolve the issue:

  1. Check Table Properties: Ensure that the table properties (such as width, alignment, and cell margins) are correctly set in the original document. These properties can significantly affect how the table is rendered in the new document.

  2. Use ImportFormatMode.KeepSourceFormatting: Instead of ImportFormatMode.KeepDifferentStyles, try using ImportFormatMode.KeepSourceFormatting. This may help retain the original formatting of the table more accurately.

    Node node = workingDoc.ImportNode(sec, true, ImportFormatMode.KeepSourceFormatting);
    workingDoc.Sections.Add(node);
    
  3. Manual Adjustment: If the layout still shifts, consider manually adjusting the table properties after importing. You can iterate through the rows and cells of the table to set their widths or other properties as needed.

  4. Update Aspose.Words: If possible, consider updating to a newer version of Aspose.Words, as there may have been bug fixes or improvements related to table handling.

If these suggestions do not resolve the issue, consider sharing a minimal reproducible example on the Aspose forums for more targeted assistance.

Best regards!

@gbette The problem is not reproducible with the latest 25.10 version of Aspose.Words. Here are output documents produced by your code with the latest version on my side:
in.1.docx (17.3 KB)
in.2.docx (9.8 KB)

So, please try using the latest version and let us know if the problem still persists on your side.

Also, I would suggest you to clone original document instead of creating a new document. Use this:

workingDoc = (Document)srcDoc.Clone(false);

instead of this

workingDoc = new Document();

In this case you can use ImportFormatMode.UseDestinationStyles and keep original document styles in your output documents.

@alexey.noskov Thanks! I was just able to confirm that version 25.10 resolves the issue. Also thanks for your suggestion. We’re adapting this.

1 Like