How to Import Data to PDF Forms in .NET with C#

Learn how to use pdfRest Import Form Data API Tool with .NET to import data into PDF forms.
Share this page

Why Use Import Form Data with C#?

The pdfRest Import Form Data API Tool is designed to streamline the process of importing data into PDF forms. It allows users to programmatically fill out PDF forms with data from various sources, such as databases or user input.

This tutorial will guide you through the steps of sending an API call to Import Form Data using C#.

Consider a scenario where a company needs to generate hundreds of personalized contracts. Manually entering data into each form is time-consuming and prone to errors. By using the Import Form Data API, the company can automate the process, ensuring that each PDF form is accurately filled out with the correct data from their database, saving time and reducing the potential for mistakes.

Import Form Data 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-imported-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.pdf");
        var byteAryContent = new ByteArrayContent(byteArray);
        multipartContent.Add(byteAryContent, "file", "file_name.pdf");
        byteAryContent.Headers.TryAddWithoutValidation("Content-Type", "application/pdf");

        var byteArray2 = File.ReadAllBytes("/path/to/datafile.xml");
        var byteAryContent2 = new ByteArrayContent(byteArray2);
        multipartContent.Add(byteAryContent2, "data_file", "datafile_name.xml");
        byteAryContent2.Headers.TryAddWithoutValidation("Content-Type", "application/xml");

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

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

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

Source of the provided code: GitHub

Breaking Down the Code

This code snippet demonstrates how to use C# to make a multipart/form-data POST request to the pdfRest API endpoint for importing form data into a PDF.

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

This line initializes a new instance of the HttpClient class and sets the base address for the API calls to the pdfRest API.

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

The Api-Key header is added to the request. This key is required for authentication and should be replaced with your actual pdfRest API key.

var multipartContent = new MultipartFormDataContent();

A new instance of MultipartFormDataContent is created, which will hold the form data to be sent in the request.

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");

This snippet reads the PDF file as a byte array, wraps it in a ByteArrayContent object, and adds it to the multipart content with the name "file". Replace "/path/to/file.pdf" with the actual file path and "file_name.pdf" with the actual file name. The "Content-Type" header is used to indicate this is a PDF file.

var byteArray2 = File.ReadAllBytes("/path/to/datafile.xml");
var byteAryContent2 = new ByteArrayContent(byteArray2);
multipartContent.Add(byteAryContent2, "data_file", "datafile_name.xml");
byteAryContent2.Headers.TryAddWithoutValidation("Content-Type", "application/xml");

Similarly, this snippet reads the data file (typically XML or FDF containing form data; in this case XML) and adds it to the multipart content with the name "data_filename.xml". The "Content-Type" header is used to indicate this is an XML file.

var response = await httpClient.SendAsync(request);

The request is sent asynchronously, and the response is captured in the response variable.

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

The response content is read as a string, which contains the result of the API call.

Beyond the Tutorial

By following the steps in this tutorial, you have learned how to use C# to make a multipart API call to the pdfRest Import Form Data endpoint. This enables you to programmatically import data into PDF forms, which can be a powerful tool for automating document workflows.

To explore and demo all of the pdfRest API Tools, visit the API Lab. For more detailed information about 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 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