Concatenate fields in a Fillable form

Recently someone referenced an old blog post: use data from one field to populate other fields in an acrobat fill-in forms and asked me how to concatenate multiple field values into one.

So I put together a sample pdf.

In this example I take four ‘Name’ fields and use a script to update the contents of a 5th field with the previous four.

Here’s quick look at the form:
image of the fillable form

The final ‘Names’ field is set to ‘Read Only’ and is populated with the contents of the 1st four name fields.

This script is added as an action to each of the Name fields. It’s triggered ‘on blur’ for the 1st four and ‘on focus’ for the final field:

//this function popluates the referenced field.
<pre>function collatePreSetFieldsTo(populate){
  //always overwrite the value in populate field with these field values
  var one = this.getField("Name1");
  var two = this.getField("Name2");
  var three = this.getField("Name3");
  var four = this.getField("Name4");
  //concatenate
  populate.value = one.value + ' ' + two.value + ' ' + three.value + ' ' + four.value;
}
var five = this.getField("Name5");
collatePreSetFieldsTo(five);

Download the example file and give it a shot.

I used Acrobat Pro 9 to edit the file.

Comments
  • Jeff says:

    Thanks so much for this post. Yours was the first result for acrobat form field concatenation, and this code did perfectly what I needed it to do (with a few changes) for this decidedly non-coder. Cheers!

  • George Kaiser says:

    I would have used the following for the custom JavaScript calculation for the Names field.

    var Name1 = this.getField(“Name1”).value;
    var Name2 = this.getField(“Name2”).value;
    var Name3 = this.getField(“Name3”).value;
    var Name4 = this.getField(“Name4″).value;

    // Concatenate 3 strings with separators where needed
    function fillin(s1, s2, s3, sep) {
    /*
    Purpose: concatenate up to 3 strings with an optional separator
    inputs:
    s1: required input string text or empty string
    s2: required input string text or empty string
    s3: required input string text or empty string
    sep: optional separator sting
    returns:
    sResult concatenated string
    */
    // variable to determine how to concatenate the strings
    var test = 0; // all strings null
    var sResult; // re slut string to return
    // force any number string to a character string for input variables
    s1 = s1.toString();
    s2 = s2.toString();
    s3 = s3.toString();
    if(sep.toString() == undefined) sep = ”; // if sep is undefined force to null
    /*
    assign a binary value for each string present
    so the computed value of the strings will indicate which strings are present
    when converted to a binary value
    */
    if (s1 != “”) test += 1; // string 1 present add binary value: 001
    if (s2 != “”) test += 2; // string 2 present add binary value: 010
    if (s3 != “”) test += 4; // string 3 present add binary value: 100
    /* return appropriate string combination based on
    calculated test value as a binary value
    */
    switch (test.toString(2)) {
    case “0”: // no non-empty strings passed – binary 0
    sResult = “”;
    break;
    case “1”: // only string 1 present – binary 1
    sResult = s1;
    break;
    case “10”: // only string 2 present – binary 10
    sResult = s2;
    break;
    case “11”: // string 1 and 2 present – binary 10 + 1
    sResult = s1 + sep + s2;
    break;
    case “100”: // only string 3 present – binary 100
    sResult = s3;
    break;
    case “101”: // string 1 and 3 – binary 100 + 001
    sResult = s1 + sep + s3;
    break;
    case “110”: // string 2 and 3 – binary 100 + 010
    sResult = s2 + sep + s3;
    break;
    case “111”: // all 3 strings – binary 100 + 010 + 001
    sResult = s1 + sep + s2 + sep + s3;
    break;
    default: // any missed combinations
    sResult = “”;
    break;
    }
    return sResult;
    }

    var Temp = fillin(Name1, Name2, Name3, ” “);
    event.value = fillin(Temp, Name4, “”, ” “);

  • Peter says:

    That’s fantastic, works like a charm in Adobe X. Thanks for posting!

Leave a Comment