Flattening a PDF converts interactive elements—forms, annotations, layers—into static content. Combined with rasterization, you get read-only image-based PDFs that can't be edited or copied.
using IronPdf;
// Install via NuGet: Install-Package IronPdf
var pdf = PdfDocument.FromFile("form.pdf");
// Flatten forms and annotations
pdf.FlattenPdf();
// Rasterize to images
pdf.RasterizeToImageFiles("page-*.png", 300);
The form fields become static images. The text becomes pixels. No more editing.
What Does Flattening Do?
Flattening merges layers into a single visual:
Before flattening:
- Form fields are editable
- Annotations can be moved/deleted
- Layers can be toggled
- Text can be selected and copied
After flattening:
- Form data becomes static text/image
- Annotations merge into the page
- Layers collapse into one
- Content appears identical but is locked
It's like taking a screenshot of the PDF and making that the new document.
Why Flatten PDFs?
Common scenarios:
- Archival - Preserve exact appearance for legal/compliance
- Security - Prevent editing of signed documents
- Distribution - Share filled forms without allowing changes
- Printing - Ensure what you see is what prints
- Compatibility - Some viewers handle flat PDFs better
The trade-off: flattened PDFs lose interactivity and text selectability (when rasterized).
How Do I Flatten Form Fields?
Lock form data while preserving vector content:
using IronPdf;
// Install via NuGet: Install-Package IronPdf
var pdf = PdfDocument.FromFile("application-form.pdf");
// Check current form field count
var formsBefore = pdf.Form.GetFieldNames();
Console.WriteLine($"Fields before: {formsBefore.Count}");
// Flatten - fields become static
pdf.FlattenPdf();
// Fields are gone (merged into page)
var formsAfter = pdf.Form.GetFieldNames();
Console.WriteLine($"Fields after: {formsAfter.Count}"); // 0
pdf.SaveAs("application-form-final.pdf");
The form looks identical, but fields are now part of the page image.
How Do I Flatten Annotations?
Merge comments, highlights, and stamps into the page:
using IronPdf;
// Install via NuGet: Install-Package IronPdf
var pdf = PdfDocument.FromFile("reviewed-document.pdf");
// All annotations (comments, highlights, stamps) become permanent
pdf.FlattenPdf();
pdf.SaveAs("reviewed-document-final.pdf");
After flattening, annotations can't be moved, edited, or deleted.
How Do I Convert to Image-Based PDF?
Full rasterization—convert each page to an image, then rebuild the PDF:
using IronPdf;
// Install via NuGet: Install-Package IronPdf
public byte[] RasterizeToPdf(string inputPath, int dpi = 300)
{
var original = PdfDocument.FromFile(inputPath);
// Flatten first (ensures forms/annotations are included)
original.FlattenPdf();
// Get page images
var images = original.ToBitmap(dpi);
// Create new PDF from images
var renderer = new [ChromePdfRenderer](https://ironpdf.com/how-to/html-to-pdf-page-breaks/)();
var pages = new List<PdfDocument>();
foreach (var image in images)
{
using var stream = new MemoryStream();
image.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
stream.Position = 0;
var html = $@"
<html>
<body style='margin:0;'>
<img src='data:image/png;base64,{Convert.ToBase64String(stream.ToArray())}'
style='width:100%; height:100%;'>
</body>
</html>";
pages.Add(renderer.RenderHtmlAsPdf(html));
}
var merged = PdfDocument.Merge(pages);
// Cleanup
foreach (var page in pages) page.Dispose();
original.Dispose();
return merged.BinaryData;
}
The resulting PDF looks identical but is pure image—no selectable text.
How Do I Preserve Text Selectability?
Flatten forms/annotations but keep text as text:
using IronPdf;
// Install via NuGet: Install-Package IronPdf
var pdf = PdfDocument.FromFile("document.pdf");
// Flatten only interactive elements
pdf.FlattenPdf();
// Text remains selectable/searchable
// But forms and annotations are locked
pdf.SaveAs("flattened-with-text.pdf");
This is the best of both worlds—locked appearance, functional text.
How Do I Flatten Specific Pages?
Process only certain pages:
using IronPdf;
// Install via NuGet: Install-Package IronPdf
var pdf = PdfDocument.FromFile("multi-section.pdf");
// Extract pages to flatten (e.g., signature pages)
var signaturePages = new[] { 4, 9, 14 }; // Pages 5, 10, 15 (0-indexed)
foreach (var pageIndex in signaturePages)
{
// Extract single page
var singlePage = pdf.CopyPage(pageIndex);
// Rasterize it
var image = singlePage.ToBitmap(300)[0];
// Convert back to PDF page
using var stream = new MemoryStream();
image.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
// Replace original page with flattened version
// (Implementation depends on your specific needs)
}
Selective flattening keeps most content editable while locking sensitive pages.
How Do I Set Image Quality?
Balance file size against clarity:
using IronPdf;
// Install via NuGet: Install-Package IronPdf
var pdf = PdfDocument.FromFile("document.pdf");
pdf.FlattenPdf();
// Low quality - smaller files
pdf.RasterizeToImageFiles("low-*.jpg", 96); // ~1MB per page
// Medium quality - balanced
pdf.RasterizeToImageFiles("medium-*.jpg", 150); // ~2-3MB per page
// High quality - print-ready
pdf.RasterizeToImageFiles("high-*.png", 300); // ~5-10MB per page
// Ultra quality - archival
pdf.RasterizeToImageFiles("ultra-*.png", 600); // ~20MB+ per page
DPI guidelines:
- 96: Web viewing
- 150: On-screen reading
- 300: Standard print quality
- 600: High-end printing
How Do I Compress Flattened PDFs?
Reduce file size after rasterization:
using IronPdf;
using System.Drawing;
using System.Drawing.Imaging;
// Install via NuGet: Install-Package IronPdf
var pdf = PdfDocument.FromFile("document.pdf");
pdf.FlattenPdf();
var images = pdf.ToBitmap(200); // Lower DPI
var compressedPages = new List<PdfDocument>();
foreach (var image in images)
{
using var stream = new MemoryStream();
// Compress as JPEG
var encoder = ImageCodecInfo.GetImageEncoders()
.First(c => c.FormatID == ImageFormat.Jpeg.Guid);
var encoderParams = new EncoderParameters(1);
encoderParams.Param[0] = new EncoderParameter(
System.Drawing.Imaging.Encoder.Quality, 70L); // 70% quality
image.Save(stream, encoder, encoderParams);
// Create PDF page from compressed image
var html = $@"<img src='data:image/jpeg;base64,{Convert.ToBase64String(stream.ToArray())}'>";
var renderer = new ChromePdfRenderer();
compressedPages.Add(renderer.RenderHtmlAsPdf(html));
}
var result = PdfDocument.Merge(compressedPages);
result.SaveAs("compressed-flat.pdf");
JPEG at 70% quality typically reduces size by 50-70%.
What About OCR After Flattening?
Make rasterized PDFs searchable again:
using IronPdf;
// Install via NuGet: Install-Package IronPdf
// Note: OCR requires additional IronOCR package
var pdf = PdfDocument.FromFile("scanned-document.pdf");
pdf.FlattenPdf();
// After rasterization, you can run OCR to add a text layer
// This makes the document searchable while keeping image appearance
// Using IronOCR (separate package):
// var ocr = new IronTesseract();
// foreach (var page in pdf.Pages) {
// var result = ocr.Read(page.ToBitmap()[0]);
// // Add text layer to page
// }
OCR adds invisible text behind images for search functionality.
Quick Reference
| Operation | Text Selectable | Forms Editable | Appearance |
|---|---|---|---|
| Original | Yes | Yes | Vector |
| Flattened | Yes | No | Vector |
| Rasterized | No | No | Image |
| Rasterized + OCR | Yes (hidden) | No | Image |
When to Use Each Approach
Flatten only:
- Lock form submissions
- Prevent annotation changes
- Keep text searchable
Rasterize completely:
- Prevent all copying
- Protect sensitive content
- Ensure exact appearance
- Maximum security
Rasterize + OCR:
- Security with searchability
- Archival with accessibility
- Best of both worlds
Flattening gives you control over what stays editable in your PDFs.
For the complete API reference, see the IronPDF flattening documentation.
Written by Jacob Mellor, CTO at Iron Software. Jacob created IronPDF and leads a team of 50+ engineers building .NET document processing libraries.
Top comments (0)