formspdfacrobatpdf-form

How do I change PDF form field backgrounds when text is entered into them?


I'm creating a PDF form to distribute to people. When text is not entered into any of the fields on the form, I want it to remain transparent, but when someone enters text into it, I want the background colour of the field to change to white.

I found this page which describes how you can do it with JavaScript, so I added it to document JavaScripts in my PDF, but it didn't work. The fields continue to have the same transparent background they do by default.

How do I achieve what I'm looking for, with or without JavaScript? I'm using Acrobat Pro DC. Thanks!


Solution

  • Put this script in the field's custom format script

    /* Turns off default field highlighting. Normally you'd put this in a doc level script it's just here for completeness */
    app.runtimeHighlight = false;
    /* The rest of this belongs in the custom format script */
    var field = event.target;
    if (field.value == field.defaultValue) {
        /* set the fillColor is the field value is the same as the default (generally an empty string) */
        field.fillColor = color.ltGray
    }
    else {
        field.fillColor = color.transparent
    }
    

    I have a functioning example file here.