How to Merge and Split Word Documents in C#

This article is aimed to introduce a simple solution to merge a part of/whole word documents into a single document along with split a word document into separate documents by section and page break in C#.

Background

I need to merge many small word documents and then send the results to my clients, after that I have to split them into the same separate documents as before. For doing this, I searched on Google and fortunately I found some free and useful solutions. First, I found Microsoft.Office.Interop.Word, I tried it but it needs me to write a lot of codes and I was stuck at somewhere. Considering the timeline, I have to turn to other solutions, finally I found a free word API on CodePlex that solved all my problems and easy to use. I am happy to share my solution with you here. Hope this solution will also help you guys.

Using the code

Part 1 - Merge

When merging two word documents, we can choose to merge part of one document into another document along with merge the whole documents into a single document.

For the data confidentiality, I created two sample documents like below.Document 1Document 2

Merge specific section of one word document into another document

Step 1: Initialize a new object of Document class and load document 1.

// Get the first section of document 2 Section sec = doc2.Sections[0];// Clone the section and add it to document 1doc1.Sections.Add(sec.Clone());//Savedoc1.SaveToFile("Mergesection.docx", FileFormat.Docx);