How to Delete Pages from PDF Files in .NET with C#

Learn how to Delete Pages from PDF Files in .NET with C# by calling Split PDF API Tool by pdfRest.
Share this page

Why Use Split PDF with C#?

The pdfRest Split PDF API Tool is a powerful resource that allows developers to programmatically extract pages PDF documents into separate files. This tutorial will guide you through the process of sending an API call to the Split PDF endpoint using C#, to delete page 4 of a given document.

Splitting PDFs can be useful in various scenarios, such as when you need to distribute individual sections of a large document to different team members or when you want to extract specific pages for separate processing.

Split PDF with C# Code Example

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.pdf");
        byteAryContent.Headers.TryAddWithoutValidation("Content-Type", "application/pdf");

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

        var byteArrayOption3 = new ByteArrayContent(Encoding.UTF8.GetBytes("deleted_page_4"));
        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 code reference: pdf-rest-api-samples

Breaking Down the Code

The provided code block is a C# example of how to make a multipart/form-data POST request to the pdfRest Split PDF API endpoint. Let's break down the key parts of this code:

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

This initializes a new instance of the HttpClient class with the base address set to the pdfRest API.

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

This line adds your API key to the request headers for authentication with the pdfRest API.

var multipartContent = new MultipartFormDataContent();

This initializes a new instance of MultipartFormDataContent to build the multipart/form-data request body.

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

Here, the code reads the PDF file as a byte array, wraps it in a ByteArrayContent object, and adds it to the multipart content with the form field name "file".

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

This snippet creates a new ByteArrayContent object with the bytes representing the string "1-3,5-last", which specifies the page range to include in the final document. In this case, the final document will have all the pages except page 4. The "pages[]" field can be repeated to specify multiple output documents having different page ranges.

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

The "output" field specifies the naming convention for the split files, in this case, indicating that it's a version of the document with page 4 deleted.

Beyond the Tutorial

By following the tutorial above, you have learned how to use C# to send an API call to the pdfRest Split PDF endpoint. This allows you to split a PDF document into separate files programmatically. You can now demo all of the pdfRest API Tools in the API Lab at https://pdfrest.com/apilab/ and refer to the API Reference documentation at https://pdfrest.com/documentation/ for further exploration of the capabilities offered by pdfRest.

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