13 Replies Latest reply on Aug 7, 2015 5:19 AM by msingson

    How to password protect a field in a PDF form?

    msingson Level 1

      Hello!

       

      I'm a novice in Adobe Javascript so I'm really having a hard time trying to figure out what to do with the code that I already have.

       

      I was able to find a code that changes the field attribute to read-only ON BLUR. Here's the code:

       

      event.rc = true;

      if (!event.willCommit && event.value != "")

      {

      event.target.readonly = true;

      }

       

      But now I need a code that prompts the user for a password ON FOCUS if the field has input, so it changes the field back to being editable.

       

      I found this code but don't know how to edit it:

       

      var cResponse = app.response({

            cQuestion: "How are you today?", 
                 cTitle: "Your Health Status", 
                 cDefault: "Fine", 
                 cLabel: "Response:"
       });
       if (cResponse == null)
            app.alert("Thanks for trying anyway.");
       else
            app.alert("You responded, \""+cResponse+"\", to the health " 
                 + "question.",3);

       

      I figured it would start off with:

       

      event.rc = true;

      if (event.value != "")

      {

       

      }

       

      And then I can change the cTitle, cLabel, cDefault and app.alerts accordingly. But I dont know how to add the password and line of code to change the field attribute to readonly = false.

       

      Any and all help will be much appreciated!

        • 1. Re: How to password protect a field in a PDF form?
          gkaiseril MVP & Adobe Community Professional

          Passwords apply to the whole PDF only and cannot be applied using JavaScript.

          • 2. Re: How to password protect a field in a PDF form?
            George_Johnson MVP & Adobe Community Professional

            A problem is a field can't receive the focus if it read-only, so an On Focus script could never be triggered. You could place a button next to the field that when clicked will prompt for a password and unlock the field. You should also realize that this sort of thing cannot be considered secure, so an important question is: How secure does it need to be?

            • 3. Re: How to password protect a field in a PDF form?
              msingson Level 1

              I've actually tried this code:

               

              event.rc = true;

              if (event.value != "")

              {

              var cResponse = app.response

              ({

              cQuestion: "Please enter password",

              cTitle: "Quality Assurance",

              cDefault: "",

              cLabel: "Password:"

              });

               

              if (cResponse == 'hello');

              {

              event.target.readonly = false;

              }

              elseif (cResponse == null)

              {

              app.alert("Please contact Quality Assurance Manager.");

              }

              }

               

              And it works On Focus but only the response part. It won't execute the change in field attribute maybe because my coding isn't right.

               

              It doesn't have to be that secure. The people here at our office don't really know how to disable javascript or even know what javascript is.

              • 4. Re: How to password protect a field in a PDF form?
                George_Johnson MVP & Adobe Community Professional

                You'd have to change "elseif" to "else if". But after the field is set to read-only by the On Blur script, the On Focus code can't get triggered again because read-only fields cannot receive the focus.

                • 5. Re: How to password protect a field in a PDF form?
                  msingson Level 1

                  If I change "elseif" to "else if", I get a syntax error.

                   

                  I'm not sure what you mean but if I understand it correctly, On Focus is when the field is selected right?

                   

                  So if it's prompting me for a password when I select it, then obviously the On Focus script is being triggered. Am I missing something here?

                  • 6. Re: How to password protect a field in a PDF form?
                    George_Johnson MVP & Adobe Community Professional

                    There was another problem with the code. Try this:

                     

                    if (cResponse == 'hello') {

                       event.target.readonly = false;

                    } else if (cResponse == null) {

                    app.alert("Please contact Quality Assurance Manager.");

                    }

                     

                     

                     

                    The On Focus code will initially be triggered. but you also have an On Blur script that can set the field to read-only, right? If the on Blur script sets the field to read-only, the On Focus script can't subsequently be triggered since read-only fields cannot receive the focus.

                     

                    To see this, set a field to read-only and give it the following On Focus script:

                     

                    app.alert("Hello");

                     

                    This will never be triggered if the field is set to read-only.

                    • 7. Re: How to password protect a field in a PDF form?
                      msingson Level 1

                      I can forget about using the On Focus action if it's not ideal. Instead, can you please teach me how to do what you suggested earlier about having a button next to the field? Or maybe just one button for multiple fields? I would really appreciate it!

                      • 8. Re: How to password protect a field in a PDF form?
                        George_Johnson MVP & Adobe Community Professional

                        i'll be glad to help, but it would be helpful if you provided some background information on exactly what you're trying to accomplish and why. Details like the intended workflow, how the form will be used throughout that workflow, etc.

                        • 9. Re: How to password protect a field in a PDF form?
                          msingson Level 1

                          Data will be transmitted from a measuring device to a data logging software in the computer. In turn, the software will then transmit the data to a pdf form. (Is there a way to move the cursor to the next field below after the field has been filled in?)

                           

                          The field where the data is to be inputted has to be protected so no changes can be made unless a quality manager unlocks it to make necessary changes.

                           

                          However, not the entire pdf has to be protected. Only a certain number of fields.

                           

                          I just tried this code on a button with On Focus action:

                           

                          var desc = this.getField("InitialDOmgLRow1");

                           

                          if (desc.value != "")

                          {

                          var cResponse = app.response

                          ({

                          cQuestion: "Please enter password",

                          cTitle: "Quality Assurance",

                          bPassword: true,

                          cDefault: "",

                          cLabel: "Password:"

                          });

                           

                          if (cResponse == 'password'){

                          desc.readonly = false;

                          }

                          else if (cResponse == null)

                          {

                          app.alert("Please contact Quality Assurance Manager.");

                          }

                          else

                          app.alert("Incorrect Password");

                          }

                           

                          How can I apply this to multiple fields?

                          • 10. Re: How to password protect a field in a PDF form?
                            George_Johnson MVP & Adobe Community Professional

                            Don't use the On Focus event for a button. Use Mouse Up instead.

                             

                            What do you mean by "apply this to multiple fields"? Do you want to lock/unlock more than InitialDOmgLRow1 when the button is pressed?

                            • 11. Re: How to password protect a field in a PDF form?
                              msingson Level 1

                              Ok, I will change that right away

                               

                              Yes, I would like to have it where the manager can make necessary changes to whatever field with just one button. I dont have enough space in the pdf form to have more than one button.

                               

                              I've also set the button to be visible but not printable.

                               

                              I tried this:

                               

                              var desc1 = this.getField("InitialDOmgLRow1");

                              var desc2 = this.getField("InitialDOmgLRow2");

                              var desc3 = this.getField("InitialDOmgLRow3");

                              var aFields = new Array("InitialDOmgLRow1", "InitialDOmgLRow2", "InitialDOmgLRow3")

                              if (aFields.value != "")

                              {

                              var cResponse = app.response

                              ({

                              cQuestion: "Please enter password",

                              cTitle: "Quality Assurance",

                              bPassword: true,

                              cDefault: "",

                              cLabel: "Password:"

                              });

                               

                              if (cResponse == 'password'){

                              aFields.readonly = false;

                              }

                              else if (cResponse == null)

                              {

                              app.alert("Please contact Quality Assurance Manager.");

                              }

                              else

                              app.alert("Incorrect Password");

                              }

                               

                              It didn't work. Can you please tell me what's wrong with my code?

                              • 12. Re: How to password protect a field in a PDF form?
                                George_Johnson MVP & Adobe Community Professional

                                It's not clear to me under what conditions you want to prompt the user for a password, but if you rename the fields to the following:

                                 

                                InitialDOmgLRow.1, InitialDOmgLRow.2, InitialDOmgLRow.1

                                 

                                the following will do it if none of those fields are blank:

                                 

                                var oFields = this.getField("InitialDOmgLRow")  // Get the field object

                                var aFields = oFields.getArray();  // Get an array of field objects

                                 

                                if (aFields[0].valueAsString && aFields[1].valueAsString && aFields[2].valueAsString) {

                                 

                                    var cResponse = app.response({

                                        cQuestion: "Please enter password",

                                        cTitle: "Quality Assurance",

                                        bPassword: true,

                                        cDefault: "",

                                        cLabel: "Password:"

                                    });

                                 

                                    if (cResponse == 'password') {

                                        oFields.readonly = false;

                                    } else if (cResponse == null) {

                                        app.alert("Please contact Quality Assurance Manager.");

                                    } else {

                                        app.alert("Incorrect Password");

                                    }

                                 

                                }

                                • 13. Re: How to password protect a field in a PDF form?
                                  msingson Level 1

                                  Hello again!

                                   

                                  I'm sorry if I wasn't clear about it. The button will be a manager override button or something like that. The users will have to ask a manager to unlock the fields when they need to make changes, the manager will have to review the changes before he/she unlocks the fields of course. Thanks for the code, I'll give it try and let you know how it goes.