• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
5

Trying to made a signature field read only unless all checkboxes in a PDF form are checked

Community Beginner ,
Mar 25, 2024 Mar 25, 2024

Copy link to clipboard

Copied

Good morning,

I have a PDF form which I've been tasked to make it so that the signature field only activates if all checkboxes in the form have been checked.

I had Chat GPT write a script which seems to work to make the signature field read only until all boxes are checked, but then when I try to apply the signature it gives me an error which states "The document cannot be signed in its current state.  Please save the document, close it, reopen it, and then attempt to sign again".

I tried that multiple times but I keep getting the same error message.

Any help would be greatly appreciated.

TOPICS
JavaScript , PDF forms

Views

133

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 25, 2024 Mar 25, 2024

Copy link to clipboard

Copied

Well, I think I figured it out.  Rather than putting in a document-level script or a custom calculation script in a hidden text field, I just had to add this as a Mouse-Up action to each of the 62 check boxes:

 

var checkbox1 = this.getField("CheckBox01").value;
var checkbox2 = this.getField("CheckBox02").value;
var checkbox3 = this.getField("CheckBox03").value;
var checkbox4 = this.getField("CheckBox04").value;
var checkbox5 = this.getField("CheckBox05").value;
var checkbox6 = this.getField("CheckBox06").value;
var checkbox7 = this.getField("CheckBox07").value;
var checkbox8 = this.getField("CheckBox08").value;
var checkbox9 = this.getField("CheckBox09").value;
var checkbox10 = this.getField("CheckBox10").value;
var checkbox11 = this.getField("CheckBox11").value;
var checkbox12 = this.getField("CheckBox12").value;
var checkbox13 = this.getField("CheckBox13").value;
var checkbox14 = this.getField("CheckBox14").value;
var checkbox15 = this.getField("CheckBox15").value;
var checkbox16 = this.getField("CheckBox16").value;
var checkbox17 = this.getField("CheckBox17").value;
var checkbox18 = this.getField("CheckBox18").value;
var checkbox19 = this.getField("CheckBox19").value;
var checkbox20 = this.getField("CheckBox20").value;
var checkbox21 = this.getField("CheckBox21").value;
var checkbox22 = this.getField("CheckBox22").value;
var checkbox23 = this.getField("CheckBox23").value;
var checkbox24 = this.getField("CheckBox24").value;
var checkbox25 = this.getField("CheckBox25").value;
var checkbox26 = this.getField("CheckBox26").value;
var checkbox27 = this.getField("CheckBox27").value;
var checkbox28 = this.getField("CheckBox28").value;
var checkbox29 = this.getField("CheckBox29").value;
var checkbox30 = this.getField("CheckBox30").value;
var checkbox31 = this.getField("CheckBox31").value;
var checkbox32 = this.getField("CheckBox32").value;
var checkbox33 = this.getField("CheckBox33").value;
var checkbox34 = this.getField("CheckBox34").value;
var checkbox35 = this.getField("CheckBox35").value;
var checkbox36 = this.getField("CheckBox36").value;
var checkbox37 = this.getField("CheckBox37").value;
var checkbox38 = this.getField("CheckBox38").value;
var checkbox39 = this.getField("CheckBox39").value;
var checkbox40 = this.getField("CheckBox40").value;
var checkbox41 = this.getField("CheckBox41").value;
var checkbox42 = this.getField("CheckBox42").value;
var checkbox43 = this.getField("CheckBox43").value;
var checkbox44 = this.getField("CheckBox44").value;
var checkbox45 = this.getField("CheckBox45").value;
var checkbox46 = this.getField("CheckBox46").value;
var checkbox47 = this.getField("CheckBox47").value;
var checkbox48 = this.getField("CheckBox48").value;
var checkbox49 = this.getField("CheckBox49").value;
var checkbox50 = this.getField("CheckBox50").value;
var checkbox51 = this.getField("CheckBox51").value;
var checkbox52 = this.getField("CheckBox52").value;
var checkbox53 = this.getField("CheckBox53").value;
var checkbox54 = this.getField("CheckBox54").value;
var checkbox55 = this.getField("CheckBox55").value;
var checkbox56 = this.getField("CheckBox56").value;
var checkbox57 = this.getField("CheckBox57").value;
var checkbox58 = this.getField("CheckBox58").value;
var checkbox59 = this.getField("CheckBox59").value;
var checkbox60 = this.getField("CheckBox60").value;
var checkbox61 = this.getField("CheckBox61").value;
var checkbox62 = this.getField("CheckBox62").value;

