Long papers or research reports are often completed collaboratively by multiple people. To save time, each person can work on their assigned parts in separate documents and then merge these documents into one after finish editing. Apart from manually copying and pasting content from one Word document to another, this article will demonstrate the following two ways to merge Word documents programmatically using Spire.Doc for .NET .
Install Spire.Doc for .NET
To begin with, you need to add the DLL files included in the Spire.Doc for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.Doc
Merge Documents by Inserting the Entire File
The Document.InsertTextFromFile() method provided by Spire.Doc for .NET allows merging Word documents by inserting other documents entirely into a document. Using this method, the contents of the inserted document will start from a new page. The detailed steps are as follows:
- Create a Document instance.
- Load the original Word document using Document.LoadFromFile() method.
- Insert another Word document entirely to the original document using Document.InsertTextFromFile() method.
- Save the result document using Document.SaveToFile() method.
- C#
- VB.NET
using Spire.Doc; namespace MergeWord { class Program { static void Main(string[] args) { //Create a Document instance Document document = new Document(); //Load the original Word document document.LoadFromFile("Doc1.docx", FileFormat.Docx); //Insert another Word document entirely to the original document document.InsertTextFromFile("Doc2.docx", FileFormat.Docx); //Save the result document document.SaveToFile("MergedWord.docx", FileFormat.Docx); } } }
Merge Documents by Cloning Contents
If you want to merge documents without starting a new page, you can clone the contents of other documents to add to the end of the original document. The detailed steps are as follows:
- Load two Word documents.
- Loop through the second document to get all the sections using Document.Sections property, and then loop through all the sections to get their child objects using Section.Body.ChildObjects property.
- Get the last section of the first document using Document.LastSection property, and then add the child objects to the last section of the first document using LastSection.Body.ChildObjects.Add() method.
- Save the result document using Document.SaveToFile() method.
- C#
- VB.NET
using Spire.Doc; namespace MergeWord { class Program { static void Main(string[] args) { //Load two Word documents Document doc1 = new Document("Doc1.docx"); Document doc2 = new Document("Doc2.docx"); //Loop through the second document to get all the sections foreach (Section section in doc2.Sections) { //Loop through the sections of the second document to get their child objects foreach (DocumentObject obj in section.Body.ChildObjects) { // Get the last section of the first document Section lastSection = doc1.LastSection; //Add all child objects to the last section of the first document lastSection.Body.ChildObjects.Add(obj.Clone()); } } // Save the result document doc1.SaveToFile("MergeDocuments.docx", FileFormat.Docx); } } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.