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.



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.
Get started making PDFs with NuGet now:
Install IronPDF with NuGet Package Manager
Copy and run this code snippet.
IronPdf.PdfDocument.FromFile("input.pdf").Flatten().SaveAs("flattened.pdf");Deploy to test on your live environment
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 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.comFor 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.comHow 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.
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.comWhat 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:
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 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.comFor advanced PDF manipulation techniques, including merging or splitting PDFs after flattening, see IronPDF's comprehensive documentation.
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 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.







