NodeImporter. ListFormat not imported

ListFormat is not copied when import one document to another using NodeImporter.

docx-documents.zip (36.3 KB)

Java Code:

public static void main(String[] args) throws Exception {
    Document doc = new Document("mainDocument.docx");

    Document importDoc = new Document("subDocument.docx");

    Node insertAfterNode = doc.getRange().getBookmarks().get("Т6367399140989276889").getBookmarkStart().getParentNode();

    CompositeNode dstStory = insertAfterNode.getParentNode();
    DocumentBase destinationDoc = insertAfterNode.getDocument();
    NodeImporter importer = new NodeImporter(importDoc, destinationDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING);
    for (Section srcSection : importDoc.getSections())
    {
        for (Node srcNode : (Iterable<Node>) srcSection.getBody())
        {
            if (srcNode.getNodeType() == (NodeType.PARAGRAPH))
            {
                Paragraph para = (Paragraph)srcNode;
                if (para.isEndOfSection() && !para.hasChildNodes())
                    continue;
            }

            Node newNode = importer.importNode(srcNode, true);
            dstStory.insertAfter(newNode, insertAfterNode);

// fixStartAtListFormat(srcNode, newNode);
insertAfterNode = newNode;
}
}

    doc.save("resultDoc.docx");
}

private static void fixStartAtListFormat(Node sourceNode, Node destNode) throws Exception {
    if (!(sourceNode instanceof Paragraph) || !(destNode instanceof Paragraph)) return;
    if (!((Paragraph) sourceNode).getListFormat().isListItem()) return;
    if (!((Paragraph) destNode).getListFormat().isListItem()) return;

    List destList = ((Paragraph) destNode).getListFormat().getList();
    List sourceList = ((Paragraph) sourceNode).getListFormat().getList();

    for (int i=0; i<destList.getListLevels().getCount(); ++i) {
        if (sourceList.getListLevels().getCount() > i) {
            destList.getListLevels().get(i).setStartAt(sourceList.getListLevels().get(i).getStartAt());;
        }
    }
}

Uncomment “fixStartAtListFormat” to fix the issue.

Works correctly on aspose-words 13.2.0.0
Reproduced on aspose-words 16.8.0

@ahrytsenko,

We tested the scenario and have managed to reproduce the same problem on our end. For the sake of correction, we have logged this problem in our issue tracking system. The ID of this issue is WORDSNET-17534. We will further look into the details of this problem and will keep you updated on the status of correction. We apologize for your inconvenience.

Any updates?

@arumyantseva,

Unfortunately, your issue is not resolved yet. We have completed the analysis of this issue. We are currently working on investigating the best possible way to fix this issue. We will inform you via this thread as soon as this issue is resolved. We apologize for your inconvenience.

@awais.hafeez,

Please note that this is critical issue for us. This is impacting several of our customers. We need a fix asap.
Thanks

@faika,

Regarding WORDSNET-17534, we have completed the analysis of your issue and come to a conclusion that this issue is actually not a bug in Aspose.Words.

In this case, the latest version of Aspose.Words mimics the behavior of MS Word. To make sure, that MS Word behaves in the same way as Aspose.Words now, please make several steps:

  1. Open mainDocument.docx with MS Word.
  2. Specify import mode : File -> Options -> Advanced -> Cut, copy, and paste -> Keep Source Formatting
  3. Open subDocument.docx with MS Word, and Copy the list
  4. In mainDocument.docx paste the list.

You will see that numbering starts from 1.

Yes, it looks strange, but it is MS Word’s behavior, and we should follow it.

The issues you have found earlier (filed as WORDSNET-17534) have been fixed in this Aspose.Words for .NET 19.4 update and this Aspose.Words for Java 19.4 update.

@ahrytsenko, @faika,

Regarding WORDSNET-17534, it is to update you that we have added a new ImportFormatOptions.KeepSourceNumbering option for use in import operations.

When import nodes between different documents there can be a situation when source document has lists with the same identifiers that were already used in a destination document. MS Word in such case always uses formatting from the destination lists. To allow users to choose an appropriate behavior, the following option was introduced in ImportFormatOptions class:

/// <summary>
/// Gets or sets a boolean value that specifies how the numbering will be imported when it clashes in source and destination documents.
/// The default value is <c>false</c>.
/// </summary>
public bool KeepSourceNumbering

Also a new public method that accepts the new KeepSourceNumbering option was introduced as the following:

/// <summary>
/// Initializes a new instance of the <see cref="NodeImporter"/> class.
/// </summary>
/// <param name="srcDoc">The source document.</param>
/// <param name="dstDoc">The destination document that will be the owner of imported nodes.</param>
/// <param name="importFormatMode">Specifies how to merge style formatting that clashes.</param>
/// <param name="importFormatOptions">Specifies various options to format imported node.</param>
public NodeImporter(DocumentBase srcDoc, DocumentBase dstDoc, ImportFormatMode importFormatMode, ImportFormatOptions importFormatOptions)

Simple use-case is as follows:

Document srcDoc = TestUtil.Open(@"source.docx");
Document dstDoc = TestUtil.Open(@"destination.docx");
 
ImportFormatOptions importFormatOptions = new ImportFormatOptions();
// Keep source list formatting when importing numbered paragraphs.
importFormatOptions.KeepSourceNumbering = true;
NodeImporter importer = new NodeImporter(srcDoc, dstDoc, ImportFormatMode.KeepSourceFormatting, importFormatOptions);
 
ParagraphCollection srcParas = srcDoc.FirstSection.Body.Paragraphs;
foreach (Paragraph srcPara in srcParas)
{
    Node importedNode = importer.ImportNode(srcPara, false);
    dstDoc.FirstSection.Body.AppendChild(importedNode);
}
 
dstDoc.Save("output.docx");

Hope, this helps.