Form Filling
Acro Form field values can be set as they are merged into a new PDF document. This is done through the Form property of the MergeDocument class.
The value that is specified needs to match the export value for the form field. Unicode values are fully supported.
This example shows how to set the values on a merged Acro Form.
MergeDocument document = new MergeDocument( @"C:\MyDocument.pdf" );
// Set the field values
document.Form.Fields["TextBox1"].Value = "My Text"; // TextBox field
document.Form.Fields["CheckBox1"].Value = "Yes"; // CheckBox field
document.Form.Fields["ComboBox1"].Value = "Item4"; // ComboBox field
document.Form.Fields["RadioButton1"].Value = "Item2"; // RadioButton field
ListBoxField listBox = document.Form.Fields["ListBox1"]; // ListBox field
listBox.SetValues( new string[] { "Item1", "Item3", "Item5" } );
// Save the PDF
document.Draw( @"C:\MyDocument.pdf" );
Dim MyDocument As MergeDocument = New MergeDocument( "C:\MyDocument.pdf" )
' Set the field values
MyDocument.Form.Fields("TextBox1").Value = "My Text" ' TextBox field
MyDocument.Form.Fields("CheckBox1").Value = "Yes" ' CheckBox field
MyDocument.Form.Fields("ComboBox1").Value = "Item4" ' ComboBox field
MyDocument.Form.Fields("RadioButton1").Value = "Item2" ' RadioButton field
Dim MyListBox As ListBoxField = MyDocument.Form.Fields("ListBox1") ' ListBox field
MyListBox.SetValues(New String() {"Item1", "Item3", "Item5"})
' Save the PDF
MyDocument.Draw("C:\MyDocument.pdf")
Marking Acro Form Fields As Read Only
This example shows how to mark form fields as read only so that they can not be modified once they are set.
MergeDocument document = new MergeDocument( @"C:\MyDocument.pdf" );
// Set the field values
document.Form.Fields["TextBox1"].Value = "My Text"; // TextBox field
// Make the form read only
document.Form.IsReadOnly = true;
// Save the PDF
document.Draw( @"C:\MyDocument.pdf" );
Dim MyDocument As MergeDocument = New MergeDocument( "C:\MyDocument.pdf" )
' Set the field values
MyDocument.Form.Fields("TextBox1").Value = "My Text" ' TextBox field
' Make the form read only
MyDocument.Form.IsReadOnly = True
' Save the PDF
MyDocument.Draw("C:\MyDocument.pdf")
Filling Reorganized Acro Form Fields
It is possible to reorganize the Acro Form fields as they are merged into the PDF document. This prevents renaming issues and helps better organize the form field data.
This example shows how to merge two PDF documents containing Acro Form fields and reorganize their names.
MergeDocument document = new MergeDocument( @"C:\fw_4.pdf", new MergeOptions( true, "fw_4" ) );
document.Append( @"C:\fw_9.pdf", new MergeOptions( true, "fw_9" ) );
// Set the field values
document.Form.Fields["fw_4.FirstName"].Value = "John";
document.Form.Fields["fw_9.FirstName"].Value = "John";
// Save the PDF
document.Draw( @"C:\MyDocument.pdf" );
Dim MyDocument As MergeDocument = New MergeDocument( "C:\fw_4.pdf", New MergeOptions( True, "fw_4" ) )
MyDocument.Append( "C:\fw_9.pdf", New MergeOptions( True, "fw_9" ) )
' Set the field values
MyDocument.Form.Fields("fw_4.FirstName").Value = "John"
MyDocument.Form.Fields("fw_9.FirstName").Value = "John"
' Save the PDF
MyDocument.Draw( "C:\MyDocument.pdf" )
The Acro Form fields in the first document will now have "fw_4." prepended to their name, and the Acro Form fields from the second document will have "fw_9." prepended to their names.