How to Export PDF Form Data with .NET C#
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 formats appropriate for the PDF form type, including XML, FDF, XFDF, XFD, or XDP. 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
Breaking Down the C# Code
The sample uses the framework HttpClient APIs, so no separate HTTP package is required. HttpClient is assigned the pdfRest base URL, and the request targets exported-form-data.
using (var httpClient = new HttpClient { BaseAddress = new Uri("https://api.pdfrest.com") })
using (var request = new HttpRequestMessage(HttpMethod.Post, "exported-form-data"))
The relative request path combines with the base address to call https://api.pdfrest.com/exported-form-data. In a long-running application, reuse a managed HttpClient rather than creating one for every request.
request.Headers.TryAddWithoutValidation(
"Api-Key",
"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
);
request.Headers.Accept.Add(new("application/json"));
var multipartContent = new MultipartFormDataContent();
The API key authenticates the request, and the Accept header requests the standard JSON resource response. MultipartFormDataContent generates the multipart boundary used for the file and text fields.
var byteArray = File.ReadAllBytes("/path/to/file");
var byteAryContent = new ByteArrayContent(byteArray);
multipartContent.Add(byteAryContent, "file", "file_name.pdf");
byteAryContent.Headers.TryAddWithoutValidation("Content-Type", "application/pdf");
The PDF becomes the file multipart part. Supplying a filename and application/pdf content type gives the service the metadata it needs to validate the uploaded document.
var formatContent = new ByteArrayContent(Encoding.UTF8.GetBytes("xml"));
multipartContent.Add(formatContent, "data_format");
var outputContent = new ByteArrayContent(Encoding.UTF8.GetBytes("extracted"));
multipartContent.Add(outputContent, "output");
data_format must match the PDF form type: AcroForms support fdf, xfdf, and xml; XFA forms support xdp, xfd, and xml. The output value is a filename without an extension, not an output-type selector.
request.Content = multipartContent; var response = await httpClient.SendAsync(request); var apiResult = await response.Content.ReadAsStringAsync(); Console.WriteLine(apiResult);
The response body contains generated-resource JSON when the export succeeds or an API error when validation fails. Production code should check response.IsSuccessStatusCode before deserializing the success shape so server validation messages remain visible during integration.
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://docs.pdfrest.com/pdfrest-api-toolkit-cloud/api-reference-guide/.
Note: This is an example of a multipart API call. Code samples using JSON payloads can be found at https://github.com/pdfrest/pdfrest-api-samples/blob/main/DotNET/Endpoint%20Examples/JSON%20Payload/exported-form-data.cs.