How to Merge PDF Files with .NET C#

Share this page

Why Merge PDF Files?

The pdfRest Merge PDFs API Tool allows users to combine multiple PDF files into a single PDF document. This can be particularly useful in scenarios such as consolidating reports, merging scanned documents, or combining chapters of a book into a single file. This tutorial will demonstrate how to make an API call to the Merge PDFs endpoint using C#.

C# Code Sample for Merging

using System.Text;

using (var httpClient = new HttpClient { BaseAddress = new Uri("https://api.pdfrest.com") })
{
    using (var request = new HttpRequestMessage(HttpMethod.Post, "merged-pdf"))
    {
        request.Headers.TryAddWithoutValidation("Api-Key", "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");
        request.Headers.Accept.Add(new("application/json"));
        var multipartContent = new MultipartFormDataContent();

        var byteArray = File.ReadAllBytes("/path/to/file.pdf");
        var byteAryContent = new ByteArrayContent(byteArray);
        multipartContent.Add(byteAryContent, "file", "file_name.pdf");
        byteAryContent.Headers.TryAddWithoutValidation("Content-Type", "application/pdf");

        var byteArrayOption = new ByteArrayContent(Encoding.UTF8.GetBytes("file"));
        multipartContent.Add(byteArrayOption, "type[]");
        var byteArrayOption2 = new ByteArrayContent(Encoding.UTF8.GetBytes("all"));
        multipartContent.Add(byteArrayOption2, "pages[]");

        var byteArray2 = File.ReadAllBytes("/path/to/file.pdf");
        var byteAryContent2 = new ByteArrayContent(byteArray2);
        multipartContent.Add(byteAryContent2, "file", "file_name.pdf");
        byteAryContent2.Headers.TryAddWithoutValidation("Content-Type", "application/pdf");

        var byteArrayOption3 = new ByteArrayContent(Encoding.UTF8.GetBytes("file"));
        multipartContent.Add(byteArrayOption3, "type[]");
        var byteArrayOption4 = new ByteArrayContent(Encoding.UTF8.GetBytes("all"));
        multipartContent.Add(byteArrayOption4, "pages[]");

        var byteArrayOption5 = new ByteArrayContent(Encoding.UTF8.GetBytes("merged"));
        multipartContent.Add(byteArrayOption5, "output");

        request.Content = multipartContent;
        var response = await httpClient.SendAsync(request);

        var apiResult = await response.Content.ReadAsStringAsync();

        Console.WriteLine("API response received.");
        Console.WriteLine(apiResult);
    }
}

Source: pdf-rest-api-samples

Explaining the C# Code

The code block above demonstrates how to merge PDF files using the pdfRest API in C#. Let's break down each part of the code:

using (var httpClient = new HttpClient { BaseAddress = new Uri("https://api.pdfrest.com") })

This creates an HttpClient instance with the base address set to the pdfRest API endpoint.

using (var request = new HttpRequestMessage(HttpMethod.Post, "merged-pdf"))

A new HttpRequestMessage is created to make a POST request to the "merged-pdf" endpoint.

request.Headers.TryAddWithoutValidation("Api-Key", "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");

This adds your API key to the request headers. Replace "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" with your actual API key.

var multipartContent = new MultipartFormDataContent();

A MultipartFormDataContent object is created to hold the parts of the multipart request.

var byteArray = File.ReadAllBytes("/path/to/file.pdf");
var byteAryContent = new ByteArrayContent(byteArray);
multipartContent.Add(byteAryContent, "file", "file_name.pdf");

These lines read a PDF file into a byte array, wrap it in a ByteArrayContent, and add it to the multipart content with the name "file". The "file_name.pdf" should be replaced with the actual name of the file you want to merge.

var byteArrayOption = new ByteArrayContent(Encoding.UTF8.GetBytes("file"));
multipartContent.Add(byteArrayOption, "type[]");

This specifies the type of the content being added. In this case, "file" indicates that a file is being sent.

var byteArrayOption5 = new ByteArrayContent(Encoding.UTF8.GetBytes("merged"));
multipartContent.Add(byteArrayOption5, "output");

This specifies the desired output type, which is "merged" in this case, indicating that the API should merge the provided files.

So Much More with pdfRest

In this tutorial, we've learned how to use C# to make an API call to pdfRest's Merge PDFs endpoint. The provided code demonstrates how to set up the request, add files and options, and handle the response.

For further exploration and to demo all of the pdfRest API Tools, visit the API Lab. For more detailed information, refer to the API Reference documentation.

Note: This is an example of a multipart API call. Code samples using JSON payloads can be found at pdf-rest-api-samples.

Generate a self-service API Key now!

Create your FREE API Key to start processing PDFs in seconds, only possible with pdfRest.

Compare Plans
Contact Us