How to Flatten Annotations in PDF Files in .NET with C#

Learn how to use .NET to flatten annotations in PDF files by calling Flatten Annotations API Tool by pdfRest.
Share this page

Why Use Flatten Annotations with C#?

The pdfRest Flatten Annotations API Tool is designed to take annotations in a PDF and make them a permanent part of the document. This process is known as "flattening." Flattening annotations is useful in scenarios where you want to prevent further modifications to the annotations or ensure that they are displayed correctly across all PDF viewers. For instance, if you're sending a contract with comments to a client, you might want to flatten these comments so that they can't be altered before signing.

This tutorial will guide you through the process of sending an API call to Flatten Annotations using C#. By the end of this tutorial, you will understand how to integrate this functionality into your C# applications.

Flatten Annotations 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-annotations-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_name.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: pdfRest API C# Sample Code

Breaking Down the Code

The provided code snippet demonstrates how to make a multipart/form-data POST request to the pdfRest Flatten Annotations API endpoint using C#.

  • HttpClient is initialized with the base address of the pdfRest API.
  • An HttpRequestMessage is created for the POST request to the "flattened-annotations-pdf" endpoint.
  • The API key is added to the request headers. Replace "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" with your actual API key.
  • The request is set to accept JSON responses.
  • A MultipartFormDataContent is created to hold the file content.
  • The PDF file is read into a byte array and added to the multipart content with the name "file" and the filename "file_name.pdf".
  • The Content-Type header for the file part is set to "application/pdf".
  • The multipart content is assigned to the request content.
  • The request is sent asynchronously, and the response is read as a string.
  • Finally, the API response is printed to the console.

Beyond the Tutorial

By following the tutorial above, you have learned how to make a call to the pdfRest Flatten Annotations API using C#. This allows you to integrate PDF annotation flattening into your applications seamlessly. You can now ensure that annotations in your PDFs are displayed consistently and cannot be altered by the end users.

To explore and demo all of the pdfRest API Tools, visit the API Lab at https://pdfrest.com/apilab/. For more detailed information about the API, refer to the API Reference documentation 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/tree/main/DotNET/Endpoint%20Examples/JSON%20Payload/flattened-annotations-pdf.cs.

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