Back to Devexpress

Fill Interactive Forms

wpf-115889-controls-and-libraries-pdf-viewer-interactive-forms-interactive-form-filling.md

latest5.4 KB
Original Source

Fill Interactive Forms

  • Jul 23, 2021
  • 3 minutes to read

This document describes how to fill a document with interactive forms and provides customization options.

PDF Viewer allows you to enter or select values in fillable form fields.

Customization

Highlight Form Field

The PDF Viewer can highlight interactive form fields with a color. To enable this functionality, set the PdfViewerControl.HighlightFormFields property to true.

If you wish to change the default color, which is used to highlight form fields, use the PdfViewerControl.HighlightedFormFieldColor property.

The image below demonstrates the PdfViewerControl.HighlightedFormFieldColor property set to Color.FromArgb(120, 255, 0, 0).

Change Form Field Value

You can use the PdfDocumentFacade class to specify form field values.

Important

The Universal Subscription or an additional Office File API Subscription is required to use this example in production code. Refer to the DevExpress Subscription page for pricing information.

The PdfViewerExtensions.GetDocumentFacade(IPdfViewer) method retrieves the PdfDocumentFacade class object that allows you to change the PDF document without access to its inner structure. Use the PdfDocumentFacade.AcroForm property to get interactive form field options.

Each FormFieldFacade class has the Value property that allows you to specify a form field value. Use the PdfChoiceFormFieldFacade.Items property to obtain a list of choice form field (combo box, list box) values.

The code sample below specifies various form field values:

csharp
public MainWindow()
{
    InitializeComponent();
    pdfViewer.DocumentLoaded += PdfViewer_DocumentLoaded;
    pdfViewer.OpenDocument("FormDemo.pdf");
}

private void PdfViewer_DocumentLoaded(object sender, RoutedEventArgs e)
{
    pdfViewer.DocumentLoaded -= PdfViewer_DocumentLoaded;
    FillFormFields();
}

private static void FillFormFields()
{
    PdfDocumentFacade documentFacade = pdfViewer.GetDocumentFacade();
    PdfAcroFormFacade acroForm = documentFacade.AcroForm;
    PdfTextFormFieldFacade visaField = acroForm.GetTextFormField("VisaNo");
    visaField.Value = "73203393";

    PdfTextFormFieldFacade addressField = acroForm.GetTextFormField("Address");
    addressField.Value = "98033, 722 Moss Bay Blvd., Kirkland, WA, USA";

    PdfRadioGroupFormFieldFacade genderField = acroForm.GetRadioGroupFormField("Gender");
    genderField.Value = genderField.Field.Items[2].Value;

    PdfComboBoxFormFieldFacade nationalityField = acroForm.GetComboBoxFormField("Nationality");
    nationalityField.Value = nationalityField.Items[68].Value;
    pdfViewer.SaveDocument("FormDemo_new.pdf");
}
vb
public MainWindow()
    InitializeComponent()
    AddHandler pdfViewer.DocumentLoaded, AddressOf PdfViewer_DocumentLoaded
    pdfViewer.OpenDocument("FormDemo.pdf")
End Sub

Private Sub PdfViewer_DocumentLoaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
    RemoveHandler pdfViewer.DocumentLoaded, AddressOf PdfViewer_DocumentLoaded
    FillFormFields()
End Sub

private static void FillFormFields()
    Dim documentFacade As PdfDocumentFacade = pdfViewer.GetDocumentFacade()
    Dim acroForm As PdfAcroFormFacade = documentFacade.AcroForm
    Dim visaField As PdfTextFormFieldFacade = acroForm.GetTextFormField("VisaNo")
    visaField.Value = "73203393"

    Dim addressField As PdfTextFormFieldFacade = acroForm.GetTextFormField("Address")
    addressField.Value = "98033, 722 Moss Bay Blvd., Kirkland, WA, USA"

    Dim genderField As PdfRadioGroupFormFieldFacade = acroForm.GetRadioGroupFormField("Gender")
    genderField.Value = genderField.Field.Items(2).Value

    Dim nationalityField As PdfComboBoxFormFieldFacade = acroForm.GetComboBoxFormField("Nationality")
    nationalityField.Value = nationalityField.Items(68).Value
    pdfViewer.SaveDocument("FormDemo_new.pdf");

Form Field Events

Handle one the following events to control specific form field actions:

EventDescription
FormFieldValueChangingOccurs when the user starts to edit a form field value.
FormFieldValueChangedFires after a form field value is changed.
PdfViewerControl.ShowingEditorFires when the form field receives focus.
PdfViewerControl.HiddenEditorOccurs when the form field loses focus.

See Also

Import and Export Interactive Form Data