To merge Word is to collect contents from several documents and then put them into one. Users can have an order to merge Word according to contents of document. This guide focuses on demonstrating the solution to merge Word in C# and VB.NET with Spire.Doc for .NET.
New Method to Merge Word Documents
In this example, two documents will be merged. In the merged Word, the contents of the second document follow the first one. The following screenshot presents effect after merging.
Download and install Spire.Doc for .NET and follow steps to merge Word. At first, initialize two Document instances DocOne and DocTwo from specified documents. Secondly, get each section of DocTwo and invoke Document.Sections.Add method to add a new section in DocOne with parameter section which is sections in DocTwo gotten by invoking Section.Clone() method. Use foreach statement to add all the sections from DocTwo to DocOne to merge. Finally, save the merged document. Code as following:
using Spire.Doc; namespace MergeDocument { class DocxMerge { static void Main(string[] args) { //Load Document1 and Document2 Document DocOne = new Document(); DocOne.LoadFromFile(@"E:\Work\Document\welcome.docx", FileFormat.Docx); Document DocTwo = new Document(); DocTwo.LoadFromFile(@"E:\Work\Document\New Zealand.docx", FileFormat.Docx); //Merge foreach (Section sec in DocTwo.Sections) { DocOne.Sections.Add(sec.Clone()); } //Save and Launch DocOne.SaveToFile("Merge.docx", FileFormat.Docx); System.Diagnostics.Process.Start("Merge.docx"); } } }
Imports Spire.Doc Namespace MergeDocument Friend Class DocxMerge Shared Sub Main(ByVal args() As String) 'Load Document1 and Document2 Dim DocOne As New Document() DocOne.LoadFromFile("E:\Work\Document\welcome.docx", FileFormat.Docx) Dim DocTwo As New Document() DocTwo.LoadFromFile("E:\Work\Document\New Zealand.docx", FileFormat.Docx) 'Merge For Each sec As Section In DocTwo.Sections DocOne.Sections.Add(sec.Clone()) Next sec 'Save and Launch DocOne.SaveToFile("Merge.docx", FileFormat.Docx) System.Diagnostics.Process.Start("Merge.docx") End Sub End Class End Namespace
Spire.Doc, the professional stand-alone component to manipulate MS Word document without automation, enables developers to generate, read, write, modify Word document on their .NET, WPF and Silverlight application.