How to merge multiple PDF Documents together in C# .NET Framework

Cloudmersive
2 min readJul 20, 2020

Merging documents is quite a difficult feature to set up in C#, requiring many elements to be accounted for, such as headers, footers, and formatting choices. Add to this the initial challenge of parsing the format to begin with and you are in for a major challenge. To make life nice and easy again, I am here today to bring you a two-step process, by which you can start merging PDF documents almost immediately.

First we import our package:

Install-Package Cloudmersive.APIClient.NET.DocumentAndDataConvert -Version 3.2.8

Now we call our PDF merge function, inputting as many PDFs as we would like:

using System;using System.Diagnostics;using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Api;using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Client;using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Model;namespace Example{public class MergeDocumentPdfMultiExample{public void main(){// Configure API key authorization: ApikeyConfiguration.Default.AddApiKey("Apikey", "YOUR_API_KEY");// Uncomment below to setup prefix (e.g. Bearer) for API key, if needed// Configuration.Default.AddApiKeyPrefix("Apikey", "Bearer");var apiInstance = new MergeDocumentApi();var inputFile1 = new System.IO.Stream(); // System.IO.Stream | First input file to perform the operation on.var inputFile2 = new System.IO.Stream(); // System.IO.Stream | Second input file to perform the operation on.var inputFile3 = new System.IO.Stream(); // System.IO.Stream | Third input file to perform the operation on. (optional)var inputFile4 = new System.IO.Stream(); // System.IO.Stream | Fourth input file to perform the operation on. (optional)var inputFile5 = new System.IO.Stream(); // System.IO.Stream | Fifth input file to perform the operation on. (optional)var inputFile6 = new System.IO.Stream(); // System.IO.Stream | Sixth input file to perform the operation on. (optional)var inputFile7 = new System.IO.Stream(); // System.IO.Stream | Seventh input file to perform the operation on. (optional)var inputFile8 = new System.IO.Stream(); // System.IO.Stream | Eighth input file to perform the operation on. (optional)var inputFile9 = new System.IO.Stream(); // System.IO.Stream | Ninth input file to perform the operation on. (optional)var inputFile10 = new System.IO.Stream(); // System.IO.Stream | Tenth input file to perform the operation on. (optional)try{// Merge Multple PDF Files Togetherbyte[] result = apiInstance.MergeDocumentPdfMulti(inputFile1, inputFile2, inputFile3, inputFile4, inputFile5, inputFile6, inputFile7, inputFile8, inputFile9, inputFile10);Debug.WriteLine(result);}catch (Exception e){Debug.Print("Exception when calling MergeDocumentApi.MergeDocumentPdfMulti: " + e.Message );}}}}

And just like that, we are finished!

--

--

Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.