var signatureField = this.getField("SignatureField");

if (checkbox1 == "Off" || checkbox2 == "Off" || checkbox3 == "Off" || checkbox4 == "Off" || checkbox5 == "Off" || checkbox6 == "Off" || checkbox7 == "Off" || checkbox8 == "Off" || checkbox9 == "Off" || checkbox10 == "Off" || checkbox11 == "Off" || checkbox12 == "Off" || checkbox13 == "Off" || checkbox14 == "Off" || checkbox15 == "Off" || checkbox16 == "Off" || checkbox17 == "Off" || checkbox18 == "Off" || checkbox19 == "Off" || checkbox20 == "Off" || checkbox21 == "Off" || checkbox22 == "Off" || checkbox23 == "Off" || checkbox24 == "Off" || checkbox25 == "Off" || checkbox26 == "Off" || checkbox27 == "Off" || checkbox28 == "Off" || checkbox29 == "Off" || checkbox30 == "Off" || checkbox31 == "Off" || checkbox32 == "Off" || checkbox33 == "Off" || checkbox34 == "Off" || checkbox35 == "Off" || checkbox36 == "Off" || checkbox37 == "Off" || checkbox38 == "Off" || checkbox39 == "Off" || checkbox40 == "Off" || checkbox41 == "Off" || checkbox42 == "Off" || checkbox43 == "Off" || checkbox44 == "Off" || checkbox45 == "Off" || checkbox46 == "Off" || checkbox47 == "Off" || checkbox48 == "Off" || checkbox49 == "Off" || checkbox50 == "Off" || checkbox51 == "Off" || checkbox52 == "Off" || checkbox53 == "Off" || checkbox54 == "Off" || checkbox55 == "Off" || checkbox56 == "Off" || checkbox57 == "Off" || checkbox58 == "Off" || checkbox59 == "Off" || checkbox60 == "Off" || checkbox61 == "Off" || checkbox62 == "Off") {
signatureField.display = display.hidden;
} else {
signatureField.display = display.visible;
}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 25, 2024 Mar 25, 2024

Copy link to clipboard

Copied

First, ChatGPT is crap and will not write correct code for Acrobat scripts. 

 

Now, your script is very straight forward, but could be massively simplify in a coulple of different ways. 

1. Since the checkbox field names follow a pattern using progressive numbers, a loop can be used to collect and test the values.

 

2. The form can be made much better by using descriptive and grouped name patterns. Grouping fields allows them to be handled together. So if all the checkboxes were named like this.  "CheckGrp.Check1","CheckGrp.Check2","CheckGrp.Check3", etc. 

 

Then this script solves your issue:

 

this.getField("Signature1").display = (this.getField("CheckGrp").getArray().every(a=>a.value != "Off"))?display.visible:display.hidden;

 

On another note. It is an exceedingly bad strategy to place the entire script into the mouse up of every single checkbox.  

At the very least this code should be defined as a document level function. But it really is much better for this type of thing to use a calculation script in a hidden field. 

 

 

 

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 25, 2024 Mar 25, 2024

Copy link to clipboard

Copied

Haha, well, Chat GTP may be crap at writing scripts, but it's better than me!  I don't know how to write codes, it's still like trying to cast spells for me, I try and google the right ingredients and hope it works.

