Problem for resetting the numbering of List while merging document

We encountered an issue when resetting the list numbering when merging documents. In order to prevent the merged list from continuing the numbering of the target document, we wrote a new processing method called “ResetNumbering” to reset the numbering of the copied list.

However, for the situation in this source document, its five items were split into two lists, resulting in the final generated document being split into two lists starting from 1.

the document source and target as bellow
target.docx (15.3 KB)

source.docx (25.6 KB)

Our expected outcome should be as follows

image.png (57.2 KB)

But the actual situation is like this

image.png (57.2 KB)

bellow is the codes.

public void CombinedDocument()
{
    var subDoc = new Document("c:\\temp\\source.docx");
    var document = new Document("c:\\temp\\target.docx");
    var last = document.LastSection.Body.LastChild;
    Hashtable newLists = new Hashtable();
    foreach (Section s in subDoc.Sections)
    {
        var children = s.Body.GetChildNodes(NodeType.Any, false);
        for (var iChildIndex = 0; iChildIndex < children.Count; iChildIndex++)
        {
            var node = children[iChildIndex];
            try
            {
                Node newNode = document.ImportNode(node, true, ImportFormatMode.KeepSourceFormatting);

                last.ParentNode.InsertAfter(newNode, last);
                last = newNode;

                if (node is Paragraph && (node as Paragraph).IsListItem)
                {
                    SectionBinder.ResetNumbering(document, newNode, node, newLists);
                }
                else if (newNode is Aspose.Words.Tables.Table)
                {
                    Table tmTb = newNode as Table;
                    Table tmSrcTb = node as Table;
                    for (int i = 0; i < tmTb.Rows.Count; i++)
                    {
                        for (int j = 0; j < tmTb.Rows[i].Cells.Count; j++)
                        {
                            for (int k = 0; k < tmTb.Rows[i].Cells[j].Paragraphs.Count; k++)
                            {

                            }
                        }
                    }
                }

            }
            catch (Exception ex)
            {

            }
        }
    }
    document.Save("c:\\temp\\processed.docx");
}

public static void ResetNumbering(Document document, Node newNode, Node node, Hashtable newLists)
{
    int id = (node as Paragraph).ListFormat.List.ListId;

    Aspose.Words.Lists.List currentList = null;
    if (newLists.Contains(id))
    {
        currentList = (Aspose.Words.Lists.List)newLists[id];
    }
    else
    {
        // Add a copy of this list to the document and store it for later reference.
        if ((node as Paragraph).ListFormat.List.Style != null)
        {
            Aspose.Words.Style stl = document.Styles.AddCopy((node as Paragraph).ListFormat.List.Style);
            currentList = stl.List;
        }
        else
        {
            currentList = document.Lists.AddCopy((node as Paragraph).ListFormat.List);
        }
        newLists.Add(id, currentList);
    }

    (newNode as Paragraph).ListFormat.List = currentList;
}

@wengyeung You can easily achieve what you need using ImportFormatOptions.KeepSourceNumbering. Please try using the following code:

Document target = new Document(@"C:\Temp\target.docx");
Document source = new Document(@"C:\Temp\source.docx");

ImportFormatOptions opt = new ImportFormatOptions();
opt.KeepSourceNumbering = true;

DocumentBuilder builder = new DocumentBuilder(target);
builder.MoveToDocumentEnd();
builder.InsertDocument(source, ImportFormatMode.KeepSourceFormatting, opt);

target.Save(@"C:\Temp\out.docx");

out.docx (13.6 KB)

Thank you

The reason why we use element by element copying is that during this copying process, we need to handle blank documents, extra blank lines, or insert Bookmarks before and after the document for other business processing. Therefore, we cannot simply use InsertDocument. Can we find the starting sequence of the list using the above method

@wengyeung You can use ImportFormatOptions.KeepSourceNumbering for importing individual elements. You should simply use NodeImporter.ImportNode instead of Document.ImportNode:

Document target = new Document(@"C:\Temp\target.docx");
Document source = new Document(@"C:\Temp\source.docx");

ImportFormatOptions opt = new ImportFormatOptions();
opt.KeepSourceNumbering = true;
        
NodeImporter importer = new NodeImporter(source, target, ImportFormatMode.KeepSourceFormatting, opt);

// .................