How to Convert PDF Files from RGB to CMYK in .NET with C#

Learn how to convert PDF color profile from RGB to CMYK using pdfRest API Toolkit with C#
Share this page

Why Convert PDF from RGB to CMYK with C#?

The pdfRest Convert PDF Colors API Tool is a valuable resource for developers working with PDF documents that require precise color control, especially when converting from RGB to CMYK. This tutorial will guide you through the process of using C# to send an API call to the Convert PDF Colors endpoint, allowing you to effortlessly achieve this color profile conversion.

Converting RGB (Red, Green, Blue) colors to CMYK (Cyan, Magenta, Yellow, Black) is essential for preparing PDF documents for printing. CMYK is the color model used by most printers, and accurately converting RGB colors to CMYK ensures that the printed colors match the intended appearance. This is particularly important for projects like brochures, magazines, and marketing materials where color accuracy is crucial.

Convert PDF from RGB to CMYK 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("acrobat9-cmyk"));
        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

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

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

Next, a HttpRequestMessage is instantiated for a POST request to the pdf-with-converted-colors endpoint. The API key is added to the request headers to authenticate the call:

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

The request expects a JSON response, indicated by adding "application/json" to the Accept headers:

request.Headers.Accept.Add(new("application/json"));

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

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

The content type for the file is set to "application/pdf". The color profile option is specified as "acrobat9-cmyk" and added to the multipart content:

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

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

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

Beyond the Tutorial

In this tutorial, you have learned how to use C# to call the pdfRest Convert PDF Colors API, allowing you to convert the color profiles of PDF documents from RGB to CMYK programmatically. This can be an invaluable tool for ensuring consistency across different media and platforms.

To explore more features, 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 that this is an example of a multipart API call, and 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