Flatten PDF form fields in Blazor SfPdfViewer
17 Jul 20263 minutes to read
Overview
Flattening PDF forms converts interactive fields such as textboxes, dropdowns, checkboxes, signatures, etc. into non‑editable page content. Use this when you want to protect filled data, finalize a document, or prepare it for secure sharing.
Prerequisites
- Blazor SfPdfViewer installed and configured
- Basic viewer setup completed. For more information, see getting started guide
Flatten forms before download
- Add a reference to the
SfPdfViewer2component using@refand set itsDocumentPathso the viewer loads a PDF. - Retrieve the current document from the viewer using
GetDocumentAsync()as a byte array. - Load the document into a
PdfLoadedDocumentfrom the Syncfusion PDF Library. - Set
Form.FlattentotrueandAnnotations.Flattentotrueon each page to convert the form fields and annotations to non-editable content. - Save the flattened PDF to a byte array and reload it into the viewer using
LoadAsync(). - Download the flattened PDF using
DownloadAsync().
Complete example
@page "/flatten-form-fields"
@using Syncfusion.Blazor
@using Syncfusion.Blazor.SfPdfViewer
@using Syncfusion.Blazor.Buttons
@using Syncfusion.Pdf
@using Syncfusion.Pdf.Parsing
@using System.IO
<SfButton OnClick="FlattenDownload">Flatten</SfButton>
<SfPdfViewer2 Height="600px"
Width="100%"
DocumentPath="@DocumentPath"
@ref="Viewer">
</SfPdfViewer2>
@code {
private SfPdfViewer2? Viewer;
private string DocumentPath { get; set; } = "wwwroot/data/form-filling-document.pdf";
private async Task FlattenDownload()
{
if (Viewer is null) return;
// Get current document from viewer as byte array
byte[] bytes = await Viewer.GetDocumentAsync();
// Load into PdfLoadedDocument for flattening
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(bytes);
// Flatten form fields
if (loadedDocument.Form != null)
{
loadedDocument.Form.Flatten = true;
}
// Flatten annotations on all pages
foreach (PdfLoadedPage page in loadedDocument.Pages)
{
page.Annotations.Flatten = true;
}
// Save flattened PDF to byte array
byte[] flattenedBytes;
using (MemoryStream stream = new MemoryStream())
{
loadedDocument.Save(stream);
flattenedBytes = stream.ToArray();
}
loadedDocument.Close(true);
// Reload flattened document into viewer
await Viewer.LoadAsync(flattenedBytes);
// Download the flattened PDF
await Viewer.DownloadAsync();
}
}Expected result
- The downloaded or “Save As” PDF will contain the visible appearance of filled form fields as static, non-editable content.
- Form fields will no longer be interactive or editable in common PDF readers.
NOTE
For a hands-on reference with working code examples, explore the sample projects available on GitHub.
Troubleshooting
- If the
Vieweris null, ensure@ref="Viewer"is present and the document is loaded before invokingGetDocumentAsync(). - A null reference exception can occur. Add a null check using
if (Viewer is null) return;before accessing viewer properties, and checkloadedDocument.Formbefore flattening. - Ensure the Syncfusion.Pdf NuGet package is installed for
PdfLoadedDocumentand flattening functionality. - After flattening, use
LoadAsync()to reload the flattened PDF into the viewer.