For the samples below involving lists we will be using these documents:
Destination document
Source Document
Each document contains a list, both of which are defined to use a linked style called MyStyle.
When appending documents which contain lists with linked styles, the chosen ImportFormatMode can make a difference in how the lists behave when the documents are combined.
Example
Shows how to append a document to another document containing lists retaining source formatting.
[Java]
Document dstDoc = new Document(gDataDir + "TestFile.DestinationList.doc");
Document srcDoc = new Document(gDataDir + "TestFile.SourceList.doc");
// Append the content of the document so it flows continuously.
srcDoc.getFirstSection().getPageSetup().setSectionStart(SectionStart.CONTINUOUS);
dstDoc.appendDocument(srcDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING);
dstDoc.save(gDataDir + "TestFile.ListKeepSourceFormatting Out.doc");
Appending the documents using ImportFormatMode.KeepSourceFormatting like in the code above will appear as expected as in the output below. The lists retain the correct numbering.
However when the same two documents are joined using ImportFormatMode.UseDestinationStyles then the lists in the combined document continue on instead of being restarted as separate lists. This behavior can be seen in the combined document below. The numbering of the two lists continues instead of restarting where the source document was appended.
If the list in the document is not using a linked style or the linked style does not occur in the destination document this issue will not occur.
The code below provides a general implementation of how to avoid this issue. Any list in the source document which has a List.ListId already found in the destination document is copied and the list in the document is changed to use the new copy instead.
Example
Shows how to append a document using destination styles and preventing any list numberings from continuing on.
[Java]
Document dstDoc = new Document(gDataDir + "TestFile.DestinationList.doc");
Document srcDoc = new Document(gDataDir + "TestFile.SourceList.doc");
// Set the source document to continue straight after the end of the destination document.
srcDoc.getFirstSection().getPageSetup().setSectionStart(SectionStart.CONTINUOUS);
// Keep track of the lists that are created.
HashMap newLists = new HashMap();
// Iterate through all paragraphs in the document.
for (Paragraph para : (Iterable<Paragraph>) srcDoc.getChildNodes(NodeType.PARAGRAPH, true))
{
if (para.isListItem())
{
int listId = para.getListFormat().getList().getListId();
// 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 (dstDoc.getLists().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.containsKey(listId))
{
currentList = (List)newLists.get(listId);
}
else
{
// Add a copy of this list to the document and store it for later reference.
currentList = srcDoc.getLists().addCopy(para.getListFormat().getList());
newLists.put(listId, currentList);
}
// Set the list of this paragraph to the copied list.
para.getListFormat().setList(currentList);
}
}
}
// Append the source document to end of the destination document.
dstDoc.appendDocument(srcDoc, ImportFormatMode.USE_DESTINATION_STYLES);
// Save the combined document to disk.
dstDoc.save(gDataDir + "TestFile.ListUseDestinationStyles Out.docx");
After executing this code the numbering of the lists appears as expected in the output document.