Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Wed May 08, 2019 1:37 am

Hello,

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

xiutao.liu
 
Posts: 3
Joined: Fri Apr 12, 2019 2:59 am

Wed May 08, 2019 9:41 am

Hi,

Thanks for your inquiry.
I did reproduce the issue ("An item with the same key has already been added") and the style issue when testing your two cases, they have been posted to our Dev team for further analysis, if there is any update, I will inform you. Sorry for the inconvenience caused.

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1182
Joined: Tue Sep 27, 2016 1:06 am

Fri May 24, 2019 2:30 am

Hello,

Thanks for your patient waiting.
Glad to inform you that the style issue that caused by the second method has been resolved. Please download the hotfix (Spire.Doc Pack(hot fix) Version:7.5.9) and add the code document.KeepSameFormat = true; Below is my testing code for your reference,
Code: Select all
string[] files = new string[] { "1.docx", "2.docx", "3.docx" };
Document[] document = new Document[files.Length];
//load the three files
for (int i = 0; i < files.Length; i++)
{
    document[i] = new Document(files[i]);
    document[i].KeepSameFormat = true;
}
//clone sections
for (int i = 1; i < document.Length; i++)
{
    foreach (Section section in document[i].Sections)
    {
        document[0].Sections.Add(section.Clone());
   }
}
document[0].SaveToFile("result.docx", Spire.Doc.FileFormat.Docx);

And as for the issue ("An item with the same key has already been added") caused by the first method, the exception has been resolved, however the merged file has some issues on content format. Our Dev team is still working on it, if there is any progress, we will inform you.

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1182
Joined: Tue Sep 27, 2016 1:06 am

Wed Jun 16, 2021 3:29 am

After I used the spire.doc component, a blank page appeared at the end of the word after the two words were merged? But using a single word conversion is normal. Please help how to deal with this?

stanleysu
 
Posts: 2
Joined: Tue Jul 31, 2018 2:06 pm

Wed Jun 16, 2021 6:48 am

Hello,

Thanks for your inquiry.
To help us investigate your issue more quickly and accurately, please provide your input files and your complete code as well as your desired output. You could send them to us (support@e-iceblue.com) via email. Thanks in advance.

Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1271
Joined: Mon Oct 19, 2020 3:04 am

Return to Spire.Doc