Hello,
We are trying to Append documents together but after the second document, the numbered lists do not reset. Not sure if we are doing something wrong that it doesn’t reset after page 2. Below is the code we are using and will attach the documents as well. We are using Aspose.Words v16.11.0.0.
Thanks for your help.
Expected numbered lists in Output.doc are:
Page 1: 1. 2. 3. 4.
Page 2: 1. 2.
Page 3: 1. 2.
Actual numbered lists:
Page 1: 1. 2. 3. 4.
Page 2: 1. 2.
Page 3: 3. 4.
Here’s the code we are using:
ArrayList lst = new ArrayList();
lst.Add(@"C:\Temp\1.doc");
lst.Add(@"C:\Temp\2.doc");
lst.Add(@"C:\Temp\3.doc");
Append(lst, @"C:\Temp\OutputTest.doc");
public void Append(ArrayList sourceDocs, String destinationFullPath, bool concatenate = false)
{
// Initialize document
Document destDoc = new Document();
destDoc.RemoveAllChildren(); // To avoid empty page
// Append docs
for (int i = 0; i < sourceDocs.Count; i++)
{
// Add new page
if (i > 0)
destDoc.LastSection.PageSetup.SectionStart = (concatenate ? SectionStart.Continuous : SectionStart.NewPage);
// Load document
Document tmpDoc = new Document(sourceDocs[i].ToString());
// Convert docProperty fields to static text
FieldsHelper.ConvertFieldsToStaticText(tmpDoc, Aspose.Words.Fields.FieldType.FieldDocProperty);
// Set the appended document to appear on the next page.
tmpDoc.FirstSection.PageSetup.SectionStart = (concatenate ? SectionStart.Continuous : SectionStart.NewPage);
tmpDoc.LastSection.PageSetup.SectionStart = (concatenate ? SectionStart.Continuous : SectionStart.NewPage);
// Restart the page numbering for the document to be appended.
tmpDoc.FirstSection.PageSetup.RestartPageNumbering = true;
if (destDoc.LastSection != null)
destDoc.LastSection.PageSetup.RestartPageNumbering = true;
// Don’t link to previous header/footer
tmpDoc.LastSection.HeadersFooters.LinkToPrevious(false);
// Keep track of the lists that are created.
Hashtable newLists = new Hashtable();
// Iterate through all paragraphs in the document.
foreach (Paragraph para in tmpDoc.GetChildNodes(NodeType.Paragraph, true))
{
if (para.IsListItem)
{
int listId = para.ListFormat.List.ListId;
// Check if the destination document contains a list with this ID already. If it does then this may
// cause the two lists to run together. Create a copy of the list in the source document instead.
if (destDoc.Lists.GetListByListId(listId) != null)
{
List currentList;
// A newly copied list already exists for this ID, retrieve the stored list and use it on
// the current paragraph.
if (newLists.Contains(listId))
{
currentList = (List)newLists[listId];
}
else
{
// Add a copy of this list to the document and store it for later reference.
currentList = tmpDoc.Lists.AddCopy(para.ListFormat.List);
newLists.Add(listId, currentList);
}
// Set the list of this paragraph to the copied list.
para.ListFormat.List = currentList;
}
}
}
// Append
destDoc.AppendDocument(tmpDoc, ImportFormatMode.KeepSourceFormatting);
}
// Save document
destDoc.Save(destinationFullPath, Aspose.Words.SaveFormat.Doc);
}