Populating PDF Form Fields from a URL Query String

| 14 Comments

Have you ever wanted to pre-populate a few form fields in a PDF file without using a server tool like the FDF Toolkit; just something quick and dirty? Well – with a clever use of Acrobat JavaScript, the URL property of the doc object you can do just about anything including fill form fields based on a URL’s query string.

JavaScript for Acrobat can be placed at a variety of levels: Document Level, Page Level, Folder Level, Field Level… the list goes no and on. If you place some JavaScript at the document level and you don’t wrap it in a function, the JavaScript will run when the document first opens and before the user gets control of it. By capturing the URL of the PDF file and parsing the query string, your JavaScript can pick up the parameters and then do something interesting… like set the value of a form field.

The following URL’s query string contains key/value pairs. For each key, the script below will add a value to each field in the PDF file with the same name as the key. The whiteList array in the code will allow you to limit which fields can be populated; more on this later.

The URL:

http://pdfdevjunkie.host.adobe.com/pdf/AquoITSurveyFormII.pdf?Name=Joel%20Geraci

The Script:

//only run the script if the PDF file is being viewed in a browser window
if (this.external)
{
//The whiteList defines which fields are permitted to be changed by the URL.
//If you want all fields to be changed, leave the array empty as in "[]"
whiteList = ["Name", "Address", "City", "State", "ZipCode"]

//get the parameters portion of the URL and unescape it so we get the spaces and punctuation back
parametersString = this.URL.substring(this.URL.indexOf("?")+1)
//only run the script if there are parameters
if (parametersString.length > 0)
{
//create an array of key/value pairs
parameters = parametersString.split("&")
//loop through the array...
for each (parameter in parameters)
{
//create a 2 element array for each parameter as [key, value]
kvPair = parameter.split("=")
//set the field named "key" to "value"
fieldName = unescape(kvPair[0])
if (whiteList.length > 0)
{
if (whiteList.indexOf(fieldName) > -1)
{
this.getField(fieldName).value = unescape(kvPair[1])
}
}
else
{
this.getField(fieldName).value = unescape(kvPair[1])
}
}
}
}

Examples:

Link to PDF file without Parameters

Link to PDF file pre-populated for Conrad Simms

Link to PDF file pre-populated for Alex Pink

In practice, you’d probably want a Cold Fusion, PHP script or similar technology to automate the link creation rather than have it hard coded into the HTML but you get the idea.

The examples above are AcroForms but the concept works the same with XFA forms. If you are using an XFA based form, the technique is the same but the script will be slightly different. Rather than using a document level JavaScript, in the XFA case, use the docReady event (the last event to fire before the user gets control).

A couple words of warning.

Once you’ve added such a script to your PDF file. Anyone who can link to the file can populate field values from a URL. I can think of several applications where you don’t want that to be the case. To help in these situations, the script contains a whiteList array. The whiteList array determines which fields can be pre-populated by the values in the query string. The script then checks the whiteList array before modifying the field value. If you want to disable the whiteList and make all fields able to be pre-populated, just leave the array empty. In the Conrad Simms example above, the URL contains values for "phone" and "’mobile" but because of the whiteList, those fields do not get changed.

Additionally, you won’t want to use this for long forms. Different browsers have different maximum lengths for a URL, you may bump up into them sooner than you think.