DsWord 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.
To split a document:
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"); |
To merge two documents:
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"); |
For more information on how to split or merge documents content using DsWord, see DsWord sample browser.