To combine the data that is present in multiple documents, and sometimes in documents of different file types, there comes the need to merge all your documents or the portion of the documents into one. In this article, you will learn, how to programmatically merge multiple documents of either the same or different file types into one file using C#.
The following are the topics covered below:
- .NET API - Merge Multiple Document Types
- Merge PDF, Word, Excel files into one PDF
- Merge Selective Pages of Multiple files into One File
.NET API for Merging Multiple Document Types
Today, I will be using GroupDocs.Merger for .NET to combine documents of different file formats into one file. The .NET API allows joining various documents of the same or different formats into one file. Furthermore, it allows documents to split, trim documents, and swap, move, remove, rotate, or arrange pages. Additionally, it supports setting or remove passwords to manage the security of the supported document formats.
Some of the document types the API supports include; word-processing documents, spreadsheets, presentations, HTML, PDF, eBooks, Visio drawings, CSV, and TSV.
PM> Install-Package GroupDocs.Merger
Merge PDF, Word, Excel files into one PDF in C#
You can combine your PDF documents with your Word documents, presentations and Excel spreadsheets with just a few lines of code. The following are the steps of how to merge documents of multiple file types into one file.
- Load the source document using the Merger class.
- Keep merging other documents using the Join method.
- Save the combined document as output using the Save method.
The following source code shows how to merge PDF, Word, and Excel documents into one PDF file in C#.
In the same way, you can also combine files of the same file format. The below-mentioned is the output obtained by joining a word document, a PDF document. and a spreadsheet using the above C# code.
Merge Selective Pages of Multiple PDF, Word, Excel files into One PDF in C#
Not always you want to combine the whole document. You may want to pick few pages from one document and some other pages from the next document, and so on. The API provides different ways to merge selective pages of multiple file types into one file.
- Load the source document using Merger class.
- Set the merging options using JoinOptions class.
- Merge the document using the Join method.
- Keep combining the documents by setting different joining options for each document.
- Save the merged document using the Save method.
The following source code shows how to merge a PDF file with the first page of a Word document, and the even sheets of Excel workbook in the provided range, into a single PDF file using C#.
To sum up, you have seen how to merge two or more documents of different file types into one file using C# within .NET application. Additionally, you have learned how to combine just the selective pages of multiple file types.