How to merge Word documents in C#?

 Both the sender and the recipient often find it challenging to share multiple Word documents — but you can simplify the task by merging them into one file before sharing. This can be particularly useful if the documents are related. Apart from manually copying and pasting content from one Word document to another, this article will demonstrate the following two ways to merge Word documents programmatically. Read on and learn how to combine Word documents for easier sharing.

  • Merge Documents by Inserting the Entire File
  • Merge Documents by Cloning Contents

Programming Environment

In this test, Free Spire.Doc for .NET is introduced into the program. The Spire.Doc.dll file can be referenced by:

Method 1: Download Free Spire.Doc for .NET locally, unzip it, and install it. After the installation is complete, find Spire.Doc.dll in the BIN folder under the installation path. Then open the "Solution Explorer" in Visual Studio, right-click "References", "Add Reference", and add a reference to the dll file in the BIN folder of the local path to the program.

Method 2: Install via NuGet. It can be installed by the following 2 methods:

(1) You can open the "Solution Explorer" in Visual Studio, right-click "References", "Manage NuGet Packages", then search for "Free Spire.Doc", and click "Install". Wait for the program installation to complete.

(2) Copy the following content to the PM console installation.

Install-Package FreeSpire.Doc -Version 10.8.0

Merge Documents by Inserting the Entire File

  • Create a Document instance.
  • Load the original Word document using Document.LoadFromFile() method.
  • Insert another Word document entirely to the original document using Document.InsertTextFromFile() method.
  • Save the result document using Document.SaveToFile() method.

Full Code

C#

  1. using Spire.Doc; 
  2. namespace MergeWord 
  3. { 
  4. class Program 
  5. { 
  6. static void Main(string[] args) 
  7. { 
  8. //Create a Document instance  
  9. Document document = new Document(); 
  10. //Load the original Word document 
  11. document.LoadFromFile("Doc1.docx", FileFormat.Docx); 
  12. //Insert another Word document entirely to the original document 
  13. document.InsertTextFromFile("Doc2.docx", FileFormat.Docx); 
  14. //Save the result document 
  15. document.SaveToFile("MergedWord.docx", FileFormat.Docx); 
  16. } 
  17. } 
  18. } 

VB.NET

  1. Imports Spire.Doc 
  2. Namespace MergeWord 
  3. Friend Class Program 
  4. Private Shared Sub Main(ByVal args As String()) 
  5. 'Create a Document instance  
  6. Dim document As Document = New Document() 
  7. 'Load the original Word document 
  8. document.LoadFromFile("Doc1.docx", FileFormat.Docx) 
  9. 'Insert another Word document entirely to the original document 
  10. document.InsertTextFromFile("Doc2.docx", FileFormat.Docx) 
  11. 'Save the result document 
  12. document.SaveToFile("MergedWord.docx", FileFormat.Docx) 
  13. End Sub 
  14. End Class 
  15. End Namespace 

Effective Shot




Merge Documents by Cloning Contents

  • Load two Word documents.
  • Loop through the second document to get all the sections using Document.Sections property, and then loop through all the sections to get their child objects using Section.Body.ChildObjects property.
  • Get the last section of the first document using Document.LastSection property, and then add the child objects to the last section of the first document using LastSection.Body.ChildObjects.Add() method.
  • Save the result document using Document.SaveToFile() method.

Full Code

C#

  1. using Spire.Doc; 
  2. namespace MergeWord 
  3. { 
  4. class Program 
  5. { 
  6. static void Main(string[] args) 
  7. { 
  8. //Load two Word documents 
  9. Document doc1 = new Document("Doc1.docx"); 
  10. Document doc2 = new Document("Doc2.docx"); 
  11. //Loop through the second document to get all the sections 
  12. foreach (Section section in doc2.Sections) 
  13. { 
  14. //Loop through the sections of the second document to get their child objects 
  15. foreach (DocumentObject obj in section.Body.ChildObjects) 
  16. { 
  17. // Get the last section of the first document 
  18. Section lastSection = doc1.LastSection; 
  19. //Add all child objects to the last section of the first document 
  20. lastSection.Body.ChildObjects.Add(obj.Clone()); 
  21. } 
  22. } 
  23. // Save the result document 
  24. doc1.SaveToFile("MergeDocuments.docx", FileFormat.Docx); 
  25. } 
  26. } 
  27. }  

VB.NET

  1. Imports Spire.Doc 
  2. Namespace MergeWord 
  3. Friend Class Program 
  4. Private Shared Sub Main(ByVal args As String()) 
  5. 'Load two Word documents 
  6. Dim doc1 As Document = New Document("Doc1.docx") 
  7. Dim doc2 As Document = New Document("Doc2.docx") 
  8. 'Loop through the second document to get all the sections 
  9. For Each section As Section In doc2.Sections 
  10. 'Loop through the sections of the second document to get their child objects 
  11. For Each obj As DocumentObject In section.Body.ChildObjects 
  12. ' Get the last section of the first document 
  13. Dim lastSection As Section = doc1.LastSection 
  14. 'Add all child objects to the last section of the first document 
  15. lastSection.Body.ChildObjects.Add(obj.Clone()) 
  16. Next 
  17. Next 
  18. ' Save the result document 
  19. doc1.SaveToFile("MergeDocuments.docx", FileFormat.Docx) 
  20. End Sub 
  21. End Class 
  22. End Namespace 


Effective Shot


Conclusion:

In this post, you have learned how to align text in Word in C#/VB.NET. Not only that, we also have other functions, such as, C#/VB.NET: Add Document Properties to Word DocumentsC#/VB.NET: Compare Two Word Documents and so on. Apart from that, if you'd like to learn more, you can visit this link to explore more about for Spire.Doc for .NET.





Comments

Popular posts from this blog

How to Convert OpenDocument Presentation (.odp) to PDF via Java Application

Java: How to encrypt or decrypt PDF documents?

How to Change Font Color in Word via Java