How can I help you?
Edit Form Fields in .NET MAUI PDF Viewer (SfPdfViewer)
9 Jun 202610 minutes to read
The SfPdfViewer allows you to programmatically set, modify, and read values for all supported form field types. Use the FormFields collection to retrieve fields by name and cast them to the appropriate type before modifying.
Editing form fields programmatically
All form field edits shown below should be placed in your page’s code-behind (MainPage.xaml.cs). The document must be fully loaded before accessing FormFields — call these from the DocumentLoaded event handler or a button click after the document is open.
Editing text form fields
A text form field can be modified using the Text property. The following code snippet illustrates retrieving a text form field named “name” from the PDF Viewer.
// Call after DocumentLoaded fires, or from a button_Clicked handler.
FormField formField = pdfViewer.FormFields.Where(x => x.Name == "name").FirstOrDefault();
if (formField is TextFormField nameTextBox)
{
// Set the value of the "name" text field.
}Editing checkbox form fields
By modifying the IsChecked property, the checkbox field can be checked or unchecked programmatically. The following code snippet illustrates retrieving a checkbox form field named “newsletter” from the PDF Viewer.
FormField formField = PdfViewer.FormFields.Where(x => x.Name == "newsletter").FirstOrDefault();
if (formField is CheckboxFormField checkBox)
{
// Mark the checkbox as checked.
checkBox.IsChecked = true;
}Editing combo box form fields
The SelectedItem property can be used to programmatically choose an item from the combo box. The SelectedItem should be one of the values from the ComboBoxFormField.Items array. The following code snippet illustrates retrieving a combobox form field named “state” from the PDF Viewer.
FormField formField = PdfViewer.FormFields.Where(x => x.Name == "state").FirstOrDefault();
if (formField is ComboBoxFormField comboBox)
{
// Select the desired item from the combo box.
comboBox.SelectedItem = comboBox.Items[4];
}Editing list box form fields
The SelectedItems property can be used to programmatically choose an item from the list box. The SelectedItems should contain only the values from the ListBoxFormFields.Items array. One or more selections are supported by the list box. The below code snippet illustrates modifying a single-select list box form field named “courses” from the PDF Viewer.
FormField formField = PdfViewer.FormFields.Where(x => x.Name == "courses").FirstOrDefault();
if (formField is ListBoxFormField listBox)
{
// Select the desired item from the list box.
listBox.SelectedItems = new ObservableCollection<string> { listBox.Items[0] };
}The below code snippet illustrates modifying a multi-select list box form field named “courses” from the PDF Viewer.
FormField formField = PdfViewer.FormFields.Where(x => x.Name == "courses").FirstOrDefault();
if (formField is ListBoxFormField listBox)
{
// Select the desired item from the list box.
listBox.SelectedItems = new System.Collections.ObjectModel.ObservableCollection<string> { listBox.Items[1], listBox.Items[2], listBox.Items[3] };
}Editing radio button form fields
Programmatically select an item from the radio buttons using the SelectedItem property. The SelectedItem should be one of the values from the RadioButtonFormField.Items array. The following code snippet illustrates retrieving a radio button form field named “gender” from the PDF Viewer.
FormField formField = PdfViewer.FormFields.Where(x => x.Name == "gender").FirstOrDefault();
if (formField is RadioButtonFormField radioButton)
{
// Select the desired item from the radio buttons.
radioButton.SelectedItem = radioButton.Items[0];
}Editing signature form fields
Programmatically, add a signature to an unsigned signature field by creating and assigning an ink annotation to the SignatureFormField.Signature property. The following code snippet illustrates retrieving a signature form field named “signature” from the PDF Viewer.
SignatureFormField? signature = PdfViewer.FormFields.Where(x => x.Name == "signature").FirstOrDefault() as SignatureFormField;
if (signature != null)
{
List<List<float>> inkPointsCollection = new();
inkPointsCollection.Add(new List<float> { 10f, 10f, 10f, 20f, 20f, 20f, 30f, 30f, 30f, 40f, 40f, 40f, 50f, 60f });
InkAnnotation inkSignature = new InkAnnotation(inkPointsCollection, signature.PageNumber);
inkSignature.Color = Colors.Red;
// Add the created handwritten signature to the signature form field.
signature.Signature = inkSignature;
}The Signature property is of type InkAnnotation and it will behave like an ink after signing. If the PDF document is saved, the signature will be preserved as an ink annotation in the saved document.
Suppressing the signature modal view
The SfPdfViewer allows you to suppress the signature modal view and use your own UI in its place. This can be achieved by setting the FormFieldModalViewAppearingEventArgs.Cancel property to true in the SignatureModalViewAppearing event handler.
The below code snippet illustrates suppressing the signature modal view and using a UI implemented in the app in its place. In this illustration, it is assumed that the signature is produced in the form of an image stream when the user completes drawing the signature in the custom dialog. When the signing is completed using the custom dialog, a stamp annotation is created and assigned as the signature of the form field.
SignatureFormField? signatureFormField;
pdfviewer.SignatureModalViewAppearing += PdfViewer_SignatureModalViewAppearing;
private void PdfViewer_SignatureModalViewAppearing(object? Sender, FormFieldModalViewAppearingEventArgs e)
{
e.Cancel = true;
signatureFormField = e.FormField as SignatureFormField;
// Implement your own UI for creating a signature.
ShowCustomDialog();
}
Private void customDialogOkButton_Clicked(object sender, EventArgs e)
{
//Get the signature in the form of a Stream instance (possibly converted from an image of the user's freehand drawing)
signatureImageStream = GetSignatureImageStream();
// Create a stamp annotation. The bounds values are not necessary since the stamp will be automatically fit over the signature form field.
StampAnnotation signatureStamp = new StampAnnotation(signatureImageStream, signatureFormField.PageNumber, new RectF(0, 0, 0, 0));
signatureFormField.Signature = signatureStamp;
}Button form fields
Button form fields will be rendered in the PDF viewer. But the PDF viewer supports only the GoTo actions that navigates to a particular location in the PDF document alone. Other types of button actions are not supported.
Flatten form fields only on save
The FlattenOnSave property converts form fields into non-editable content only when the PDF document is saved. This means the form fields remain editable while the document is open, and are flattened (made part of the document content) during the save operation, preventing any further modification afterward.
Flatten specific form fields
You can selectively flatten specific form fields, such as signature fields, by iterating through the form field collection.
foreach (var item in pdfViewer.FormFields)
{
//Iterate Only signature form field and flatten it
if (item is SignatureFormField signature)
{
item.FlattenOnSave = true;
}
}Flatten all form fields
To flatten all form fields in the document, set the FlattenOnSave property for each field:
//Iterate all the form fields and set flatten
foreach (var item in pdfViewer.FormFields)
{
item.FlattenOnSave = true;
}Controlling form field editing at the viewer Level
The AllowEditFormFields property is used to control form field editing at the viewer level. By default, editing is enabled, allowing users to interact with all supported form fields. When this property is set to false, all form fields become non-editable, making the document effectively read-only without modifying individual field properties. This behavior applies to all form field types and takes effect immediately on the loaded document.
You can disable editing programmatically using the following:
<syncfusion:SfPdfViewer
x:Name="PdfViewer"
AllowEditFormFields="False" />// Disable form field editing
pdfViewer.AllowEditFormFields = false;This property supports dynamic changes at runtime, meaning you can enable or disable form field editing at the viewer level based on requirements, and the changes will be applied instantly.
NOTE
Setting AllowEditFormFields to false does not modify the ReadOnly property of individual form fields. It acts as an additional layer of control, and a field remains non-editable if either its ReadOnly property is true or viewer-level editing is disabled