Skip to main content
Participant
November 18, 2024
Answered

Autofill a form's field using an if condition

  • November 18, 2024
  • 2 replies
  • 640 views

I am attempting to set up a script to run that will automatically complete different types of forms with demographic information- so not all forms that will use the script will be the same. I don't need this to be perfect but would like for when a field says either Zip or Zip Code, either would fill with the correct number because the word Zip appears. I'm using a custom script below that works if the field requested is Zip Code:

 

var zipcodefield = this.getField("Zip Code");

zipcodefield.value = "60690";

 

I am not sure if there is an if condition for that, and if so, I'm not sure how exactly to set it up. Can anyone help?

This topic has been closed for replies.
Correct answer Thom Parker

Sounds like this should be a folder level automation script that is run from a tool button.

https://www.pdfscripting.com/public/Automating-Acrobat.cfm

 

One way to do this is to search all the fields for ones that match a pattern

 

Here is a brute force approach:

var strFldName;
for(var i=0;i<this.numFields;i++){
   strFldName = this.getNthFieldName(i);
   
   if(/Zip/i.test(strFldName))
       this.getField(strFldName).value = "9999999";
   else if(/State/i.test(strFldName)) 
       this.getField(strFldName).value = "Illinois";
}

 The regular expressions and the "if" statements can be modified to include more and different conditions. 

 

 

2 replies

Thom Parker
Community Expert
Thom ParkerCommunity ExpertCorrect answer
Community Expert
November 19, 2024

Sounds like this should be a folder level automation script that is run from a tool button.

https://www.pdfscripting.com/public/Automating-Acrobat.cfm

 

One way to do this is to search all the fields for ones that match a pattern

 

Here is a brute force approach:

var strFldName;
for(var i=0;i<this.numFields;i++){
   strFldName = this.getNthFieldName(i);
   
   if(/Zip/i.test(strFldName))
       this.getField(strFldName).value = "9999999";
   else if(/State/i.test(strFldName)) 
       this.getField(strFldName).value = "Illinois";
}

 The regular expressions and the "if" statements can be modified to include more and different conditions. 

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Participant
November 19, 2024

This is it! Much appreciated and hopefully saving our teams a lot of time. Thank you!!

try67
Community Expert
Community Expert
November 19, 2024

So if another field's value is "Zip" or "Zip Code" then you want to fill this field with that number? Should the user be able to override this value and change it to something else?

Participant
November 19, 2024

for what I need this, not likely. Ideally it would fill with 60690 for any form that calls for a Zip or Zip Code. Will also be using to fill fields that say "state/province" for example - I want anything that asks for a State to be Illinois