I tried to merge the attached 3 docx files into one with method InsertTextFromFile but meeting the following error:
An item with the same key has already been added. Key: ContentTypeId
I have attached the 3 docx files: file.rar
Please check my code below:
- Code: Select all
Document document = new Document();
document.LoadFromFile("1.docx", FileFormat.Docx);
document.InsertTextFromFile("2.docx", FileFormat.Docx);
document.InsertTextFromFile("3.docx", FileFormat.Docx);
document.SaveToFile("merged.docx", FileFormat.Docx);
But then I tried with section clone method with the following code:
- Code: Select all
Document document = new Document();
document.LoadFromFile("1.docx", FileFormat.Docx);
Document doc2 = new Document();
doc2.LoadFromFile("2.docx", FileFormat.Docx);
foreach (Section sec in doc2.Sections)
{
document.Sections.Add(sec.Clone());
}
Document doc3 = new Document();
doc3.LoadFromFile("3.docx", FileFormat.Docx);
foreach (Section sec in doc3.Sections)
{
document.Sections.Add(sec.Clone());
}
document.SaveToFile("merged.docx", FileFormat.Docx);
The above code works well but the merged doc has some style issues. Please check the attached merged file.rar
The Spire.Doc version I used on Visual Studio is 7.5.1 which is the latest stable version.
Please check the issues and see if I missed something.
Thanks,
Xiutao