Skip to main content
New Participant
March 22, 2024
Question

Hiding multiple fields on a form if they are empty.

  • March 22, 2024
  • 2 replies
  • 1540 views

I have created a PDF form that has many user fillable fields and also has spawned template pages for additional information. I am looking for a way for the user of the form to hide any empty form fields and drop down fields that the did not need. 

I have found scrip to identify empty fields if they are marked as required and i have found examples of  JS to hide select individual fields only by their name controlled by a checkbox. Listing the fields by name wont work because i do not know what fields will be empty and if i did there are maybe 100 different field names.

I believe there must be a  script to first identify empty fields on a select page and, if empty, to hide the fields / dropdowns. I am thinking this would be controlled with a mouse up JS button on the page.

100% disclosure, Beyond Novice here! Learning but probably over my head, yet. so far i have learned / managed to do quite alot with this form through searching this and other forums. But would prefer if someone could provide me the complete script to do so since i am not understanding how to combine seperate operations. 

Thank you,

This topic has been closed for replies.

2 replies

Thom Parker
Adobe Expert
March 22, 2024

"Empty" fields can be identified with two different methods

1. Compare the field value to an empty string. This works for most text fields, but is otherwise unreliable. 

2. Compare the field value to it's default value. This works for all fields that have a value, i.e. no buttons and signatures.

 

Identifying the page for a field is not as straight forward as you'd think.  Conceptually, fields do not exist on pages, they are document level entities.  What you see on the page is a widget annotation that acts as the UI for the field. A field can have many widgets on different pages. So, if there is more than one field widget, the field "page" property is an array of page numbers. But if there is only one field widget, then there the "page" property is a single number.         

 

So here is a function that determines if a field object is empty an on a particular page.

 

function IsEmptyOnPage(oFld, nPg)

{

     return (oFld.value == oFld.defaultValue) && ((typeof(oFld.page) == "number"?nPg == oFld.page:oFld.page.indexOf(nPg) >= 0))

}

 

Here's how it's used to hide all empty fields on the current page

 

var oFld;

for(var i=0;i<this.numFields;i++)

{

    oFld = this.getField(this.getNthFieldName(i));

    if(IsEmptyOnPage(oFld,this.pageNum))

        oFld.display = display.hidden;

}

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
New Participant
April 1, 2024

Thank you for the reply  and the walkthrough Thom, 

Do both of the above scripts get posted in the same mouse-up JS event window? Basically, copy and past the above in entirety less the "Here's how it's used to hide all empty fields on the current page"?

I have been away from this for a bit, i will try as you suggest soon. 

Thom Parker
Adobe Expert
April 1, 2024

Yes, both scripts can be placed in the same MouseUp script. However, this is not ideal.  The ideal solution is to put the function definition into a document level script.  But in this case there is no real need to separate the scripts. 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Amal.
Community Manager
Community Manager
March 22, 2024

Hi there

Hope you are doing well, and thanks for reaching out.

The workflow you are trying to achieve is might be possible using JavaScript. For more information, please check the help pages listed below:
https://acrobatusers.com/tutorials/javascript_console/
https://helpx.adobe.com/acrobat/using/applying-actions-scripts-pdfs.html

Hope it will help

Regards
Amal