How to Flatten PDFs in IronPDF C#
IronPDF flattens PDF documents in C# with the Flatten() method, converting interactive form fields into static content so the document can no longer be modified.
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 flatten the PDF file. This applies when working with PDF forms in business applications, legal documents, or any scenario requiring a permanent record.
Quickstart: Flatten Your PDF in a Few LinesFlatten PDF documents using IronPDF to remove all interactivity and create permanent, non-editable content. This C# snippet loads an existing PDF, removes all fillable widgets, and saves the secured document.
-
1Install IronPDF with NuGet Package Manager
-
2Copy and run this code snippet.
var pdf = IronPdf.PdfDocument.FromFile("input.pdf"); pdf.Flatten(); pdf.SaveAs("flattened.pdf");C# -
3Deploy to test on your live environment
Start using IronPDF in your project today with a free trial
Minimal Workflow (5 steps)
- Install IronPDF from NuGet Package Manager
- Load existing PDF or create new from HTML
- Call the
Flattenmethod - Save the flattened PDF document
- Verify form fields are removed
How Do I Flatten a PDF Document in C#?
Once IronPDF is installed, you can flatten PDF files with a single method call. The process works on existing PDF documents and on PDFs you render from HTML files before flattening.
The code example below uses the PdfDocument class to load an existing PDF. For dynamic PDF generation, render the form with the ChromePdfRenderer class 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 non-editable.
Input
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");Imports IronPdf
' Select the desired PDF File
Private pdf As PdfDocument = PdfDocument.FromFile("before.pdf")
' Flatten the pdf
pdf.Flatten()
' Save as a new file
pdf.SaveAs("after_flatten.pdf")Output
For complex scenarios, flatten specific pages or set form data before flattening. Pass a list of zero-based page indexes to flatten only those pages:
using IronPdf;
// Load a PDF with fillable forms
PdfDocument pdf = PdfDocument.FromFile("form-document.pdf");
// Optionally, pre-fill form fields by name before flattening
pdf.Form.FindFormField("name").Value = "John Doe";
pdf.Form.FindFormField("email").Value = "john@example.com";
// Flatten only pages 1-3, passing their zero-based indexes
pdf.Flatten(new[] { 0, 1, 2 });
// Save the result
pdf.SaveAs("flattened-form.pdf");
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 the flatten method runs, the document becomes non-editable. This code works in any .NET project, including Blazor apps.
Flatten method.To verify successful flattening, check the form field count:
using IronPdf;
// Load the flattened PDF
PdfDocument flattenedPdf = PdfDocument.FromFile("flattened.pdf");
// FormFieldCollection exposes Count directly on the Form property
if (flattenedPdf.Form.Count == 0)
{
Console.WriteLine("PDF has been successfully flattened - no interactive fields remain.");
}
else
{
Console.WriteLine($"Warning: {flattenedPdf.Form.Count} form fields still exist.");
}
What Happens to Form Fields After Flattening?
When you flatten a PDF document, every interactive form element becomes part of the page content. Form fields turn into static text and graphics:
- 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. Flattening keeps text and vector content selectable, unlike rasterizing pages to images, which converts each page into a flat picture. For documents requiring both security and editability, use PDF permissions and passwords instead of flattening.
When Should I Flatten My PDF Documents?
Common cases for flattening:
- Legal Document Archival: Flatten contracts and agreements after signing to prevent content alteration and maintain legal integrity.
- Report Distribution: Flatten financial reports and data sheets with calculated fields before distribution to prevent tampering.
- Form Submission Processing: Create permanent records by flattening PDFs after users complete online forms.
- Printing Optimization: Flattened PDFs print more reliably since printers don't process interactive elements.
- File Size Reduction: Flattening can reduce file size by removing form field data structures, which pairs well with 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}");
}
}
}
}Imports IronPdf
Imports System.IO
Public Class BatchPdfFlattener
Public Shared Sub FlattenAllPdfsInDirectory(sourceDir As String, outputDir As String)
' Ensure output directory exists
Directory.CreateDirectory(outputDir)
' Get all PDF files in source directory
Dim pdfFiles As String() = Directory.GetFiles(sourceDir, "*.pdf")
For Each pdfFile As String In pdfFiles
Try
' Load the PDF
Dim pdf As PdfDocument = PdfDocument.FromFile(pdfFile)
' Flatten the document
pdf.Flatten()
' Save to output directory with "_flattened" suffix
Dim fileName As String = Path.GetFileNameWithoutExtension(pdfFile)
Dim outputPath As String = Path.Combine(outputDir, $"{fileName}_flattened.pdf")
pdf.SaveAs(outputPath)
Console.WriteLine($"Flattened: {fileName}")
Catch ex As Exception
Console.WriteLine($"Error processing {pdfFile}: {ex.Message}")
End Try
Next
End Sub
End ClassConclusion
Flattening converts a PDF's interactive form fields into static page content with a single Flatten() call, locking the document against further edits. Keep an unflattened copy if you might need to change the data later, since the operation cannot be undone.
If you need to populate fields first, set the form values before flattening; to assemble locked documents, merge or split them afterward.
Library Quick Access
Read More Documentation
Read the Documentation for more on how to flatten PDFs, edit and manipulate them, and more.
Visit IronPDF DocumentationReady to see what else you can do? Check out our tutorial page here: Additional Features
Frequently Asked Questions
What does it mean to flatten a PDF in C#?
Flattening a PDF with IronPDF involves converting interactive form fields like text boxes, checkboxes, and radio buttons into static content. This prevents further modifications to the document.
How can I flatten a PDF document using IronPDF?
To flatten a PDF document using IronPDF, you need to load the document with `PdfDocument.FromFile()`, call the `Flatten()` method, and then save the flattened version with `SaveAs()`.
Can I flatten specific pages of a PDF rather than the entire document?
Yes, you can flatten specific pages by passing an array of zero-based page indices to the `Flatten()` method in IronPDF.
What happens to form fields in a PDF after flattening?
After flattening a PDF with IronPDF, all form fields become part of the static page content. Text fields turn into selectable text, and graphical elements like checkboxes become static images.
Is it possible to verify if a PDF is successfully flattened?
Yes, you can verify that a PDF is flattened by checking the form field count. After flattening with IronPDF, the form fields should count as zero.
Are there benefits to flattening PDFs in certain scenarios?
Flattening PDFs is beneficial for legal and archival purposes, report distribution to prevent tampering, optimizing printing reliability, and reducing file size by removing interactive elements.
How do I process multiple PDFs for flattening in IronPDF?
For batch processing in IronPDF, use a loop to load each PDF file, apply the `Flatten()` method, and save each document using `SaveAs()` with a unique filename.
Is the flattening process reversible in IronPDF?
No, the flattening process is irreversible. It permanently converts interactive elements into non-editable content, so it's crucial to keep an original unflattened copy if future edits are needed.
Do flattened PDFs still retain selectable text and vector graphics?
Yes, while form fields are converted to static content, IronPDF retains selectable text and vector graphics within the document.
Can interactive elements like digital signatures be preserved upon flattening?
Digital signatures are preserved visually after flattening in IronPDF, but cryptographic validation is lost, meaning they remain visible yet are no longer valid signatures.

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.
