Merge Word Documents using C#

Merge Word Documents using C#

We can easily merge two or more Word documents into a single document using C#. We do it because sharing or printing a single file is easier than processing multiple files. In this article, we will learn how to merge Word documents using C#.

The following topics shall be covered in this article:

C# API to Merge Word Documents

For merging DOC or DOCX files, we will be using GroupDocs.Merger for .NET API. Please either download the DLL of the API or install it using NuGet.

Install-Package GroupDocs.Merger

Merge Two or More Word Documents using C#

We can easily merge two or more Word documents programmatically by following the steps given below:

  • Firstly, load the DOCX file using the Merger class.
  • Next, call the Merger.Join() method with target DOCX file path to merge with the loaded file.
  • Then, repeat the above step to merge more files.
  • Finally, call the Merger.Save() method with the output file path to save the merged file.

The following code sample shows how to merge two or more DOCX files using C#.

Merge two or more Word Documents using C#
Merge two or more Word Documents using C#.

Combine Specific Pages of Word Documents using C#

We can combine specific pages of Word documents programmatically by following the simple steps mentioned below:

  • Firstly, load the DOCX file using the Merger class.
  • Next, create an instance of the JoinOptions class with the start page and end page number. You may also set range mode to join odd or even pages from the specified page range.
  • Then, call the Merger.Join() method with the target DOCX file path and JoinOptions object as arguments. The JoinOptions object merges specific pages of the target file with the source file.
  • Finally, call the Merger.Save() method with the output file path to save the merged file.

The following code sample shows how to combine selected pages of Word documents using C#.

Merge DOCX Files and Secure with Password using C#

We can merge two or more DOCX files and then secure the merged file with a password programmatically by following the simple steps given below:

  • Firstly, load the DOCX file using the Merger class.
  • Next, call the Merger.Join() method with target DOCX file path to merge with the loaded file.
  • Optionally, repeat the above step to merge more files.
  • Then, set password using AddPasswordOptions
  • After that, call the Merger.AddPassword() method with AddPasswordOptions.
  • Finally, call the Merger.Save() method to save the password-protected merged file.

The following code sample shows how to merge multiple Word documents and then secure the merged file with a password using C#.

Merge Word Documents into PDF using C#

We can merge Word documents into a PDF document programmatically by following the simple steps given below:

  • Firstly, load the PDF file using the Merger class.
  • Next, call the Merger.Join() method with target DOCX file path to merge with the loaded file.
  • Optionally, repeat the above step to merge more files.
  • Finally, call the Merger.Save() method with the output PDF file path to save the merged file.

The following code sample shows how to merge DOCX files into a PDF file using C#.

Get a Free License

Please try the API without evaluation limitations by requesting a free temporary license.

Conclusion

In this article, we have learned how to merge two or more Word documents using C#. We have also seen how to combine specific pages of Word documents programmatically. This article also explained how to merge DOCX files into a PDF file using C#. Besides, you can learn more about GroupDocs.Merger for .NET API using the documentation. In case of any ambiguity, please feel free to contact us on the forum.

See Also