When I tried using loops and both when I tried doing the hidden calculation field or the document-level script I'd get that "The document cannot be signed in its current state.  Please save the document, close it, reopen it, and then attempt to sign again" error.

When I get a minute, I'll try your code and let you know what happens.  Thanks for your response!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 26, 2024 Mar 26, 2024

Copy link to clipboard

Copied

I ended up taking your advice, and it works great!  (also, much quicker to do than putting that action into every checkbox).

I've now been tasked to make a message (text field called 'Message') pop up when the user tries to fill in the "PrintedName" field  if not all of the checkboxes are checked, and 'Message' should disappear once they are all checked.  So far I'm using the following, but it's not quite working (user has to click back into the "PrintedName" after checking the remaining checkboxes in order to get the 'Message' to disappear.

// Function to toggle visibility of the Message field
function toggleMessageVisibility() {
var messageField = this.getField("Message"); // Replace "Message" with the actual name of your message field
var checkGrpField = this.getField("CheckGrp"); // Replace "CheckGrp" with the actual name of your checkbox group

if (checkGrpField.getArray().some(a => a.value == "Off")) {
messageField.display = display.visible;
} else {
messageField.display = display.hidden;
}
}

// Call the toggleMessageVisibility function when any checkbox is clicked
for (var i = 1; i <= 62; i++) {
this.getField("CheckBox" + i).setAction("MouseUp", "toggleMessageVisibility();");
}

// Call the toggleMessageVisibility function when the PDF is opened
toggleMessageVisibility();

// Call the toggleMessageVisibility function when the user interacts with the PrintedName field
this.getField("PrintedName").setAction("Focus", "toggleMessageVisibility();");

Thanks.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 27, 2024 Mar 27, 2024

Copy link to clipboard

Copied

LATEST

I'm assuming that all of that code is in a document level script?  And that the checkboxes are named using a "CheckGrp" prefix (or whatever group name you've choosen)? 

 

If so then there are two problems with this part of the script

// Call the toggleMessageVisibility function when any checkbox is clicked
for (var i = 1; i <= 62; i++) {
this.getField("CheckBox" + i).setAction("MouseUp", "toggleMessageVisibility();");
}

 

First, this code should only be run at design time from the console window. Once it's run the scripts are set and it doesn't need to be run again. 

Second, getField("CheckBox" + i) is not valid since the checkboxes are named using a group prefix. 

 

The next two lines are also problematic. 

This line is unnecesary, but not in error, since the form is saved with correct message display property.

 // Call the toggleMessageVisibility function when the PDF is opened
toggleMessageVisibility();

 

And this line has the same issue as the first problem line, i.e., it only needs to be run once at design time. 

// Call the toggleMessageVisibility function when the user interacts with the PrintedName field
this.getField("PrintedName").setAction("Focus", "toggleMessageVisibility();");

 

Fix these issue and give it another try. 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 25, 2024 Mar 25, 2024

Copy link to clipboard

Copied

You can use loop to simplify your script:

var signatureField = this.getField("SignatureField");
var allChecked = true;
for (var i=1; i<=62; i++) {
var fieldName = "CheckBox" + (i<10 ? "0" : "") + i;
var checkboxValue = this.getField(fieldName).value;
if (checkboxValue == "Off") {
allChecked = false;
break;}}
signatureField.display = allChecked ? display.visible : display.hidden;

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 26, 2024 Mar 26, 2024

Copy link to clipboard

Copied

The goal posts have shifted.

While the big clunky code I posted is working as far as hiding the signature field unless all checkboxes are checked, and showing it once they are all checked, the boss now wants a message to pop up when a user mouses up from the printed name text field ("PrintedName") if, and only if not all of the checkboxes are checked.  She wants it to be hidden until the user enters their printed name.

Now, I figured out how to get it so the message shows unless all checkboxes are checked and then gets hidden once they are all checked, that was fairly easy, but that is not the functionality she is looking for.

If anyone could help me out I'd be greatly appreciative.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines