Hi there
Our software contains a workflow, where specific ContentControls are copied into another docx File.
This used to work on 15.6.
After we updated to 22.9 a while ago ( we’re now on 25.4 but still face this issue ) the sourcenumbering is messed up.
In attempt to manually patch the numbering I found out that all the paragraphs lists are listed with StartAt: 1 which is incorrect.
The next weird thing behavior lies in the reusage of the nodeimporter.
Whenever I initialize the Nodeimporter it continuously numbers the lists no matter what importFormatMode or Options I provide.
Your Documentation states
// If we set the "KeepSourceNumbering" flag to "true", then the document clone
// list will preserve its original numbering, making the two lists appear as separate lists.
which is not the case.
→ ImportFormatOptions.KeepSourceNumbering | Aspose.Words for .NET
So here is the current behavior:
- One instance of the importer → Continuous Lists
- One Importer for each node to import → Every List starts at 1
- One Importer per document → By pure chance the document is not altered.
I guess in case there is another content control with a new list the numbering will be borked again.
But that is yet to be tested.
I attached a minimal Sample which creates a merged document for each of these 3 cases.
I want the content Controls to be imported as is with no modifications whatsoever.
Templates, styles, etc. should remain unaltered.
NodeimporterSample.zip (61.5 KB)
Kind Regards
Simon
@SimonSchwendele
It seems you are experiencing issues with the NodeImporter
in Aspose.Words, particularly with the KeepSourceNumbering
option not functioning as expected after upgrading to version 22.9 and continuing in 25.4. Here are some insights and potential solutions to address the problems you’re facing:
Issues with Numbering
-
Continuous Lists: When using a single instance of the NodeImporter
, it appears that the lists are being treated as continuous rather than separate. This can happen if the KeepSourceNumbering
option is not being respected.
-
Lists Starting at 1: When you create a new NodeImporter
for each node, the lists are starting at 1, which indicates that the numbering is not being preserved correctly. This behavior can occur if the source document’s list formatting is not being properly imported into the destination document.
Suggested Solutions
-
Use of ImportFormatOptions: Ensure that you are correctly setting the KeepSourceNumbering
option in the ImportFormatOptions
when initializing the NodeImporter
. This option should be set to true
to maintain the original numbering from the source document.
-
Example Code:
Here is a sample code snippet that demonstrates how to set up the NodeImporter
with the KeepSourceNumbering
option:
Document srcDoc = new Document("source.docx");
Document dstDoc = new Document("destination.docx");
ImportFormatOptions importFormatOptions = new ImportFormatOptions();
importFormatOptions.KeepSourceNumbering = true;
NodeImporter importer = new NodeImporter(srcDoc, dstDoc, ImportFormatMode.KeepSourceFormatting, importFormatOptions);
foreach (Paragraph paragraph in srcDoc.FirstSection.Body.Paragraphs)
{
Node importedNode = importer.ImportNode(paragraph, true);
dstDoc.FirstSection.Body.AppendChild(importedNode);
}
dstDoc.Save("merged.docx");
-
Multiple Importers: If you are using multiple instances of the NodeImporter
, ensure that each instance is configured correctly. It may be beneficial to use a single instance for importing all nodes to maintain consistency in list numbering.
Additional Considerations
-
Document Structure: Check the structure of your source document to ensure that the lists are defined correctly. If there are any discrepancies in the list definitions, it may lead to unexpected behavior during the import process.
-
Testing with Different Scenarios: Since you mentioned that the behavior might change with different content controls, it would be prudent to test various scenarios to identify any specific conditions that trigger the issues.
If the problem persists after trying these suggestions, consider reaching out to Aspose support with a minimal reproducible example, as this will help in diagnosing the issue more effectively.
Feel free to ask if you have any further questions or need additional assistance!
@SimonSchwendele Keeping source numbering is tricky and KeepSourceNumbering
option has no analogue in MS Word.
When option KeepSourceNumbering
is set to true , Aspose.Words clones list from the source document with a new list definition identifier, if list with the same identifier already exists in a destination document. So to get expected output you should use one node importer per document. In this case when SDT is imported the second time Aspose.Words will create a new list definition for already existing list and the output will be expected:
[Test]
public void TestImporterPerDocument()
{
Document sourceDoc = new Document(@"C:\Temp\SourceDoc.docx");
Document targetDoc = new Document(@"C:\Temp\TargetDoc.docx");
List<StructuredDocumentTag> sdts = sourceDoc.GetChildNodes(NodeType.StructuredDocumentTag, true).Cast<StructuredDocumentTag>().ToList();
Bookmark bk = targetDoc.Range.Bookmarks["SdtThings"];
NodeImporter importer1 = new NodeImporter(sourceDoc, targetDoc, ImportFormatMode.KeepDifferentStyles, new ImportFormatOptions { KeepSourceNumbering = true });
NodeImporter importer2 = new NodeImporter(sourceDoc, targetDoc, ImportFormatMode.KeepDifferentStyles, new ImportFormatOptions { KeepSourceNumbering = true });
Paragraph bkParent = (Paragraph)bk.BookmarkStart.GetAncestor(NodeType.Paragraph);
foreach (StructuredDocumentTag tag in sdts)
bkParent.ParentNode.InsertAfter(importer1.ImportNode(tag, true), bkParent);
foreach (StructuredDocumentTag tag in sdts)
bkParent.ParentNode.InsertAfter(importer2.ImportNode(tag, true), bkParent);
targetDoc.Save(@"C:\Temp\out_ImporterPerDocument.docx");
}
Okay I can do this thanks for the help.
However what is the issue with the incorrect listnumbering ?
When you have a look at the “ReapplyNumbering” Method ( as well as the console output ) you see that every list in every paragraph always starts at 1.
@SimonSchwendele
This occurs because in this case every paragraph belongs to a separate list in MS Word document. When you create NodeImporter
for each node import operation and the imported node is a list item, Aspose.Words creates a clone of the list in the target document and expectedly the firs item of the cloned list has 1 as list label.