Document Automation Made Simple

How to Flatten a PDF in C# (Forms, Annotations, and Text)

Flatten a filled-out PDF form in C# so nothing can be changed: lock form fields, freeze annotations and signature fields, and stop text from being copied - with a complete working example and an option reference.

Tomas, CEO

You have a PDF form that is already filled out, and you need to make sure nobody can change it: lock the form fields, freeze the annotations, and stop the text from being copied. That is exactly what flattening does - it converts interactive elements into static page content, so the document looks the same everywhere and can no longer be edited.

In this tutorial, we'll flatten a PDF in C# using the ConvertAPI PDF Flatten converter: lock filled form fields, disable annotations, and protect text from extraction, with a complete working example you can drop into your project.

Before and after flattening a PDF -  all fields locked and text protected

Why Flatten a PDF in C#?

1. Lock filled form fields

When you flatten PDF form fields in C#, editable inputs such as text boxes, checkboxes, and dropdowns become static page content. The values stay exactly as filled, but nobody can change them - the most common reason to flatten a completed form before archiving it or sending it on.

2. Freeze annotations and signature fields

Flattening widget annotations such as buttons, list boxes, and signature fields makes them visible but non-interactive, so a signed or reviewed document cannot be quietly altered afterwards.

3. Protect text from copying

By converting text into vector paths, you protect PDF text from copying or extraction, making the document fully read-only without losing visual quality.

Setting Up ConvertAPI

Install the NuGet package

dotnet add package ConvertApi

Configure your API token

Sign up at ConvertAPI and get your API token from the dashboard.

Store it securely as an environment variable:

setx CONVERTAPI_API_TOKEN "your-api-token"

Complete Working Example

using System;
using System.IO;
using System.Threading.Tasks;
using ConvertApiDotNet;

class Program
{
    static async Task Main(string[] args)
    {
        if (args.Length < 2)
        {
            Console.WriteLine("Usage: dotnet run -- <input.pdf> <output-folder>");
            return;
        }

        var inputPath  = args[0];
        var outputDir  = args[1];

        // Get API token from environment variable
        var apiToken = Environment.GetEnvironmentVariable("CONVERTAPI_API_TOKEN");

        if (string.IsNullOrWhiteSpace(apiToken))
        {
            Console.WriteLine("Set CONVERTAPI_API_TOKEN environment variable first.");
            return;
        }

        var api = new ConvertApi(apiToken);

        try
        {
            var res = await api.ConvertAsync("pdf", "flatten",
                new ConvertApiFileParam("File", inputPath),
                new ConvertApiParam("FlattenControls", "true"),
                new ConvertApiParam("FlattenWidgets", "true"),
                new ConvertApiParam("FlattenText", "true")
            );

            await res.SaveFilesAsync(outputDir);
            Console.WriteLine($"Flattened PDF saved to: {Path.GetFullPath(outputDir)}");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Error: {ex.Message}");
        }
    }
}

Quick Example - Make a PDF Read-Only in C#

var res = await new ConvertApi(Environment.GetEnvironmentVariable("CONVERTAPI_API_TOKEN"))
    .ConvertAsync("pdf", "flatten",
        new ConvertApiFileParam("File", "input.pdf"),
        new ConvertApiParam("FlattenControls", "true"),
        new ConvertApiParam("FlattenWidgets", "true"),
        new ConvertApiParam("FlattenText", "true")
    );

await res.SaveFilesAsync("output-folder");

Flatten Options

All three options default to true, so a plain pdf to flatten call with no parameters locks everything. Set an option to false to keep that layer as it is:

  • FlattenControls - lock form fields (text boxes, checkboxes, dropdowns) by converting them to static page content.
  • FlattenWidgets - disable widget annotations such as buttons, list boxes, and signature fields while keeping their appearance.
  • FlattenText - convert text to vector shapes so it cannot be selected, copied, or extracted.

Best Practices

  • Flatten only what you need - to keep text searchable and selectable, set FlattenText to false. Form fields and annotations will still be locked.
  • Flatten after the last edit - flattening is one-way, so run it as the final step of your workflow, after all fields are filled and signatures are collected.
  • Store your API token securely - never hardcode it in source code.
  • Password-protected files - pass the document password with the Password parameter and the file will be flattened just the same.

Common Use Cases

  • Finalizing contracts and agreements
  • Archiving completed forms
  • Securing signed PDF documents
  • Preventing accidental edits in workflows
  • Locking confidential PDF reports

Try It Yourself: Download the Sample Files

We filled out a shipment release form - 11 interactive fields: text boxes, a dropdown, and checkboxes - and flattened it with the exact API call from the example above. These are the actual input and output:

Frequently Asked Questions

How do I flatten an already filled-out PDF form in C#?

Exactly as in the example above - upload the filled file and call pdf to flatten. The field values are preserved as static text on the page. Only the interactivity is removed.

Can a flattened PDF be unflattened?

No. Flattening permanently converts fields and annotations into page content. Keep the original interactive version if you may need to edit the values later.

Can I keep the text selectable?

Yes - set FlattenText to false. Form fields and annotations are still flattened, but the text remains searchable and copyable.

How do I flatten many PDFs at once?

Loop over your files with the same ConvertAsync call - each conversion is independent, and requests can run in parallel with Task.WhenAll.

Does flattening change how the PDF looks?

No. Unlike rasterizing pages into images, flattening preserves the original vector quality, fonts, and layout. If you also want to compare the two approaches, see flatten or rasterize a PDF.

Conclusion

PDF flattening in C# comes down to a single API call: lock form fields, flatten annotations, and protect text with a few lines of code. Whether you are building a secure document workflow or just need to make a PDF read-only in C#, flattening gives you control without compromising visual fidelity.

Try ConvertAPI PDF Flatten for C# and start flattening PDFs in your next C# project today! Get the NuGet package from https://www.nuget.org/packages/ConvertApi


Related converters

Ready to Streamline Your File Conversions?