Documents for Word, .NET Edition Documentation
Features / Document / Split or Merge Documents
In This Topic
    Split or Merge Documents
    In This Topic

    GcWord allows you to easily split or merge documents by providing the SplitDocument and MergeDocuments methods in GcWordDocument class. While performing the split or merge operations, the formatting settings of the document can also be copied or cleared using the Copy or Clear value of FormattingCopyStrategy enumeration.

    The MergeDocuments method appends the document content one after the another to create a new merged document and the SplitDocument method copies the content from specific ranges to create new documents.

    Split Documents

    To split a document:

    1. Load a document using the Load method of GcWordDocument class.
    2. Define an IEnumerable collection of document ranges using the Range class. The count of these ranges will determine the number of split documents to be created.
    3. Split the document using the SplitDocument method of GcWordDocument class. To copy the formatting settings, use the Copy value of FormattingCopyStrategy enumeration.
    4. Save the split documents as new documents using the Save method of GcWordDocument class.
      C#
      Copy Code
      //Load the document
      GcWordDocument doc = new GcWordDocument();
      doc.Load("SampleDoc.docx");
      
      //Define an IEnumerable collection of document ranges which are intended to be copied to the new document
      Range[] splitRanges = new Range[] { doc.Body.Sections[0].GetRange(), doc.Body.Sections[1].GetRange() };
      
      //Split the document into multiple documents
      IEnumerable<GcWordDocument> docs = doc.SplitDocument(FormattingCopyStrategy.Copy, splitRanges);
                 
      //Save the individual documents
      List<GcWordDocument> splitDocs = docs.ToList<GcWordDocument>();
      splitDocs[0].Save("Doc1.docx");
      splitDocs[1].Save("Doc2.docx");
      

    Merge Documents

    To merge two documents:

    1. Load the documents which you want to merge using the Load method of GcWordDocument class.
    2. Define an IEnumerable collection of the documents you want to merge together.
    3. Merge the documents using the MergeDocuments method of GcWordDocument class. To copy the formatting settings, use the Copy value of FormattingCopyStrategy enumeration.
    4. Save the merged document as a new document using the Save method of GcWordDocument class.
      C#
      Copy Code
      //Load the documents
      GcWordDocument doc1 = new GcWordDocument();
      doc1.Load("Document1.docx");
      
      GcWordDocument doc2 = new GcWordDocument();
      doc2.Load("Document2.docx");
      
      //Define an IEnumerable collection of documents to be merged
      GcWordDocument[] docs = new GcWordDocument[] { doc1, doc2 };
      
      //Merge the documents
      GcWordDocument mergedDoc = GcWordDocument.MergeDocuments(FormattingCopyStrategy.Copy, docs);
      
      //Save the merged document
      mergedDoc.Save("MergedDoc.docx");