Sample of the Week:
In an earlier post I discussed how to merge PDF files using the Datalogics PDF Java Toolkit in a way that preserved all of the page features, bookmarks, and links. By default, the toolkit merges form fields in the same way that Acrobat does when you merge documents through the Acrobat UI. If you are combining multiple different forms that were filled out by the same person and the values are likely to all be the same, this works perfectly… it’s exactly what you’d want. However, if you want to create an archive of the same form that was filled out by different individuals, you may want to take a different approach.
As background, in PDF, each fully qualified field name can only have one value, multiple representations of that value can show on different pages and with completely different appearances… but the field always has the same underlying value. This can cause problems if your goal as a developer is to combine multiple copies of the same form that have the same field names but different values. There are a number of ways to handle this situation programmatically and two of them are presented here as Gists; you can rename the fields so that each one is unique and carries it’s own value or you can flatten the form. Which approach is considered “correct” really depends on your use case. If you’ve already looked at the MergeDocuments Gist, this new code will look pretty much the same except for the area where we deal with the form fields.
Merging Forms and Retaining Interactive Fields:
If your goal is to merge multiple PDF files and retain the interactive form fields with their discrete values and you know that there will be field name collisions, the easiest approach is to simply rename the fields prior to merging the documents. In the highlighted code snippet below, I iterate through the form fields and then prepend the base filename of the PDF file to the field name to make each field in the combined document unique.
This allows the value of the field to migrate into the new combined file properly. However, because the field name has changed, any calculations that refer to that field won’t work properly. For this reason, my recommendation is to flatten the fields removing the interactivity and the risk of corrupting the collected data when fields are merged.
Merging Forms and Flattening Fields:
Flattening the form prior to merging the files completely removes the problems inherent in combining forms… mostly because you’re no longer merging form data, just the appearances. In the highlighted code snippet below, the FormFlattener class is used to flatten all forms in the PDF document prior to merging; the form field widgets are removed.
This method allows the developer to create a single, print-ready, PDF file that visually represents the data that was entered into the form.
Click the download .ZIP icon on the right to get the input files for these two Gists. You’ll also need at least an evaluation version of the Datalogics PDF Java Toolkit.