How to Split PDF in .NET with C#

Share this page

Why Split a PDF Apart?

The pdfRest Split PDF API Tool is a powerful resource for developers who need to programmatically split PDF documents into separate files based on page ranges. This tutorial will guide you through the process of making an API call to the Split PDF endpoint using C# to split a PDF document.

One example for using this tool would be to extract specific chapters or sections from a large PDF document for separate distribution or analysis.

C# Code Example For Splitting a PDF

using System.Text;

using (var httpClient = new HttpClient { BaseAddress = new Uri("https://api.pdfrest.com") })
{
    using (var request = new HttpRequestMessage(HttpMethod.Post, "split-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("1"));
        multipartContent.Add(byteArrayOption, "pages[]");

        var byteArrayOption2 = new ByteArrayContent(Encoding.UTF8.GetBytes("2-last"));
        multipartContent.Add(byteArrayOption2, "pages[]");

        var byteArrayOption3 = new ByteArrayContent(Encoding.UTF8.GetBytes("split"));
        multipartContent.Add(byteArrayOption3, "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 of the provided code: pdf-rest-api-samples

Breaking Down the Code

The code snippet above demonstrates how to make a multipart/form-data POST request to the pdfRest Split PDF API endpoint using C#. Let's break down the key parts:

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

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

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

The API key is added to the request headers for authentication. Replace the placeholder with your actual API key.

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

This reads the PDF file as a byte array and adds it to the multipart form data content with the key "file" and a filename.

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

Here, we're specifying the pages to be split. In this case, "1" indicates the first page of the PDF. The "pages[]" key is used to pass an array of page ranges to the API.

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

This sets the output format to "split", which instructs the API to split the PDF into separate files.

var response = await httpClient.SendAsync(request);

This sends the POST request to the API and waits for the response.

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

Finally, the response from the API is read as a string and printed to the console.

Next Steps with pdfRest

In this tutorial, we've covered how to use C# to make an API call to the pdfRest Split PDF endpoint. We've gone through the process of constructing a multipart/form-data request, adding the necessary headers, and handling the API response. With this knowledge, developers can integrate PDF splitting functionality into their applications.

Feel free to demo all of the pdfRest API Tools in the API Lab, and refer to the API Reference documentation for more information.

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.