As a C# developer, you may need to combine two or more PDF files into a single PDF. In such cases, if you don’t want to print various PDF files like reports, receipts, etc. one by one then combine them into one document and print. In this article, I am covering how to merge PDF files using a REST API.

The following topics shall be covered in this article:
File Merger REST API and .NET SDK
For merging files, I will be using the .NET SDK of GroupDocs.Merger Cloud API. It is a feature-rich and high-performance Cloud SDK used to merge several documents into one, split a single document into multiple documents. It offers functionality to reorder or replace document pages, change page orientation, manage document passwords and perform other manipulations easily for any supported file format. Currently, it also provides Java, PHP, Ruby, Android, and Node.js SDKs as its document merger family members for the Cloud API.
You can install GroupDocs.Merger-Cloud to your Visual Studio project from the NuGet Package manager or using the following command in the Package Manager console:
Install-Package GroupDocs.Merger-Cloud
You need to get your Client ID and Client Secret from the dashboard before you start following the steps and available code examples. Add your Client ID and Client Secret in the code as demonstrated below:
string MyClientId = "YOUR-CLIENT-ID"; | |
string MyClientSecret = "YOUR-CLIENT-SECRET"; | |
string MyStorage; | |
var storageConfig = new Configuration(MyClientId, MyClientSecret); | |
var documentApi = new DocumentApi(storageConfig); | |
var storageApi = new StorageApi(storageConfig); | |
var fileApi = new FileApi(storageConfig); |
Merge PDF Files using a REST API
You can combine two or more PDF files or merge specific pages of PDFs by following the simple steps mentioned below:
- Upload the PDF documents to the Cloud
- Merge the uploaded PDF files
- or Combine Specific Pages of a PDF File with Another File
- Download the merged file
Upload the PDF Documents
Firstly, upload the PDF documents to the Cloud using any of the following methods:
- Using the dashboard
- Upload all files one by one using Upload File API from the browser
- Upload programmatically using the code example given below:
public static void UploadFiles(StorageApi storageApi, FileApi fileApi) | |
{ | |
var path = @"C:\Files"; | |
var files = Directory.GetFiles(path, "*.pdf", SearchOption.AllDirectories); | |
foreach (var file in files) | |
{ | |
var relativeFilePath = file.Replace(path, string.Empty).Trim(Path.DirectorySeparatorChar); | |
var response = storageApi.ObjectExists(new ObjectExistsRequest(relativeFilePath, MyStorage)); | |
if (response.Exists != null && !response.Exists.Value) | |
{ | |
var fileStream = File.Open(file, FileMode.Open); | |
fileApi.UploadFile(new UploadFileRequest(relativeFilePath, fileStream, MyStorage)); | |
fileStream.Close(); | |
} | |
} | |
Console.WriteLine("File Upload Process Completed."); | |
} |
As the result, PDF files will be uploaded to the Cloud Storage.

Uploaded PDF files at dashboard.groupdocs.cloud/files
Merge the Uploaded PDF Files
This simple code example demonstrates how to merge multiple PDF files using a REST API into a single PDF.
public static void MergeMultipleFiles(DocumentApi documentApi, FileApi fileApi) | |
{ | |
try | |
{ | |
var item1 = new JoinItem | |
{ | |
FileInfo = new GroupDocs.Merger.Cloud.Sdk.Model.FileInfo | |
{ | |
FilePath = "one-page.pdf" | |
} | |
}; | |
var item2 = new JoinItem | |
{ | |
FileInfo = new GroupDocs.Merger.Cloud.Sdk.Model.FileInfo | |
{ | |
FilePath = "second-page.pdf" | |
} | |
}; | |
var options = new JoinOptions | |
{ | |
JoinItems = new List<JoinItem> { item1, item2 }, | |
OutputPath = "merged-files.pdf" | |
}; | |
var request = new JoinRequest(options); | |
var response = documentApi.Join(request); | |
Console.WriteLine("Output file path: " + response.Path); | |
// Download the Merged File | |
DownloadFiles(fileApi, "merged-files.pdf"); | |
} | |
catch (Exception e) | |
{ | |
Console.WriteLine("Exception while calling api: " + e.Message); | |
} | |
} |
Combine Specific Pages of a PDF File with another File
You may combine specific pages of a PDF file with another file. For this purpose, you need to provide a range of pages as demonstrated in the code example given below.
public static void MergeSpecificPagesOfFiles(DocumentApi documentApi, FileApi fileApi) | |
{ | |
try | |
{ | |
var item1 = new JoinItem | |
{ | |
FileInfo = new GroupDocs.Merger.Cloud.Sdk.Model.FileInfo | |
{ | |
FilePath = "sample-10-pages.pdf" | |
}, | |
}; | |
var item2 = new JoinItem | |
{ | |
FileInfo = new GroupDocs.Merger.Cloud.Sdk.Model.FileInfo | |
{ | |
FilePath = "four-pages.pdf" | |
}, | |
StartPageNumber = 1, | |
EndPageNumber = 3, | |
RangeMode = JoinItem.RangeModeEnum.OddPages | |
}; | |
var options = new JoinOptions | |
{ | |
JoinItems = new List<JoinItem> { item1, item2 }, | |
OutputPath = "merged-pages.pdf" | |
}; | |
var request = new JoinRequest(options); | |
var response = documentApi.Join(request); | |
Console.WriteLine("Output file path: " + response.Path); | |
// Download the Merged File | |
DownloadFiles(fileApi, "merged-pages.pdf"); | |
} | |
catch (Exception e) | |
{ | |
Console.WriteLine("Exception while calling api: " + e.Message); | |
} | |
} |
Download the Merged File
The above code sample will save the merged PDF file on the cloud. You can download it using the following code sample:
public static void DownloadFiles(FileApi fileApi, string file) | |
{ | |
try | |
{ | |
var downloadRequest = new DownloadFileRequest(file, MyStorage); | |
Stream downloadResponse = fileApi.DownloadFile(downloadRequest); | |
using (var fileStream = File.Create("C:\\Files\\" + file)) | |
{ | |
downloadResponse.Seek(0, SeekOrigin.Begin); | |
downloadResponse.CopyTo(fileStream); | |
} | |
Console.WriteLine("File downloaded successfully"); | |
} | |
catch (Exception e) | |
{ | |
Console.WriteLine("Exception while calling api: " + e.Message); | |
} | |
} |
Conclusion
In this article, you have learned how to combine two or more PDF files or specific pages of PDF files on the cloud with .NET Merger REST API using C#. You also learned how to programmatically upload the files on the cloud and then download them from the cloud. You can learn more about GroupDocs.Merger Cloud API from the documentation. In case of any ambiguity, feel free to contact support.