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
But the actual situation is like this
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;
}

