When copying a section the style is not preserved


#1

Hello,
When running the following code, the style of the second document is not preserved. The bullets are transformed into numbers.
protected void Page_Load(object sender, EventArgs e)
{
var doc1 = new Document(@“C:\1.docx”);
var doc2 = new Document(@“c:\2.docx”);

       var sectionCount = doc2.Sections.Count;

       var nodeImporter = new NodeImporter(doc2, doc1, ImportFormatMode.KeepSourceFormatting);
        for (int i = 0; i < sectionCount; i++)
        {
            var section = doc2.Sections[i];
            var importedSection = (Section) nodeImporter.ImportNode(section, true);
            importedSection.PageSetup.MultiplePages = section.PageSetup.MultiplePages;

            CopySection(importedSection, doc1);
        }

        doc1.Save(@"c:\issue.docx");

}
public void CopySection(Section importedSection, Document mainDocument)
{
var mainDocumentLastSection = mainDocument.LastSection;
var importedSectionNodes = importedSection.Body.ChildNodes.ToArray();
foreach (var childNode in importedSectionNodes)
mainDocumentLastSection.Body.ChildNodes.Add(childNode);
}
I’ve also attached the documents.
Thank youdocuments.zip (60.4 KB)


#2

@adrian.anton

You are facing the expected behavior of Aspose.Words. Please note that Aspose.Words mimics the behavior of Aspose.Words. If you copy the content of source document into destination document using MS Word, you will get the same output.


#3

The thing is that even when I’m creating a new document and adding the provided documents by copying them into the newly created one, this is still happening. And also, the styles are totally different and have different names.
I was able to reproduce this by using the following code:
protected void Page_Load(object sender, EventArgs e)
{
var doc1 = new Document(@“C:\1.docx”);
var doc2 = new Document(@“c:\2.docx”);
var mainDoc = new Document();
mainDoc.Cleanup();

var sectionCount = doc1.Sections.Count;
       
var nodeImporter = new NodeImporter(doc1, mainDoc, ImportFormatMode.KeepSourceFormatting);
for (int i = 0; i < sectionCount; i++)
{
	var section = doc1.Sections[i];
    var importedSection = (Section) nodeImporter.ImportNode(section, true);
    importedSection.PageSetup.MultiplePages = section.PageSetup.MultiplePages;

	CopySection(importedSection, mainDoc);
}

sectionCount = doc2.Sections.Count;
nodeImporter = new NodeImporter(doc2, mainDoc, ImportFormatMode.KeepSourceFormatting);
for (int i = 0; i < sectionCount; i++)
{
	var section = doc2.Sections[i];
    var importedSection = (Section) nodeImporter.ImportNode(section, true);
    importedSection.PageSetup.MultiplePages = section.PageSetup.MultiplePages;

	CopySection(importedSection, mainDoc);
}

mainDoc.Save(@"c:\issue.docx");

}

public void CopySection(Section importedSection, Document mainDocument)
{
var mainDocumentLastSection = mainDocument.LastSection;
var importedSectionNodes = importedSection.Body.ChildNodes.ToArray();
foreach (var childNode in importedSectionNodes)
mainDocumentLastSection.Body.ChildNodes.Add(childNode);
}
I’ve attached the newly resulted document.
issue.zip (14.0 KB)


#4

@adrian.anton

We have tested the scenario and have 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-18873. You will be notified via this forum thread once this issue is resolved.

We apologize for your inconvenience.


#5

@adrian.anton

It is to inform you that the issue which you are facing is actually not a bug in Aspose.Words. So, we have closed this issue (WORDSNET-18873) as ‘Not a Bug’.

The definition with numbers is copied to the destination after the first document is imported. Second document has bulleted list with the same definition identifier, so definition is reused in the destination. As the result, we see number instead of bullets.

Please use the following code example to get the desired output. Hope this helps you.

Document doc1 = new Document(MyDir + @"1.docx");
Document doc2 = new Document(MyDir + @"2.docx");
Document mainDoc = new Document();
mainDoc.Cleanup();

int sectionCount = doc1.Sections.Count;
ImportFormatOptions options = new ImportFormatOptions();
options.KeepSourceNumbering = true;

NodeImporter nodeImporter = new NodeImporter(doc1, mainDoc, ImportFormatMode.KeepSourceFormatting, options);
for (int i = 0; i < sectionCount; i++)
{
    Section section = doc1.Sections[i];
    Section importedSection = (Section)nodeImporter.ImportNode(section, true);
    importedSection.PageSetup.MultiplePages = section.PageSetup.MultiplePages;

    CopySection(importedSection, mainDoc);
}

sectionCount = doc2.Sections.Count;
nodeImporter = new NodeImporter(doc2, mainDoc, ImportFormatMode.KeepSourceFormatting, options);
for (int i = 0; i < sectionCount; i++)
{
    Section section = doc2.Sections[i];
    Section importedSection = (Section)nodeImporter.ImportNode(section, true);
    importedSection.PageSetup.MultiplePages = section.PageSetup.MultiplePages;

    CopySection(importedSection, mainDoc);
}

mainDoc.Save(@MyDir + "19.7.docx"); 

public static  void CopySection(Section importedSection, Document mainDocument)
{
    var mainDocumentLastSection = mainDocument.LastSection;
    var importedSectionNodes = importedSection.Body.ChildNodes.ToArray();
    foreach (var childNode in importedSectionNodes)
        mainDocumentLastSection.Body.ChildNodes.Add(childNode);
}