Since 1983, Microsoft Word has been the most popular choice for helping teams manage their documents, including books, articles, newsletters, educational documents, marketing materials, and much more. When creating these items, a team might be working on one section, while someone else might be designing a different page or section. For instance, someone might be working on the cover page of a book, and another person might be drafting the chapters. In these scenarios, you will need to merge the documents from different team members to finalize the content or split the content to continue working on the same draft.

Grapecity Documents for Word, which allows you to create, modify, and save Word documents, can also help you perform the split and merge operations by merely invoking the SplitDocument and MergeDocuments methods, respectively.

To understand these methods, consider designing a safety manual for electrical appliances. Since the document will be available around the world, it will need to be distributed in several different languages. This document can be shared as a single document (that includes all the languages available), or it can be split into multiple documents for each language it's going to be printed.

Here you can learn how to use the GcWord API to merge or split the documents.

How to Merge and Split Documents in a Word API

Splitting Documents in a Word API

For this example, let's say that we have a single document that consists of all the instructions for each language, and we need to split the document to create multiple documents. Each document will include the instructions for a specific language.

The SplitDocument method of the GcWordDocument class lets you copy the document ranges to create a new separate document for each range. It accepts a list of document ranges that are used to split the document into multiple documents. It also accepts an enum value to determine whether to retain or omit the formatting of the document being divided.

In the current example, we will define a document range for each set of paragraphs that describe the safety instructions for one language and then use this collection of ranges to split the document. For example, the first two paragraphs will describe the instructions in English, the next two in Chinese, and so on.

Here is a sample code snippet which defines the document ranges as described above and splits a word document into multiple word documents:

public void SplitDocuments()
      { 
          //Load the document to be split 
          GcWordDocument doc = new GcWordDocument();
          doc.Load(Path.Combine("Resources", "MultiLang.docx"));

          //Define an IEnumerable collection of document ranges which are intended to be copied to the new               document 
         var paracount = doc.Body.Paragraphs.Count / 2; 
          Range[] splitRanges = new Range[paracount];          
          var j = 0; 
          for (var i = 0; i < doc.Body.Paragraphs.Count - 1; i = i + 2)
          { 
              splitRanges[j] = doc.Body.GetPersistentRange(doc.Body.Paragraphs[i].Start, doc.Body.Paragraphs[i + 1].End); 
              j++;  
         }      

          //Split the document into multiple documents  
          IEnumerable<GcWordDocument> docs = doc.SplitDocument(FormattingCopyStrategy.Copy, splitRanges);

          //Save the individual documents  
         List<GcWordDocument> splitDocs = docs.ToList<GcWordDocument>();
         var doccount = 1; 
          foreach (var paradoc in splitDocs) 
         { 
              paradoc.Save("Doc" + doccount + ".docx"); 
              doccount++;
          }           
     }

Merging Documents in a Word API

Let's combine the documents created above to form a merged document and create a manual that includes the instructions for all languages.

The MergeDocuments method of the GcWordDocument class creates a new Word document by merging other word documents. It accepts a list of documents and an enum value to determine whether to keep or omit the formatting of the document being combined.

Here we will create a list of documents using the documents for each language and use this list to perform the merge operation.

Here is a sample code snippet which merges multiple word documents:

public void MergeDocuments()
      { 
          //Load the documents to be merged  
          GcWordDocument doc1 = new GcWordDocument();  
          doc1.Load(Path.Combine("Resources", "Doc1.docx"));

          GcWordDocument doc2 = new GcWordDocument(); 
          doc2.Load(Path.Combine("Resources", "Doc2.docx"));

          GcWordDocument doc3 = new GcWordDocument();
          doc3.Load(Path.Combine("Resources", "Doc3.docx"));

          GcWordDocument doc4 = new GcWordDocument();
          doc4.Load(Path.Combine("Resources", "Doc4.docx"));

          //Define an IEnumerable collection of documents to be merged 
          GcWordDocument[] docs = new GcWordDocument[] { doc1, doc2, doc3, doc4 };

          //Merge the documents 
          GcWordDocument mergedDoc = GcWordDocument.MergeDocuments(FormattingCopyStrategy.Copy, docs);

          //Save the merged document 
         mergedDoc.Save("MergedManual.docx");
      }

Refer to the working sample to see Merge and Split in action. Let us know what other scenarios could benefit from the Split and Merge feature.

Try the new GrapeCity Documents for Word

Download GrapeCity Documents for Word

Download Now!