Preserve List Styles in Word Documents when using NodeImporter with KeepSourceFormatting Setting | Java

Hi, I copied node by NodeImporter with KEEP_SOURCE_FORMATTING and expect that style moved to result document but not why? You can see http://joxi.ru/zANppYPfvWBNdm
what I need to do that have expect result ?
test code and document in attach.
importTest.zip (38.8 KB)

@vhostt,

We are checking this scenario and will get back to you soon.

Ok. I want to explain why I do it. I store html paragraphs and their style properties in the database, so in order to form a document I make the same sequence as in the code, but I have more properties. I gave an example with only 2 list properties (setting color and bold).

@vhostt,

We have managed to generate an output document (see awjava-20.6.zip (15.8 KB)) from your source (test.docx) document by using the code you provided and using the latest 20.6 version of Aspose.Words for Java on our end. Do you see the same problem in this awjava-20.6.docx? If yes, then can you please also provide your expected DOCX file showing the desired output here for our reference. You can create this document by using MS Word. Thanks for your cooperation.

The same as shown in the demo cod. In the output file, the list items are different from what was in the original version.
yours version http://joxi.ru/LmGqqePtwXg0bA
original http://joxi.ru/E2pWW1OC7Q4NYm
if you saw demo code, you see that I set color, listid and bold to document after DocumentBuilder.
After NodeImporter they changes

@vhostt,

We have logged this problem in our issue tracking system. Your ticket number is WORDSNET-20670. We will further look into the details of this problem and will keep you updated on the status of the linked issue.

@vhostt,

Regarding WORDSNET-20670, you clone document and then apply Font formatting to the List of the problematic paragraph. As nsids of the lists in the original and cloned documents are equal, Aspose.Words mimics MS Word’s behavior and doesn’t import list from the cloned document. This is expected behavior.

Also, please note that the Bold and Color are defined in direct attributes of problematic paragraphs. To fix the issue with list label’s properties, you may use Paragraph.ParagraphBreakFont.Color property instead of Paragraph.ListLabel.Font.Color and Paragraph.ParagraphBreakFont.Bold property instead of Paragraph.ListLabel.Font.Bold.

However, if you still want to set it in direct attributes of list, you should use ImportFormatOptions.KeepSourceNumbering that makes Aspose.Words to not mimic MS Word and import lists even with equal nsids:

ImportFormatOptions options = new ImportFormatOptions();
options.setKeepSourceNumbering(true);
NodeImporter importer = new NodeImporter(newDocument, doc, ImportFormatMode.KEEP_SOURCE_FORMATTING, options);

But note, it will apply new List to every imported paragraph (i.e., will create a new list for every paragraph). So, in this case, you must additionally be concerned that the correct list is applied to the imported paragraph:

Document copy = doc.deepClone();
doc.getFirstSection().getBody().removeAllChildren();

// ==========
// The table to translate lists from original document into destination one.
Hashtable lists = new Hashtable();
// ==========

for (int i = 0; i < paragraphs.size(); i++)
{
    DocumentBuilder builder = new DocumentBuilder(copy.deepClone());
    builder.insertHtml(((miniParDto)paragraphs[i]).getHtml());
    Document newDocument = builder.getDocument();
    if (((miniParDto)paragraphs[i]).getListId() != null)
    {
        List curentList = newDocument.getLists().getListByListId((((miniParDto)paragraphs[i]).getListId().HasValue)
                ? ((miniParDto)paragraphs[i]).getListId().Value
                : 0;
        Paragraph p = newDocument.getFirstSection().getBody().getFirstParagraph();
        p.getListFormat().setList((curentList));
        p.getListLabel().getFont().setColor((((miniParDto)paragraphs[i]).getRgb()));
        p.getListLabel().getFont().setBold((((miniParDto)paragraphs[i]).isBold()));
    }

    // ==========
    // Enable KeepSourceNumbering option to allow AW recreate lists with equal nsids.
    ImportFormatOptions options = new ImportFormatOptions();
    options.setKeepSourceNumbering(true);
    NodeImporter importer = new NodeImporter(newDocument, doc, ImportFormatMode.KEEP_SOURCE_FORMATTING, options);
    // ==========

    Body body = newDocument.getFirstSection().getBody();
    Body bodyDestination = doc.getFirstSection().getBody();
    for (int j = 0; j < body.getChildNodes().getCount(); j++)
    {
        Node temp = body.getChildNodes().get(j);
        // Let's skip the node if it is a last empty paragraph in a section.
        if (temp.getNodeType() == NodeType.PARAGRAPH)
        {
            Paragraph para = (Paragraph)temp;
            if (para.isEndOfSection() && !para.hasChildNodes())
                continue;
        }

        Node newNode = importer.importNode(temp, true);
        bodyDestination.appendChild(newNode);

        // ==========
        // This is additional code to translate lists from the original document to destination one.
        Paragraph importedPara =  newNode instanceof Paragraph ? (Paragraph)newNode : null;
        if ((importedPara != null) && importedPara.isListItem())
        {
            List originalList = ((Paragraph)temp).getListFormat().getList();
            List destinationList = (List)lists.get(originalList);
            // If such list is not imported yet, then just remember a new list translation.
            if (destinationList == null)
            {
                destinationList = importedPara.getListFormat().getList();
                lists.put(originalList,destinationList);
            }
            else
            {
                // Otherwise, reapply list.
                importedPara.getListFormat().setList(destinationList);
            }
        }
        // ==========
    }
}

doc.save("E:\\Temp\\importTest\\21.6.docx");