Form Fields API in WPF PDF Viewer
9 Jul 20266 minutes to read
The PDF Viewer provides comprehensive APIs to Add, edit, remove, import, export, and manage form fields programmatically. The following APIs are available:
| API | Description |
|---|---|
| ImportFormData | Import form field data in different formats (FDF, XFDF, JSON, XML). |
| ExportFormData | Export form field data in different formats (FDF, XFDF, JSON, XML) as a downloadable file. |
| LoadedDocument.Form.Fields | By accessing the LoadedDocument form fields, we can add, remove, and modify the form fields in code behind. |
ImportFormData
The following example imports form field data as FDF.
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 SubExportFormData
The following example exports form field data as FDF.
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 SubLoadedDocument Form field
The below code snippet illustrates how to add a textbox field in a LoadedDocument
private void AddTextbox_Click(object sender, RoutedEventArgs e)
{
if (pdfViewer.LoadedDocument.Form != null)
{
PdfLoadedPage page = pdfViewer.LoadedDocument.Pages[0] as PdfLoadedPage;
//Create a textbox field and add the properties.
PdfTextBoxField textBoxField = new PdfTextBoxField(page, "FirstName");
textBoxField.Bounds = new RectangleF(0, 0, 100, 20);
//Add the form field to the document.
pdfViewer.LoadedDocument.Form.Fields.Add(textBoxField);
}
}Private Sub AddTextbox_Click(sender As Object, e As RoutedEventArgs)
If pdfViewer.LoadedDocument.Form IsNot Nothing Then
Dim page As PdfLoadedPage = TryCast(pdfViewer.LoadedDocument.Pages(0), PdfLoadedPage)
'Create a textbox field and add the properties.
Dim textBoxField As PdfTextBoxField = New PdfTextBoxField(page, "FirstName")
textBoxField.Bounds = New RectangleF(0, 0, 100, 20)
'Add the form field to the document.
pdfViewer.LoadedDocument.Form.Fields.Add(textBoxField)
End If
End SubThe below code snippet illustrates how to modify a textbox field in a LoadedDocument
private void Update_Click(object sender, RoutedEventArgs e)
{
if (pdfViewer.LoadedDocument.Form != null)
{
if (pdfViewer.LoadedDocument.Form.Fields[0] is PdfLoadedTextBoxField)
{
(pdfViewer.LoadedDocument.Form.Fields[0] as PdfLoadedTextBoxField).Text = "Syncfusion";
}
}
}Private Sub Update_Click(sender As Object, e As RoutedEventArgs)
If pdfViewer.LoadedDocument Is Nothing OrElse pdfViewer.LoadedDocument.Form Is Nothing Then
Return
End If
Dim form = pdfViewer.LoadedDocument.Form
' TextBox (Field 0)
Dim textBox = TryCast(form.Fields(0), PdfLoadedTextBoxField)
If textBox IsNot Nothing Then
textBox.Text = "Syncfusion"
End If
End SubThe below code snippet illustrates how to remove a form field from a LoadedDocument
private void RemoveAt_Click(object sender, RoutedEventArgs e)
{
if (pdfViewer.LoadedDocument.Form.Fields.Count > 0)
//Remove the field at index 0.
pdfViewer.LoadedDocument.Form.Fields.RemoveAt(0);
}Private Sub RemoveAt_Click(sender As Object, e As RoutedEventArgs)
If pdfViewer.LoadedDocument.Form.Fields.Count > 0 Then
`Remove the field at index 0.
pdfViewer.LoadedDocument.Form.Fields.RemoveAt(0)
End If
End Sub