How can I help you?
Form Fields API in WPF PDF Viewer
27 Apr 20267 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 as in different format (Fdf,xfdf,Json,xml). |
| ExportFormData | Export form field data as in different format (Fdf,xfdf,Json,xml)as a downloadable file. |
| LoadedDocument.Form.Fields | By accessing the LoadedDocument form fileds we can perform add, remove and modify the form fileds 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 to 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 to 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 to 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