How to Flatten PDF Forms in .NET with C#

Learn how to use pdfRest Flatten Forms API Tool with C# to flatten PDF forms
Share this page

Why Use Flatten Forms with C#?

The pdfRest Flatten Forms API Tool is designed to help users convert interactive PDF forms into static PDF documents by flattening form fields. This means that the form fields are no longer editable and become part of the PDF content, preserving the data entered into the form. This tutorial will guide you through the process of sending an API call to the Flatten Forms endpoint using C# to achieve this result.

Flattening forms can be particularly useful in scenarios where you want to finalize the content of a form after it has been filled out, such as when submitting official documents or archiving filled forms for record-keeping. For instance, a business might use this tool to flatten employment application forms after they have been completed by applicants, ensuring that the information provided cannot be altered and is ready for review and storage.

Flatten Forms 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, "flattened-forms-pdf"))
    {
        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");

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

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

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

Reference: pdf-rest-api-samples on GitHub

Breaking Down the Code

The code above demonstrates how to make a multipart API call to the pdfRest Flatten Forms API using C#. Let's break down the key parts of the code:

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

This line initializes a new instance of the HttpClient class and sets the BaseAddress 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, which is required for authentication with the pdfRest API.

var multipartContent = new MultipartFormDataContent();

A new instance of MultipartFormDataContent is created to hold the file and any other parts of the multipart request.

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

These lines read the PDF file into a byte array, create a ByteArrayContent with this array, and add it to the multipart content with the appropriate content type header for PDF files.

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

Finally, the API call is sent asynchronously, and the response is read into a string which is then output to the console.

Beyond the Tutorial

By following the steps outlined above, you have learned how to programmatically flatten PDF forms using C# and the pdfRest API. This capability can be integrated into various applications to automate the process of converting interactive forms into non-editable documents.

We encourage you to demo all of the pdfRest API Tools in the API Lab and refer to the API Reference Guide for more detailed information on other API endpoints and their usage.

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