Document Automation Made Simple

Flatten PDF in C# – Lock Forms, Annotations, and Text with ConvertAPI

Learn how to flatten PDF in C# with ConvertAPI. Lock form fields, flatten annotations, and protect text while preserving quality in your PDF files.

Tomas, CEO

If you need to flatten a PDF in C#, you’re likely looking for a way to make it read-only, prevent edits, and ensure it looks the same everywhere. Flattening removes interactive elements like form fields and annotations, and can even convert text into vector shapes so it can’t be selected or copied.

In this tutorial, we’ll walk through how to flatten PDF form fields in C#, disable annotations, and protect text using the ConvertAPI PDF Flatten for C#. You’ll get a complete C# PDF flatten example and best practices for preserving quality while securing your documents.

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

Why Flatten a PDF in C#?

1. Prevent Editing of PDF Form Fields in C#

When you flatten PDF form fields in C#, you turn editable inputs into static content, preventing further modifications while keeping their original look.

2. Flatten PDF Annotations in C# for Better Security

Flattening annotations such as checkboxes, buttons, and signature fields makes them visible but non-interactive, ensuring no one can alter them.

3. Protect PDF Text from Copying in C#

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 for PDF Flattening in C#

Install ConvertAPI NuGet Package

dotnet add package ConvertApi

Configure Your API Token for PDF Flattening

Sign up at ConvertAPI and get your API token. You can find your API token by signing into your ConvertAPI Dashboard.

Store it securely as an environment variable:

setx CONVERTAPI_API_TOKEN "your-api-token"

Flatten PDF in C# – 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 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 PDF Options in C#

  • FlattenControls – Lock PDF form fields by converting them to static page content.
  • FlattenWidgets – Disable interactive annotations while keeping their appearance.
  • FlattenText – Convert text to vector shapes to make it unselectable and uncopyable.

Best Practices for PDF Flattening in C#

  • Flatten only what you need – Keep text searchable by leaving FlattenText set to false if required.
  • Store your API token securely – Never hardcode it in source code.
  • Leverage high-speed processing – ConvertAPI delivers flattening results in seconds, even for large PDF files.
  • Preserve vector quality – Flattening with ConvertAPI keeps original sharpness and layout.

Common Use Cases

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

Conclusion

With ConvertAPI, PDF flattening in C# is simple, reliable, and high-quality. You can lock form fields, flatten annotations, and protect text with just a few lines of code. Whether you’re building a secure document workflow or need to make a PDF read-only in C#, this solution gives you control without compromising visual fidelity.

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


Related converters

Ready to Streamline Your File Conversions?