Merge document word in order that i want with spire.doc

523 Views Asked by At

i'm trying to merge multiple word document into one document using spire.doc, but I would like the order to be my choice.

Document doc = new Document();

//document1
doc.LoadFromFile(sourcefile1, FileFormat.Docx);
doc.InsertTextFromFile(@"C:\document.docx", FileFormat.Docx);
doc.SaveToFile(@"C:\document.docx", FileFormat.Docx);

//document2
doc.LoadFromFile(sourcefile2, FileFormat.Docx);
doc.InsertTextFromFile(@"C:\document.docx", FileFormat.Docx);
doc.SaveToFile(@"C:\document.docx", FileFormat.Docx);

//document 3
doc.LoadFromFile(sourcefile3, FileFormat.Docx);
doc.InsertTextFromFile(@"C:\document.docx", FileFormat.Docx);
doc.SaveToFile(@"C:\document.docx", FileFormat.Docx);

For example I would like document1 to be on the front page but currently it is put on the last page.

Thank you all

1

There are 1 best solutions below

0
Dheeraj Malik On

You can use the following code to achieve your requirement:

Document doc = new Document();
doc.LoadFromFile("file1.docx");

string[] files = new string[] {"file2.docx", "file3.docx" };

//Insert contents from multiple files into file1.docx
foreach (string file in files)
{
    doc.InsertTextFromFile(file, FileFormat.Docx);
}

//Save the result file
doc.SaveToFile("merge.docx", FileFormat.Docx);