How to Export PDF Form Data with .NET C#

Share this page

Why Export PDF Form Data with C#?

The pdfRest Export Form Data API Tool is designed to extract data from fillable PDF forms and return the data in various formats such as XML, FDF, or JSON. This tutorial will demonstrate how to make an API call to the Export Form Data endpoint using C# to programmatically retrieve data from a PDF form. This functionality can be useful in scenarios where you need to automate the extraction of form data for processing, such as collecting survey results or processing registration forms.

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, "exported-form-data"))
    {
        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("xml"));
        multipartContent.Add(byteArrayOption, "data_format");

        var byteArrayOption2 = new ByteArrayContent(Encoding.UTF8.GetBytes("extracted"));
        multipartContent.Add(byteArrayOption2, "output");


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

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

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

Source: pdf-rest-api-samples on GitHub

Explaining the C# Code

The code block above demonstrates how to send a multipart/form-data request to the pdfRest API to export form data from a PDF file.

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

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

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

The Api-Key is required for authentication. Replace the placeholder with your actual API key provided by pdfRest.

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

Read the PDF file as a byte array and add it to the multipart/form-data content with the field name "file" and the file name "file_name".

byteAryContent.Headers.TryAddWithoutValidation("Content-Type", "application/pdf");

This specifies the content type of the PDF file part in the multipart request.

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

The "data_format" field specifies the desired format for the exported data. In this case, "xml" is chosen.

var byteArrayOption2 = new ByteArrayContent(Encoding.UTF8.GetBytes("extracted"));
multipartContent.Add(byteArrayOption2, "output");

The "output" field specifies the output type. The value "extracted" indicates that the data should be extracted from the PDF.

var response = await httpClient.SendAsync(request);

This sends the request to the pdfRest API and awaits the response.

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

Finally, the response from the API is read as a string, which contains the extracted form data in the specified format.

API Reference, Labs, Github, and More

In this tutorial, we've covered how to use C# to send an API call to the pdfRest Export Form Data endpoint. By following the steps outlined, you can extract data from PDF forms and use it in your applications. To explore all the pdfRest API Tools, visit the API Lab at https://pdfrest.com/apilab/, and for further documentation, refer to the API Reference at https://pdfrest.com/documentation/.

Note: This is an example of a multipart API call. Code samples using JSON payloads can be found at https://github.com/datalogics/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