Form Filling in PDF Files in WPF Pdf Viewer

12 Nov 20217 minutes to read

PDF Viewer provides the ability to Fill, Edit, Flatten, and Save the AcroForm fields in PDF files.

WPF PDF Viewer Form Filling

Supported form fields

You can load and fill the following form fields in a PDF document using the PDF Viewer.

  1. Text box.
  2. Password box.
  3. Checkbox.
  4. Radio button.
  5. Combo box.
  6. List box.

Retrieve the form field details

You can retrieve the details of a form field through the FormFieldClicked event of PdfViewerControl by simply clicking on the field. The FormField property of the FormFieldClickedEventArgs needs to be typecast to the respective control.The following code snippet explains how to retrieve details for all supported form fields using the FormFieldClicked event.

//Wire the `FormFieldClicked` event.
pdfViewer.FormFieldClicked += PdfViewer_FormFieldClicked;

private void PdfViewer_FormFieldClicked(object sender, FormFieldClickedEventArgs args)
{
    if (args.FormField is TextBox)
            {
                //Typecast the `FormField` property to `System.Windows.Controls.TextBox`
                TextBox text = args.FormField as TextBox;
                //{Insert your code here}
            }
            else if (args.FormField is PasswordBox)
            {
                //Typecast the `FormField` property to `System.Windows.Controls.PasswordBox`
                PasswordBox PasstextBox = args.FormField as PasswordBox;
                //{Insert your code here}
            }          
            else if (args.FormField is ListBox)
            {
                //Typecast the `FormField` property to `System.Windows.Controls.ListBox`
                ListBox listBox = args.FormField as ListBox;
                //{Insert your code here}
            }         
            else if (args.FormField is ComboBox)
            {
                //Typecast the `FormField` property to `System.Windows.Controls.ComboBox`
                ComboBox comboBox = args.FormField as ComboBox;
                //{Insert your code here}
            }
            else if (args.FormField is CheckBox)
            {
                //Typecast the `FormField` property to `System.Windows.Controls.CheckBox`
                CheckBox checkBox = args.FormField as CheckBox;
                //{Insert your code here}
            }
            else if (args.FormField is RadioButton)
            {
                //Typecast the `FormField` property to `System.Windows.Controls.RadioButton`
                RadioButton radiobtn = args.FormField as RadioButton;
                //{Insert your code here}
            }	 
}

NOTE

The sample project of PDF form filling using the Syncfusion PDF Viewer is available in the GitHub.

Import and Export Form Data

In WPF, the PDF viewer allows the users to import and export form data to and from the PDF documents.

Import Form Data

Follow the below steps to import date to PDF document with AcroForm.

  1. Click the form data tool button in the left pane, the form data toolbar will appear as a secondary toolbar in the PdfViewerControl.
  2. Select Import option in form data toolbar to import the PDF form data.

WPF PDF Viewer Import Form Data

The following code shows how to import form data in code behind.

private void button1_Click(object sender, RoutedEventArgs e)
{
    //Import PDF form data
    pdfviewer.ImportFormData("Import.fdf", Syncfusion.Pdf.Parsing.DataFormat.Fdf);
}
Private Sub button1_Click(sender As Object, e As RoutedEventArgs)
    'Import PDF form data
    pdfviewer.ImportFormData("Import.fdf", Syncfusion.Pdf.Parsing.DataFormat.Fdf)
End Sub

Export Form Data

Follow the below steps to export data from PDF document

  1. Select Export option in the form data toolbar, to save the completed PDF form data as a file in another file format.
  2. In Export Form Data As dialog box, you can select the desired format to save the form data (FDF, XFDF, XML, and JSON).

NOTE

If the PDF document is loaded as a stream, the PdfViewerControl will request for the form name when exporting.

The following code shows how to export form data in code behind.

private void button1_Click(object sender, RoutedEventArgs e)
{
    //Export PDF form data
    pdfviewer.ExportFormData("Export.fdf", Syncfusion.Pdf.Parsing.DataFormat.Fdf, "SourceForm.pdf");
}
Private Sub button1_Click(sender As Object, e As RoutedEventArgs)
    'Export PDF form data
    pdfviewer.ExportFormData("Export.fdf", Syncfusion.Pdf.Parsing.DataFormat.Fdf, "SourceForm.pdf")
End Sub

NOTE

You can refer to our WPF PDF Viewer feature tour page for its groundbreaking feature representations. You can also explore our WPF PDF Viewer example to know how to render and configure the pdfviewer.