How to Merge Two PDF Documents Together in C# .NET Framework

Cloudmersive
2 min readJan 18, 2024

--

Our PDFs are typically just static iterations of content created in other applications — and this makes it quite viable to manually combine PDFs for assembly into larger PDF documents. This same process can be a bit tricky to pull off with code, however, so it’s not a bad idea to lean on external services to get the job done.

Using the ready-to-run code examples below, we can take advantage of a free API that will combine two of our PDF documents into a single PDF document. The order of content in the resulting document will match the order the files were uploaded in, so we can easily and intuitively create the organized, logical content that we’re looking for.

To structure our API call, we can start by installing the SDK. Let’s install via NuGet by running the following command in our Package Manager console:

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

Next, let’s go ahead and obtain a free-tier API key so we can authorize our API calls. Free-tier API keys will allow a limit of 800 API calls per month with zero additional commitments; once we reach our call limit, it will simply reset the following month.

Our final step is to include the remaining code in our file. We can now authorize our request and load in our PDF files:

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 MergeDocumentPdfExample
{
public void main()
{
// Configure API key authorization: Apikey
Configuration.Default.AddApiKey("Apikey", "YOUR_API_KEY");

var apiInstance = new MergeDocumentApi();
var inputFile1 = new System.IO.FileStream("C:\\temp\\inputfile", System.IO.FileMode.Open); // System.IO.Stream | First input file to perform the operation on.
var inputFile2 = new System.IO.FileStream("C:\\temp\\inputfile", System.IO.FileMode.Open); // System.IO.Stream | Second input file to perform the operation on (more than 2 can be supplied).

try
{
// Merge Two PDF Files Together
byte[] result = apiInstance.MergeDocumentPdf(inputFile1, inputFile2);
Debug.WriteLine(result);
}
catch (Exception e)
{
Debug.Print("Exception when calling MergeDocumentApi.MergeDocumentPdf: " + e.Message );
}
}
}
}

Now we have a simple, low-code solution for merging PDF documents.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet