Section numbers not coming as expected when using NodeImporter and KeepSourceFormatting mode

Dear Aspose Team,
I tried to insert contents of source word document into a target word document at a bookmark place using the following code provided by Aspose:
https://docs.aspose.com/words/java/insert-and-append-documents/
My source and target documents have certain sections with section numbers in it.
The Target document looks like as given below:

  1. SectionTA

[SectionsInsertionPlace]
2. SectionTB
3. SectionTC

The Source document looks like as given below:

  1. SectionSA
    1.1 SectionSAChild

Now I want to insert sections from source document into target document at a bookmark “SectionsInsertionPlace”. I used NodeImporter class and need to use KeepSourceFormatting import mode.
I got the following final document after insert document:

  1. SectionTA
  2. SectionSA
    1.1 SectionSAChild
  3. SectionTB
  4. SectionTC

But the expected final document should be:

  1. SectionTA
  2. SectionSA
    2.1 SectionSAChild
  3. SectionTB
  4. SectionTC

How can I accomplish this? How can I get desired section numbers in final document?
Do I need to modify word template document?
Many Thanks,
Chandra

Hi Chandra,

Thanks for your inquiry.

When you copy content from one document to another and your source and destination documents contain lists, Aspose.Words copies list from the source document to the destination. That is why numbering of items will not continue. If you need that numbering continues, you should apply numbering in source and destination documents using styles and use UseDestinationStyles option upon copying content from one document to another.

In this case, if names of styles you applied to the paragraphs are the same, the style from the source document will not be copied into the destination document. The copied paragraphs will use styles from the destination document. And since you applied numbering using paragraph styles, the paragraphs from the destination document and the copied paragraphs will become members of the same list and numbering will continue.

I would suggest you please read the following article for more details:
https://docs.aspose.com/words/net/working-with-lists/

I hope, this helps.

Best regards,

Thanks for this information.
But we need to use KeepSourceFormatting setting instead of UseDestinationStyles.
For UseDestinationStyles it’s working already.
Will this solution, you proposed, work with KeepSourceFormatting setting?
Thanks,
Chandra

Hi,
I just used ImportFormatMode.KeepSourceFormatting instead of UseDestinationStyles and I’m getting same issue that I reported initially. When I use UseDestinationStyles then it works fine for me.
We need to use KeepSourceFormatting option. How to proceed now?
Thanks,
Chandra

Hi Chandra,

Thanks for your inquiry. As mentioned here, when you specify ImportFormatMode.KeepSourceFormatting, the lists from source document will retain their original numbering. The reason this is happening appears to be because those affected lists are actually separate (two different lists). This messes with the numbering upon importing. You can see there are two separate lists as clicking on the affected numbering in Microsoft Word will highlight the second entry gray, but not the rest in the list. To avoid this I would suggest trying to remake the lists in final document so that ultimately you have one combined list.
https://reference.aspose.com/words/net/aspose.words.lists/

Best regards,

Hi,
Can you please provide some tips (sample code will be a great help) on how to remake the desired lists in final document?

  1. Do I need to create a new List object?
  2. Or, I need to manipulate existing List objects in final document? I need to get List object using style identifier?

Thanks,
Chandra

Hi Chandra,

Thanks for your inquiry.

Please find attached a couple of input documents and the output document. The ‘srcDoc.docx’ contains a list displaying up to two levels. I have defined a custom styles for the first two levels named ‘MyListStyle’ and ‘MyListStyle1’. Similarly, the ‘dstDoc.docx’ contains another list displaying only the first level; but, again I have defined custom styles for the first two levels named ‘MyListStyle’ and ‘MyListStyle1’ (style names are same to those defined in source document which is useful when you want to use UseDestinationStyles option) and of course there is a bookmark in this document where we will be inserting list from source document at. Now, use the following code snippet to generate the final output:

Document dstDoc = new Document(@"C:\Temp\dstDoc.docx");
Document srcDoc = new Document(@"C:\Temp\srcDoc.docx");
Node insertAfterNode = dstDoc.Range.Bookmarks["SectionsInsertionPlace"].BookmarkStart.ParentNode;
if ((!insertAfterNode.NodeType.Equals(NodeType.Paragraph)) &
    (!insertAfterNode.NodeType.Equals(NodeType.Table)))
    throw new ArgumentException("The destination node should be either a paragraph or table.");
CompositeNode dstStory = insertAfterNode.ParentNode;
NodeImporter importer = new NodeImporter(srcDoc, insertAfterNode.Document, ImportFormatMode.KeepSourceFormatting);
foreach(Section srcSection in srcDoc.Sections)
{
    foreach(Node srcNode in srcSection.Body)
    {
        if (srcNode.NodeType.Equals(NodeType.Paragraph))
        {
            Paragraph para = (Paragraph) srcNode;
            if (para.IsEndOfSection && !para.HasChildNodes)
                continue;
        }
        Node newNode = importer.ImportNode(srcNode, true);
        dstStory.InsertAfter(newNode, insertAfterNode);
        insertAfterNode = newNode;
    }
}
dstDoc.Range.Bookmarks["SectionsInsertionPlace"].BookmarkStart.ParentNode.Remove();
dstDoc.Lists[1].ListLevels[0].LinkedStyle.Name = "MyListStyle";
dstDoc.Lists[1].ListLevels[1].LinkedStyle.Name = "MyListStyle1";
dstDoc.Save(@"C:\Temp\out.docx");

I hope, this helps.

Best regards,