How to Convert PDF Colors in .NET with C#

Learn how to change PDF color profiles with Convert PDF Colors API tool by pdfRest using C#
Share this page

Why Convert PDF Colors with C#?

The pdfRest Convert PDF Colors API Tool is a powerful utility that allows developers to programmatically change the color profile of a PDF document using an API call. This tutorial will show you how to send an API call to Convert PDF Colors with C#, enabling you to automate the process of color conversion in your PDF workflows.

Consider a scenario where a printing company needs all PDF files to adhere to a specific color profile, such as sRGB, for consistent print quality. By using the Convert PDF Colors API, the company can automate the conversion process, ensuring that all incoming PDF files are converted to the required color profile before printing, thus saving time and reducing the risk of errors.

Convert PDF Colors 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("srgb"));
        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

Breaking Down the Code

Let's break down the provided code to understand how it works:

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

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

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

Here, we create a new HttpRequestMessage object for a POST request to the "pdf-with-converted-colors" endpoint.

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

These lines add the necessary headers to the request. The "Api-Key" header contains your API key for authentication, and the "Accept" header specifies that the response should be in JSON format.

var multipartContent = new MultipartFormDataContent();

This line creates a new instance of the MultipartFormDataContent class to hold the multipart form data for the request.

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");

In these lines, we read the PDF file into a byte array, create a ByteArrayContent object from the byte array, and add it to the multipart content with the name "file" and the file name "file_name". The content type is set to "application/pdf".

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

Here, we create another ByteArrayContent object for the color profile option, set to "srgb", and add it to the multipart content with the name "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);

Finally, we set the request content to the multipart content, send the request asynchronously, read the response content as a string, and print the API response to the console.

Beyond the Tutorial

In this tutorial, we demonstrated how to use the pdfRest Convert PDF Colors API Tool with C# to convert the color profile of a PDF document. This example showed how to create and send a multipart API call, handle the response, and print the result.

To explore more functionalities, we encourage you to demo all of the pdfRest API Tools in the API Lab. For detailed information on each API endpoint, refer to the API Reference Guide.

Note: This example uses a multipart API call. Code samples using JSON payloads can be found at GitHub.

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