How to Validate PDF/A Conformance in .NET with C#

Learn how to use pdfRest Query PDF API Tool with C# to validate PDF/A conformance
Share this page

Why Validate PDF/A with C#?

The pdfRest Query PDF API Tool is a powerful resource for developers looking to extract information from PDF files programmatically. By leveraging this API, you can quickly obtain metadata and other details about your PDF documents without the need for manual inspection or complex parsing logic. This tutorial will guide you through the process of making an API call to the Query PDF endpoint using C#, a popular programming language for enterprise and desktop applications.

In the real world, a user might use the Query PDF API to automate the process of cataloging a large number of PDF documents, extracting key information such as titles, page counts, languages, and authorship. For instance, a digital library service could use this API to enhance their search functionality by indexing additional metadata from their PDF collection, or a legal firm might need to quickly assess the properties of numerous case files without opening each one individually.

Validate 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, "pdf-info"))
    {
        request.Headers.TryAddWithoutValidation("Api-Key", "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");
        request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("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("pdfa"));
        multipartContent.Add(byteArrayOption, "queries");

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

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

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

Reference to the code: pdf-rest-api-samples repository on GitHub.

Breaking Down the Code

The provided code block demonstrates how to create an HTTP client, configure a multipart/form-data request, and send it to the pdfRest API to obtain information about a PDF file. Here's a breakdown of the key components:

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

This line initializes a new instance of the HttpClient class, setting the base address for the API calls to the pdfRest API endpoint.

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

This line adds your API key to the request headers for authentication with the pdfRest API service. Replace the placeholder with your actual API key.

var multipartContent = new MultipartFormDataContent();

A new instance of MultipartFormDataContent is created to hold the file and queries as separate parts of the multipart request.

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, wrap it in a ByteArrayContent object, and add it to the multipart request with the name "file". Replace "/path/to/file" with the actual file path.

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

This snippet creates another ByteArrayContent object containing the queries for the desired PDF information and adds it to the multipart request. In this case, we are only querying "pdfa", which triggers a PDF/A validation check.

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, which contains the requested PDF information in JSON format.

Beyond the Tutorial

Through this tutorial, you've learned how to construct and send a multipart/form-data request to the pdfRest API using C#. The response from the API provides valuable information about the PDF file, which can be used for a variety of purposes depending on your application's needs.

To explore more capabilities and demo all of the pdfRest API Tools, visit the API Lab. For comprehensive documentation and additional details on how to use the API, refer to the API Reference Guide.

Note: This is an example of a multipart API call. Code samples using JSON payloads can be found at the pdf-rest-api-samples repository on 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