How to Flatten PDF Images in C# with IronPDF

Flatten PDFs in C#

IronPDF flattens PDF documents in C# with a single line of code, converting interactive form fields into static content to prevent further modifications and ensure document integrity.

PDF documents often include interactive forms with fillable widgets such as radio buttons, checkboxes, text boxes, and lists. To make these documents non-editable for security or archival purposes, you need to flatten the PDF file. IronPDF provides this functionality with just one line of code. This capability is essential when working with PDF forms in business applications, legal documents, or any scenario requiring permanent document preservation.

Fillable PDF form banner showing Adobe PDF logo and two sample interactive form documents with input fields
Pencil with prohibition symbol indicating editing restrictions or read-only access
Adobe PDF file icon with red header and curved logo

Quickstart: Flatten Your PDF in One Line

Flatten PDF documents using IronPDF to remove all interactivity and create permanent, uneditable content. This C# one-liner loads an existing PDF, removes all fillable widgets, and saves the secured document.

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronPDF with NuGet Package Manager

    PM > Install-Package IronPdf

  2. Copy and run this code snippet.

    IronPdf.PdfDocument.FromFile("input.pdf").Flatten().SaveAs("flattened.pdf");
  3. Deploy to test on your live environment

    Start using IronPDF in your project today with a free trial
    arrow pointer

How Do I Flatten a PDF Document in C#?

Once IronPDF is installed, you can flatten PDF files with one line of code. The process works with PDFs created from HTML files, HTML strings, or existing PDF documents.

The code example below uses the PdfDocument class to load an existing PDF. For dynamic PDF generation, use the ChromePdfRenderer class. IronPDF's Chrome rendering engine ensures accurate rendering of complex forms before flattening.

To flatten a PDF file, call the Flatten method. This removes all interactive widgets including radio buttons, checkboxes, and text fields, making the document completely uneditable.

:path=/static-assets/pdf/content-code-examples/how-to/pdf-image-flatten-csharp-flatten-pdf.cs
using IronPdf;

// Select the desired PDF File
PdfDocument pdf = PdfDocument.FromFile("before.pdf");

// Flatten the pdf
pdf.Flatten();

// Save as a new file
pdf.SaveAs("after_flatten.pdf");
using IronPdf;

// Select the desired PDF File
PdfDocument pdf = PdfDocument.FromFile("before.pdf");

// Flatten the pdf
pdf.Flatten();

// Save as a new file
pdf.SaveAs("after_flatten.pdf");
using IronPdf;

// Select the desired PDF File
PdfDocument pdf = PdfDocument.FromFile("before.pdf");

// Flatten the pdf
pdf.Flatten();

// Save as a new file
pdf.SaveAs("after_flatten.pdf");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

For complex scenarios, you can flatten specific pages or manipulate form data before flattening:

using IronPdf;

// Load a PDF with fillable forms
PdfDocument pdf = PdfDocument.FromFile("form-document.pdf");

// Optionally, pre-fill form fields before flattening
pdf.Form.Fields[0].Value = "John Doe";
pdf.Form.Fields[1].Value = "john@example.com";

// Flatten only specific pages (pages 1-3)
pdf.FlattenPagesRange(0, 2);

// Or flatten the entire document
pdf.Flatten();

// Save the result
pdf.SaveAs("flattened-form.pdf");
using IronPdf;

// Load a PDF with fillable forms
PdfDocument pdf = PdfDocument.FromFile("form-document.pdf");

// Optionally, pre-fill form fields before flattening
pdf.Form.Fields[0].Value = "John Doe";
pdf.Form.Fields[1].Value = "john@example.com";

// Flatten only specific pages (pages 1-3)
pdf.FlattenPagesRange(0, 2);

// Or flatten the entire document
pdf.Flatten();

// Save the result
pdf.SaveAs("flattened-form.pdf");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

How Can I Verify the PDF Is Flattened?

The output below shows the before and after states. The first PDF contains editable form fields. After applying IronPDF's flatten method, the document becomes completely non-editable. This code works in any .NET project, including ASP.NET applications and Blazor servers.

Flattened PDF

Please noteForms will not be detectable after using the Flatten method.

To verify successful flattening, check the form field count:

using IronPdf;

// Load the flattened PDF
PdfDocument flattenedPdf = PdfDocument.FromFile("flattened.pdf");

// Check if any form fields exist
if (flattenedPdf.Form.Fields.Count == 0)
{
    Console.WriteLine("PDF has been successfully flattened - no interactive fields remain.");
}
else
{
    Console.WriteLine($"Warning: {flattenedPdf.Form.Fields.Count} form fields still exist.");
}
using IronPdf;

// Load the flattened PDF
PdfDocument flattenedPdf = PdfDocument.FromFile("flattened.pdf");

