We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Aspose.Words NodeImporter issue

Importing a node with NodeImporter using the ImportFormatMode.KeepSourceFormatting causes wrong text font names and font sizes.

Sample code:

        var destDocNames = new List<string> { "destination_fontSize.docx", "destination_fontName.docx" };

        using (FileStream fileStreamSource = File.OpenRead(Server.MapPath("/File") + "\\source.docx"))
        {
            var docSource = new Document(fileStreamSource);
            var section = docSource.Sections[0];

            foreach (var name in destDocNames)
            {
                using (FileStream fileStreamDest = File.OpenRead(Server.MapPath("/File") + "\\" + name))
                {
                    var docDest = new Document(fileStreamDest);
                    var nodeImporter = new NodeImporter(docSource, docDest, ImportFormatMode.KeepSourceFormatting, new ImportFormatOptions { KeepSourceNumbering = true });
                    var importedSection = (Section)nodeImporter.ImportNode(section, true);
                    var docDestLastSection = docDest.LastSection;
                    var importedSectionNodes = importedSection.Body.ChildNodes.ToArray();

                    foreach (var childNode in importedSectionNodes)
                    {
                        docDestLastSection.Body.ChildNodes.Add(childNode);
                    }

                    docDest.Save(Server.MapPath("/File") + "\\output_" + name);
                };
            }
        };

For the “output_destination_fontSize.docx” document:
Expected: the “Text3” has a font size of 11, just like the “destination_fontSize.docx”.
Actual: the “Text3” has a font size of 10

For the “output_destination_fontName.docx” document:
Expected: the “Text2”, “Text3” and “Text4” all have Calibri font, just like “destination_fontName.docx”
Actual: the “Text2”, “Text3” and “Text4” all have Times New Roman font.

Changing the ImportFormatMode to UseDestinationStyles or KeepDifferentStyles does not reproduce these issues, but we need to use the KeepSourceFormatting option.

I have attached the WebApp reproducing these issue.
WebApp.zip (38.2 KB)

@edis.omer,
Please note that Aspose.Words mimics the behavior of MS Word. Please copy and paste (Ctrl+A Ctrl+C Ctrl+V, use KeepSourceFormatting) the whole “source.docx” to your destination documents. The formatting of the imported text will look the same as in the documents, produced by Aspose.Words.
We suggest you please use ImportFormatMode as UseDestinationStyles to save the text formatting

@sergey.lobanov
I have found an instance where copy pasting with KeepSourceFormatting in MS Word gives different results than Aspose Words. It is reproduced the same as before, so the code stays the same besides the source and destination document:

        var destDocNames = new List<string> { "main.docx" };

        using (FileStream fileStreamSource = File.OpenRead(Server.MapPath("/File") + "\\piece.docx"))
        {
            var docSource = new Document(fileStreamSource);
            var section = docSource.Sections[0];

            foreach (var name in destDocNames)
            {
                using (FileStream fileStreamDest = File.OpenRead(Server.MapPath("/File") + "\\" + name))
                {
                    var docDest = new Document(fileStreamDest);
                    var nodeImporter = new NodeImporter(docSource, docDest, ImportFormatMode.KeepSourceFormatting, new ImportFormatOptions { KeepSourceNumbering = true });
                    var importedSection = (Section)nodeImporter.ImportNode(section, true);
                    var docDestLastSection = docDest.LastSection;
                    var importedSectionNodes = importedSection.Body.ChildNodes.ToArray();

                    foreach (var childNode in importedSectionNodes)
                    {
                        docDestLastSection.Body.ChildNodes.Add(childNode);
                    }

                    docDest.Save(Server.MapPath("/File") + "\\output_" + name);
                };
            }
        }; 

The “output_main.docx” file generated with Aspose will have Times New Roman set for the imported section runs (see “Excel1”, “Excel2”, etc. runs)

Copy pasting “piece.docx” into “main.docx” with KeepSourceFormatting in MS Word will have Calibri set for the imported section runs (see “Excel1”, “Excel2”, etc. runs)

I have attached the web app reproducing this issue

WebApp2.zip (24.1 KB)

@edis.omer,
Thank you for reporting this problem to us. We have tested the scenario and managed to reproduce the same issue at our side. For the sake of correction, we have logged this problem in our issue tracking system as WORDSNET - 23366. You will be notified via this forum thread once this issue is resolved.

We apologize for your inconvenience.

The issues you have found earlier (filed as WORDSNET-23366) have been fixed in this Aspose.Words for .NET 22.3 update also available on NuGet.