Search code examples
javascriptpdfadobeacrobat

Conditionally fill button in Adobe PDF form


I have created a huge button covering my whole PDF file. I want to fill it in white if the necessary fields in the PDF form are not filled in.(This is to prevent people from skipping required fields. My javascript code is listed below. I have initially filled in the button using Adobe Acrobat Pro, I am then trying to remove the button if the "year" field is completed.

var aNames = ["year"];
var bComplete = true;
var cValue ="";
for(i=0;i<aNames.length;i++){
    cValue=this.getField(aNames[i]).value;
    if(cValue==this.getField(aNames[i]).defaultValue){ 
        bComplete=false;
    }
}

if(bComplete==true){
    document.getElementById("Button1").remove();
}else {
app.alert("Please complete form",0,0);
}

Solution

  • This looks very much like influenced by webbrowser JavaScript…

    The first issue is the button; if it covers the document, how can you fill the form (or does it cover subsequent pages?).

    About the JavaScript, you do not need to create an array of field names, you know the fields directly, so you can address them directly, without having to loop, as in:

    if (this.getField("myYear").value != this.getField("myYear").defaultValue) 
    

    Of course, if you have a bunch of such fields, it may be an idea to create a document-level array of the field names.

    Next, you won't remove the button field, but you simply hide it, as in:

    this.getField("myCoverButton").display = display.hidden ;
    

    It might be a good idea to have a look at the Acrobat JavaScript documentation, which is part of the Acrobat SDK, downloadable from the Adobe website.