How to Convert PDF Forms from XFA to Acroforms in .NET with C#

Learn how to use the pdfRest XFA to Acroforms API Tool to convert PDF forms with C#
Share this page

Why Use XFA to Acroforms with C#?

The pdfRest XFA to Acroforms API Tool allows developers to convert XFA forms into Acroforms, which are more widely supported and easier to work with in various PDF readers and editors. This tutorial will show you how to send an API call to convert XFA to Acroforms using C#.

Users might need to convert XFA forms to Acroforms to ensure compatibility with a broader range of PDF viewers. For example, a business might receive XFA-based forms from clients but need to convert them to Acroforms to integrate seamlessly with their document management system, which only supports Acroforms.

XFA to Acroforms 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-with-acroforms"))
    {
        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");


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

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

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

Source: GitHub

Breaking Down the Code

Let's break down the code to understand how it works:

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

This line initializes an instance of HttpClient with the base address set to the pdfRest API endpoint.

using (var request = new HttpRequestMessage(HttpMethod.Post, "pdf-with-acroforms"))

Here, we create a new HttpRequestMessage object with the HTTP method set to POST and the endpoint specified as "pdf-with-acroforms".

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

This line adds the API key to the request headers. Replace xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx with your actual API key.

request.Headers.Accept.Add(new("application/json"));

We specify that the request expects a JSON response by adding the "application/json" media type to the Accept header.

var multipartContent = new MultipartFormDataContent();

This line initializes a new MultipartFormDataContent object to hold the multipart form data.

var byteArray = File.ReadAllBytes("/path/to/file");

We read the file to be uploaded into a byte array. Replace /path/to/file with the actual path to your PDF file.

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

Here, we create a ByteArrayContent object from the byte array and add it to the multipart content with the name "file". We also set the Content-Type header to "application/pdf".

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

We set the request content to the multipart content and send the request asynchronously.

var apiResult = await response.Content.ReadAsStringAsync();
Console.WriteLine("API response received.");
Console.WriteLine(apiResult);

Finally, we read the API response as a string and print it to the console.

Beyond the Tutorial

In this tutorial, we demonstrated how to use C# to call the pdfRest XFA to Acroforms API. This allows you to convert XFA forms to Acroforms, ensuring broader compatibility and easier handling of PDF forms.

We encourage you to explore all the pdfRest API Tools in the API Lab. For more detailed information, refer to the API Reference Guide.

Note: This example uses 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