// Check if any form fields exist
if (flattenedPdf.Form.Fields.Count == 0)
{
    Console.WriteLine("PDF has been successfully flattened - no interactive fields remain.");
}
else
{
    Console.WriteLine($"Warning: {flattenedPdf.Form.Fields.Count} form fields still exist.");
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

What Happens to Form Fields After Flattening?

When you flatten a PDF document, all interactive form elements undergo permanent transformation. Form fields convert into static page content, becoming part of the document's visual layer:

  • Text fields become regular text on the page
  • Checkboxes and radio buttons become static images showing their selected state
  • Dropdown menus display only the selected value as plain text
  • Digital signatures are preserved visually but lose cryptographic validation

This process is irreversible. Keep a copy of the original interactive PDF if you need future editing capabilities. For documents requiring both security and editability, use PDF permissions and passwords instead of flattening.

When Should I Flatten My PDF Documents?

PDF flattening is essential in these business scenarios:

  1. Legal Document Archival: Flatten contracts and agreements after signing to prevent content alteration and maintain legal integrity.

  2. Report Distribution: Flatten financial reports and data sheets with calculated fields before distribution to prevent tampering.

  3. Form Submission Processing: Create permanent records by flattening PDFs after users complete online forms.

  4. Printing Optimization: Flattened PDFs print more reliably since printers don't process interactive elements.

  5. File Size Reduction: Flattening can reduce file size by removing form field data structures when using PDF compression.

Here's a batch processing example for archiving multiple completed forms:

using IronPdf;
using System.IO;

public class BatchPdfFlattener
{
    public static void FlattenAllPdfsInDirectory(string sourceDir, string outputDir)
    {
        // Ensure output directory exists
        Directory.CreateDirectory(outputDir);

        // Get all PDF files in source directory
        string[] pdfFiles = Directory.GetFiles(sourceDir, "*.pdf");

        foreach (string pdfFile in pdfFiles)
        {
            try
            {
                // Load the PDF
                PdfDocument pdf = PdfDocument.FromFile(pdfFile);

                // Flatten the document
                pdf.Flatten();

                // Save to output directory with "_flattened" suffix
                string fileName = Path.GetFileNameWithoutExtension(pdfFile);
                string outputPath = Path.Combine(outputDir, $"{fileName}_flattened.pdf");
                pdf.SaveAs(outputPath);

                Console.WriteLine($"Flattened: {fileName}");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error processing {pdfFile}: {ex.Message}");
            }
        }
    }
}
using IronPdf;
using System.IO;

public class BatchPdfFlattener
{
    public static void FlattenAllPdfsInDirectory(string sourceDir, string outputDir)
    {
        // Ensure output directory exists
        Directory.CreateDirectory(outputDir);

        // Get all PDF files in source directory
        string[] pdfFiles = Directory.GetFiles(sourceDir, "*.pdf");

        foreach (string pdfFile in pdfFiles)
        {
            try
            {
                // Load the PDF
                PdfDocument pdf = PdfDocument.FromFile(pdfFile);

                // Flatten the document
                pdf.Flatten();

                // Save to output directory with "_flattened" suffix
                string fileName = Path.GetFileNameWithoutExtension(pdfFile);
                string outputPath = Path.Combine(outputDir, $"{fileName}_flattened.pdf");
                pdf.SaveAs(outputPath);

                Console.WriteLine($"Flattened: {fileName}");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error processing {pdfFile}: {ex.Message}");
            }
        }
    }
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

For advanced PDF manipulation techniques, including merging or splitting PDFs after flattening, see IronPDF's comprehensive documentation.


Library Quick Access

Documentation

Read More Documentation

Read the Documentation for more on how to flatten PDFs, edit and manipulate them, and more.

Visit IronPDF Documentation

Ready to see what else you can do? Check out our tutorial page here: Additional Features

Frequently Asked Questions

What does flattening a PDF mean?

Flattening a PDF converts all interactive form fields like checkboxes, text boxes, and radio buttons into static, non-editable content. IronPDF provides this functionality to ensure document integrity and prevent further modifications.

How do I flatten a PDF in C#?

With IronPDF, you can flatten a PDF in one line of code: IronPdf.PdfDocument.FromFile("input.pdf").Flatten().SaveAs("flattened.pdf"). This loads the PDF, removes all interactive elements, and saves the secured document.

Can I flatten specific pages instead of the entire document?

Yes, IronPDF allows you to flatten specific pages using the FlattenPagesRange method. For example, pdf.FlattenPagesRange(0, 2) will flatten only pages 1-3 of your document while leaving other pages interactive.

What types of form fields can be flattened?

IronPDF can flatten all interactive widgets including radio buttons, checkboxes, text fields, dropdown lists, and any other fillable form elements, converting them to permanent static content.

Can I fill form fields before flattening the PDF?

Yes, IronPDF allows you to pre-fill form fields before flattening. You can set values like pdf.Form.Fields[0].Value = "John Doe" before calling the Flatten method to create a completed, non-editable document.

What rendering engine does the PDF flattening process use?

IronPDF uses a Chrome rendering engine to ensure accurate rendering of complex forms before flattening, maintaining the visual integrity of your documents throughout the process.

Why would I need to flatten a PDF document?

Flattening PDFs with IronPDF is essential for security, archival purposes, legal documents, or any scenario requiring permanent document preservation where you need to prevent further modifications to form data.

Curtis Chau
Technical Writer

Curtis Chau holds a Bachelor’s degree in Computer Science (Carleton University) and specializes in front-end development with expertise in Node.js, TypeScript, JavaScript, and React. Passionate about crafting intuitive and aesthetically pleasing user interfaces, Curtis enjoys working with modern frameworks and creating well-structured, visually appealing manuals.

...

Read More
Ready to Get Started?
Nuget Downloads 16,585,857 | Version: 2025.12 just released