How to Convert PDF to PDF/A in .NET with C#

Learn how to use pdfRest Convert to PDF/A API Tool to convert standard PDF to PDF/A conformant files with C#.
Share this page

Why Use Convert to PDF/A with C#?

The pdfRest Convert to PDF/A API Tool is an online service that allows users to convert PDF documents to the PDF/A format, which is an ISO-standardized version of the PDF specialized for the digital preservation of electronic documents. This tutorial will guide you through the process of sending an API call to Convert to PDF/A using C#.

One real-world example of why a user might use Convert to PDF/A is to ensure that important documents, such as legal contracts or archival records, are preserved in a format that maintains their visual appearance over time, even as software and technology evolve. PDF/A helps to ensure that documents remain readable and accessible for the long term.

Convert to PDF/A 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, "pdfa"))
    {
        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_name.pdf");
        byteAryContent.Headers.TryAddWithoutValidation("Content-Type", "application/pdf");

        var byteArrayOption = new ByteArrayContent(Encoding.UTF8.GetBytes("PDF/A-3u"));
        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 on GitHub

Breaking Down the Code

The code example above demonstrates how to use C# to make an API call to the pdfRest Convert to PDF/A endpoint. Let's break down the key parts of the code:

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

This line initializes a new instance of the HttpClient class and sets its BaseAddress to the pdfRest API base URL.

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

This line adds your API key to the request headers. Replace the placeholder with your actual API key.

var multipartContent = new MultipartFormDataContent();

This line creates a new instance of MultipartFormDataContent, which allows you to send files and data in the same request.

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

These lines read the file to be converted, create a ByteArrayContent object with the file's binary data, and add it to the multipart content with the name "file". The file's content type is also set to "application/pdf".

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

This snippet specifies the output type for the conversion. In this case, "PDF/A-3u" is chosen, but you can select another PDF/A conformance level as needed.

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

These lines send the request to the pdfRest API and read the response as a string.

Beyond the Tutorial

By following this tutorial, you've learned how to make a multipart API call to convert a PDF to the PDF/A format using C#. This can be particularly useful for ensuring long-term preservation and accessibility of important documents.

To explore more capabilities and try out all of the pdfRest API Tools, visit the API Lab. For further information and detailed documentation, refer to the API Reference.

Note: This is an example of 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