How to Convert PDF Files from Color to Black and White (Grayscale) in .NET with C#

Learn how to convert color PDFs to black and white using pdfRest Convert PDF Colors API Tool with C#
Share this page

Why Convert PDFs from Color to Black and White with C#?

The pdfRest Convert PDF Colors API Tool is designed to help developers programmatically convert the color profile of PDF documents using a simple API call. By using C#, a powerful and widely-used programming language, you can integrate this functionality into your applications, enabling automated processing of PDFs to match specific color requirements. This tutorial will guide you through the process of sending an API call to convert a color PDF to black and white using C#, providing a practical example and detailed explanation of the code.

You might need to convert PDF colors to grayscale for various reasons, such as ensuring better readability on certain devices or reducing file size. For instance, a document preparation specialist might need to convert a colorful PDF report into a black and white version for archival purposes or to reduce printing costs. By leveraging the Convert PDF Colors API, such tasks can be automated, saving time and reducing the potential for human error.

Convert PDFs from Color to Black and White 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, "pdf-with-converted-colors"))
    {
        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");
        var byteAryContent = new ByteArrayContent(byteArray);
        multipartContent.Add(byteAryContent, "file", "file_name");
        byteAryContent.Headers.TryAddWithoutValidation("Content-Type", "application/pdf");

        var byteArrayOption = new ByteArrayContent(Encoding.UTF8.GetBytes("gamma-22"));
        multipartContent.Add(byteArrayOption, "color_profile");


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

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

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

Source: GitHub Repository

Breaking Down the Code

The code begins by creating an instance of HttpClient with a base address set to the pdfRest API URL:

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

Next, an HttpRequestMessage is configured for a POST request to the "pdf-with-converted-colors" endpoint:

using (var request = new HttpRequestMessage(HttpMethod.Post, "pdf-with-converted-colors"))

The API key is added to the request headers for authentication, and the request is set to accept JSON responses:

request.Headers.TryAddWithoutValidation("Api-Key", "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");
request.Headers.Accept.Add(new("application/json"));

A MultipartFormDataContent object is created to hold the file and additional parameters. The PDF file is read into a byte array and added to the content with the appropriate content type:

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

The color profile option is specified as "srgb" and added to the multipart content:

var byteArrayOption = new ByteArrayContent(Encoding.UTF8.GetBytes("gamma-22"));
multipartContent.Add(byteArrayOption, "color_profile");

The request is sent asynchronously, and the response is read and printed to the console:

var response = await httpClient.SendAsync(request);
var apiResult = await response.Content.ReadAsStringAsync();
Console.WriteLine("API response received.");
Console.WriteLine(apiResult);

Beyond the Tutorial

In this tutorial, you learned how to use C# to call the pdfRest Convert PDF Colors API, enabling you to automate the conversion of color PDFs to black and white. This example demonstrates how to set up and send a multipart API request, process the response, and handle PDF files programmatically.

To explore more functionalities, demo all of the pdfRest API Tools in the API Lab. For more detailed information, refer to the API Reference Guide. Note that this is an example of a multipart API call, and code samples using JSON payloads can be found at GitHub Repository.

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