How to Convert PDF to PDF/X with .NET C#

Learn how to ensure PDF documents are PDF/X compliant with the Convert to PDF/X API Tool from pdfRest.
Share this page

Why Convert to PDF/X with C#?

The pdfRest Convert to PDF/X API Tool is a powerful resource for developers who need to ensure that their PDF files meet the PDF/X standard, which is a subset of the PDF specification that is optimized for the reliable prepress document exchange. This tutorial will demonstrate how to send an API call to Convert to PDF/X using C#. A real-world example of why a user might use Convert to PDF/X is to prepare a document for professional printing, where color consistency and font embedding are crucial.

Convert to PDF/X 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, "pdfx"))
    {
        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("PDF/X-3"));
        multipartContent.Add(byteArrayOption, "output_type");

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

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

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

Reference: pdf-rest-api-samples

Breaking Down the C# Code

The provided code snippet is a C# example of how to call the pdfRest API to convert a PDF file to the PDF/X format. Let's break down the key parts of the code:

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

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

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

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

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

These lines read the PDF file as a byte array, create a ByteArrayContent object with it, and add it to the MultipartFormDataContent with the name "file". The "file_name" should be the name of the file you are uploading.

var byteArrayOption = new ByteArrayContent(Encoding.UTF8.GetBytes("PDF/X-3"));
multipartContent.Add(byteArrayOption, "output_type");

This specifies the desired output type for the conversion. In this case, "PDF/X-3" is specified as the output type.

var response = await httpClient.SendAsync(request);

This line sends the multipart/form-data request to the pdfRest API and waits for the response.

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

Once the response is received, this line reads the content of the response asynchronously as a string.

Beyond the Tutorial

In this tutorial, we've learned how to make a multipart/form-data API call to the pdfRest Convert to PDF/X API using C#. This allows you to programmatically convert PDF files to the PDF/X standard, which is essential for professional printing purposes. You are encouraged to 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 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.

Compare Plans
Contact Us