Syncfusion AI Assistant

How can I help you?

Form Fields Collection in .NET MAUI PDF Viewer (SfPdfViewer)

7 Mar 20263 minutes to read

The SfPdfViewer.FormFields property provides access to all form fields present in the loaded PDF document. The collection is available after the document finishes loading and can be accessed from the DocumentLoaded event.

Accessing the FormFields collection

The following code snippet illustrates getting the total count of form fields in the PDF document.

public void WireDocumentLoadedEvent()
{
    // Wire the document loaded event to occur when a PDF document is loaded.
    PdfViewer.DocumentLoaded += OnDocumentLoaded;
}

private void OnDocumentLoaded(object? sender, EventArgs? e)
{
    // Get the form field count.
    int fieldCount = PdfViewer.FormFields.Count;
}

Retrieve a specific form field by name

You can retrieve a specific form field from the collection by filtering on the Name property. The following example retrieves a text form field named "name".

FormField formField = PdfViewer.FormFields.Where(x => x.Name == "name").FirstOrDefault();

if (formField is TextFormField nameTextBox)
{
    string currentValue = nameTextBox.Text;
}

Restrict form field editing

Form fields can be prevented from being modified by setting the ReadOnly property to true. The following example makes all form fields read-only.

// Restrict editing of all form fields.
foreach (FormField formField in PdfViewer.FormFields)
{
    formField.ReadOnly = true;
}

Clear form data

The ClearFormData method clears the data in all form fields in the PDF document.

// Clear all form field data in the document.
PdfViewer.ClearFormData();

To clear form data on a specific page, pass the page number to the method.

// Clear all form field data on page 2.
PdfViewer.ClearFormData(2);

Attach custom data to a form field

The CustomData property allows you to store additional reference information on any form field instance. This data is for application use only and is not displayed or saved in the PDF document.

The following example stores the modification timestamp on a form field whenever its value changes.

private void PdfViewer_FormFieldValueChanged(object sender, FormFieldValueChangedEventArgs e)
{
    e.FormField.CustomData = "Modified: " + DateTime.Now.ToString();
}

See Also