I was recently given an assignment where I needed to populate the fields in an existing PDF with data from a separate form. I had no idea where to start on this task so I immediately turned to Google for help. Little did I know that this task was going to turn out easier than I had expected. After a few minutes of searching, I found that the secret lies in an amazingly simple language called
Forms Data Format (FDF).
As far as searching goes, I found plenty of libraries that would do much of the work for generating an FDF file automatically but as a programmer, I had to ask myself "where's the fun in that?" so I kept on hunting and eventually stumbled across an actual FDF reference buried deep inside a PDF version of the PDF reference manual. In this article, I'll cover some of the basics of using FDF to populate an existing PDF.
FDF is a simple text format based on PDF. Typically, FDF is used to submit data to a server and merge the data into a PDF template but it may also be used independently to save form data for later use.
At a minimum, an FDF file is comprised of a header, a body, and a trailer (or footer).
Header Format
The header of the FDF consists of just one line. The header identifies the file as a FDF file and also specifies the version.
Body Format
The body of the FDF specifies the object being manipulated along with field name/value pairings. The body
can also optionally contain an path to the target PDF. The table below contains some attributes that are commonly
used inside of a FDF.
Common Body Attributes
| Key |
Description |
| T |
Field Name |
| V |
Value |
| F |
Path to the target PDF |
Trailer Format
Finally, there is the trailer. The trailer of a FDF defines an object dictionary and also contains the end of file marker. The only required key inside of the FDF trailer is "ROOT" which must reference the main object defined in the body.
Sample FDF
%FDF-1.2 %Comment
1 0 obj %Object Definition
<<
/FDF
<<
/Fields [
<<
%Setting the value of field1
<< /T (field1) /V (value1) >>
%Setting the value of field2
<< /T (field2) /V (value2) >>
]
/F (template.pdf) %identifying the target PDF
>>
>>
endobj
trailer
<< /Root 1 0 R >>
%%EOF
Clean-up
As you can see, creating an FDF is a pretty straight forward process. Since the format is plain text, software can easily be adapted to generate a FDF file automatically. One caveat to creating a FDF on the fly without using a library is that the field names need to be known in order for the FDF to function properly. If a tool to retrieve the field names is not available, the field names can probably be extracted by opening the PDF in a text editor and reading them...if you can follow the PDF syntax.
More Information