Acro Form filed values can be set as they are merged into a new PDF document. This is done through the getForm() 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.
[Java]
MergeDocument document = new MergeDocument("[PhysicalPath]/MyDocument.pdf");
// Set the field values
document.getForm().getFields().getFormField("TextBox1").setValue("My Text"); // TextBox field
document.getForm().getFields().getFormField("CheckBox1").setValue("Yes"); // CheckBox field
document.getForm().getFields().getFormField("ComboBox1").setValue("Item4"); // ComboBox field
document.getForm().getFields().getFormField("RadioButtom1").setValue("Item2"); // RadioButtom field
ListBoxField listBox = document.getForm().getFields().getFormField("ListBox1"); // ListBox field
listBox.setValues(new String[] {"Item1", "Item3", "Item5"});
// Save the PDF
document.draw("[PhysicalPath]/MyDocument.pdf");
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.
[Java]
MergeDocument document = new MergeDocument("[PhysicalPath]/fw_4.pdf", new MergeOptions(true, "fw_4"));
document.append("[PhysicalPath]/fw_9.pdf", new MergeOptions(true, "fw_9"));
// Set the field values
document.getForm().getFields().getFormField("fw_4.FirstName").setValue("John");
document.getForm().getFields().getFormField("fw_9.FirstName").setValue("John");
// Save the PDF
document.draw("[PhysicalPath]/MyDocument.pdf");
The Acro Form fields in the first document will now have "fw_4." prepended to their names, and the Acro Form fields from the second document will have "fw_9." prepended to their names.