15 Replies Latest reply on Oct 27, 2011 9:34 PM by dtm-1979

    How to set ALL fields in Acroform to readonly

    SI_MSD7 Level 2

      Hello, I'm looking for a way to set all the fields in a 2-page acrofrom to readonly.  I understand this can be achieved via the usage of a digital signature, but I rather not go that route for specific reasons. Futhermore, I understand setting access to individual fields can be achieved as follow:

       

      var f = this.getField("FieldName");

      f.readonly = true;

       

       

      Is there a way to specify an entire page or ALL pages in a doc? Something like:

       

      var f = this.getField("Page1");

      f.readonly = true;

       

      or

       

      var f = this.getField(All);

      f.readonly = true;

       

      Any help would be greatly appreciated.

       

      Thank you,

       

      S

        • 1. Re: How to set ALL fields in Acroform to readonly
          dtm-1979 Level 1

          This sounds a little similar to something I was trying to do a few weeks ago (here - http://forums.adobe.com/message/3961108#3961108 )

           

          Think this code might do the trick for you . . .

           

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

              var fname = this.getNthFieldName(i);

              var f = this.getField(fname);

                  f.readonly = true;

          }

          • 2. Re: How to set ALL fields in Acroform to readonly
            SI_MSD7 Level 2

            THANK YOU SOOOO MUCH. I've been trying to figure this out for the last 4 hours, and this code did exactly what i needed!

             

            One last thing. Do you know the JScript to control the precense of a field in Acrobat (i.e. hidden, visible, etc)? I'm sure it's simple, but I rarely script in Acrobat...I do most of my work in LC Designer.

             

            What i want to do is make the submit button hidden once I invoke the readonly script that yyou provided so that users know the current file is the readonly version (by not seeing the Submit button).  FYI, the script you provided is part of a larger group of script tasks.

             

            Thanks!

            • 3. Re: How to set ALL fields in Acroform to readonly
              SI_MSD7 Level 2

              Nevermind - I figured it out (found it in the Acrobat JS  Reference)

               

              var h = getField("Button1");

              h.display = display.hidden;

               

              Thanks again for your help!

               

              S

              • 4. Re: How to set ALL fields in Acroform to readonly
                dtm-1979 Level 1

                haha, i know exactly how you feel.  i've done it so many times, spending hours trying to figure something out.  glad i could help.

                 

                i think i get what you mean, but if this is wrong, you should be able to adapt it to suit . . .

                 

                 

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

                 

                    var fname = this.getNthFieldName(i);

                    var f = this.getField(fname);

                 

                    if (fname == "submit"){

                    f.display = display.hidden;

                    }

                 

                    else{

                    f.display = display.visible;

                    }

                }

                 

                 

                just saw that you replied, but maybe my script might help anyway!

                • 5. Re: How to set ALL fields in Acroform to readonly
                  SI_MSD7 Level 2

                  Thanks!  Actually what I did was use your inital script first, then used the second script I indicated in my second response directly below your script.  So, it sets all fields to "readonly", then hides the submit button. Works, perfect but now I only have one more bug to figure out.

                   

                  Basically, this is the entire picture... After completing the form, the user would click the submit button and a series of actions will take place:

                   

                   

                  1. Execute a menu item: SaveAs giving the user a chance to Save the current state of the form.

                   

                   

                  2. Once the form has been saved, the next action involves prepping and generating an email to send the form using the user's default email client:

                   

                   

                  var e = this.getField("ccAdd");  //There is a field on the form that enables the user to enter a CC: address, if applicable

                   

                   

                  var cCCAddr = e.value;

                   

                   

                     var cSubLine = "Subject goes Here"; //preformatted subject

                     var cBody = "File attached." // preformatted Body text

                   

                   

                     // Build the e-mail URL

                     var cEmailURL = "mailto:email@emailaddress.com?cc=" + cCCAddr

                                   + "&subject=" + cSubLine

                                   + "&body=" + cBody;

                   

                  //Before sending, set ALL form fields to "readonly"

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

                      var fname = this.getNthFieldName(i);

                      var f = this.getField(fname);

                          f.readonly = true;

                  }

                   

                  // hide submit display button, so that it doesn’t show on “readonly” version

                  var h = getField("Button1");

                  h.display = display.hidden;

                   

                  //Generate the outgoing email using the user’s default email client. Send attachment as a PDF

                     this.submitForm({cURL: cEmailURL, cSubmitAs:"PDF", cCharSet:"utf-8"});

                   

                   

                  3. Once the form has been emailed, another SaveAs menu item fires, thereby, allowing the user to save the "readonly" version.  So now, he has a clean "open version"  that he saved before Step #2, and a "readonly" version after step #2.

                   

                   

                   

                  The only thing that's causing an issue now is if the user presses Cancel after the Step #1  (where he/she is asked to save the form), the remaining scripts in-line continue to fire, alougth the "Cancel" button was pressed. 

                  SO, I need a script that terminates all remaining scripts (i.e. end process) if the user hits cancels during Step #1, Thereby preventing step 2 from starting.

                   

                   

                  Any ideas?

                   

                  S

                  • 6. Re: How to set ALL fields in Acroform to readonly
                    dtm-1979 Level 1

                    think we're going a little out of my depth here . . . i'm more a javascript hack and slash coder, than a real coder!

                     

                    how are you calling the Save As function?  via a 'trusted' function?  or via the app.execMenuItem function?

                     

                    if it's the latter, i'm not sure there's any way to catch the cancel button being pressed.

                     

                    i'm found this post which has some similar methods in save as actions:

                    http://forums.adobe.com/message/3982441#3982441

                    which links to one of Thom Parker's fantastic tutorials on the acrobatusers site.

                     

                    it would probably be possible to create a custom save dialog, but that would then involve setting up trusted functions for everyone who will use your form.

                    • 7. Re: How to set ALL fields in Acroform to readonly
                      SI_MSD7 Level 2

                      Well, I used the drop-down menu option in the button properties within Acrobat (see attached) - accessed on Action tab. Hence, it was addded the process for me. I assume what was added was the app.execMenuItem function.

                       

                      So bascially, I... 1)  inserted a menu item: SaveAs, then I 2) inserted by custom script, then I 3) inserted another SaveAs menu item. So, I was loking to see if there was a way for me to introduce/interject an "endprocess" function.  I've done this before in LiveCycel, but it's like learning to program all over again when scripting within Acrobat. And heck, I'm not even a programmer!

                       

                      But yeah...I know what you mean re: the trusted level script. I read about that a while back when I was tryign to figure out a way to generate a custom PDF [filename.pdf] based on one or more form field values (ie. a person's last name). When I read what it all entitled, I gave that up pretty quick. lol  More improtantly, it would not have been an idea solution as each person would need to have that script added to their computer in order for it to work.

                       

                      I guess I'm not going to stress it if I can't find an answer re: ending the SaveAs process.

                       

                      The form pretty much works as intended, but it could casue headaches if the user Cancels the initial SaveAs, as all the remaining processes fire - including generating the readonly version as asking them to save again. All in all, the user will need to be SURE they are ready to proceed before hitting submit.

                      • 8. Re: How to set ALL fields in Acroform to readonly
                        SI_MSD7 Level 2

                        snapshot.jpg

                         

                        image attached.

                         

                        S

                        • 9. Re: How to set ALL fields in Acroform to readonly
                          dtm-1979 Level 1

                          hmmm . . . i had a think about it, personally, i'd move the saveas commands into the script.  i think it keeps things a little neater, and if you make any changes to the core document, you only need to add one action, not three.

                           

                          try something like this, i think it should work.  if you prompt the user 'do you want to save...?' it''s unlikely that they'd hit yes in that dialog, and then cancel in the save dialog.  (i think, anyway.)  just replace the app.alert line in the function at the start with the email/readonly code you posted before.

                           

                          also, i wouldn't leave a function on a button javascript; it should be applied to the document level.

                           

                          //add all your custom code here
                          function finaliseForm() {
                              app.alert("This is a test box, replace it with your emailing script.");
                          }
                          
                          //prompt the user to continue save as
                          var saveEditable = app.alert({
                                      cMsg : "Do you want to save an editable version of this file before emailing?",
                                      cTitle : "Save As...",
                                      nIcon : 2,
                                      nType : 2
                                  });
                          if (saveEditable == 4) {
                              app.execMenuItem("SaveAs");
                              finaliseForm();
                              //and invoke a second save as
                                  var saveReadonly = app.alert({
                                              cMsg : "Do you want to also save a read-only this file?",
                                              cTitle : "Save As...",
                                              nIcon : 2,
                                              nType : 2
                                          });
                                  if (saveReadonly == 4) {
                                      app.execMenuItem("SaveAs");
                                  } else {}
                              } 
                          else {}
                          
                          • 10. Re: How to set ALL fields in Acroform to readonly
                            dtm-1979 Level 1

                            the other thing you could do, is to have app.alerts with yes/no for all three actions . . .

                             

                            • do you wish to save an editable form?
                            • do you wish to email the file now?
                            • do you wish to save the read-only form?

                             

                            that way, the script only executes as it needs to, and the user gets control over what the form is doing.

                             

                            just another thought.

                            • 11. Re: How to set ALL fields in Acroform to readonly
                              SI_MSD7 Level 2

                              Thanks! I will give this a try as soon as I get a chance!  I couldn't agree with you more re: adding everything to one script...I had actually thought about doing that, so this is a good reason to do it now.

                               

                              I won't pretend to act as though I fully understand what you meant by "also, i wouldn't leave a function on a button javascript; it should be applied to the document level." so can you please explain further?  What exactly does that entail re: applying it to the "document level" as oppose to a button? What would trigger the script to fire? Or, are you saying just the function segement of the script should be placed on the document level - sort of like variables in LC Designer?

                               

                               

                              And just so that i understand...when you mentioned  "This is a test box, replace it with your emailing script."   am I to copy my ENTIRE script (starting with: var e = this.getField("ccAdd");  and ending with: this.submitForm({cURL: cEmailURL, cSubmitAs:"PDF", cCharSet:"utf-8"});) and paste it within the " ", or replace the entire app.alert line with my ENTIRE script?   Or, am i to paste a specific section of my script?

                               

                              Sorry, I'm not a prpgrammer, but I do catch on fast once I "see" what's to be done.

                               

                              Thanks again in advance!

                              • 12. Re: How to set ALL fields in Acroform to readonly
                                SI_MSD7 Level 2

                                If I go the route with yes/no (wish makes good sense), will the remaining scripts (questions) fire in their respective order, despite the user pressing "no" to any of the previous selections?

                                 

                                Basically, it's fine the user chooses not to save during the first prompt, but that shouldn't prevent the email question from showing up next.  However, if he/she chooses not to email the doc at the moment, then it shouldn't perform the readonly script, and the subsequent SaveAs question shouldn't be asked.

                                 

                                I presume I have to design it that way...huh? lol

                                • 13. Re: How to set ALL fields in Acroform to readonly
                                  dtm-1979 Level 1

                                  quick reponses, but some of this is answered in the other thread

                                  I won't pretend to act as though I fully understand what you meant by "also, i wouldn't leave a function on a button javascript; it should be applied to the document level." so can you please explain further?  What exactly does that entail re: applying it to the "document level" as oppose to a button? What would trigger the script to fire? Or, are you saying just the function segement of the script should be placed on the document level - sort of like variables in LC Designer?

                                  the document level scripts can hold as much code as you want, and the buttons can just call that code to execute. i honestly can't remember how LC references these; it's been a while since i used it.  (from memorry, there are some good articles around about LC vs Acrobat JS differences though.)

                                   

                                  i do a lot of interactive pdfs, and just tend to assign everything from the document level with a host of functions.  here's a little test, set up a document and give it a couple of buttons, Button1 and Button2, then add the code below to the document level.  the doc level holds the functions and applies the actions to the buttons, all in one hit.  (of course, there's a smarter way to check that the button exists first, before applying the action. but we're just starting with the basics here!)

                                   

                                  function testFunction1() {
                                      app.alert("You clicked Button1");
                                  }
                                  
                                  function testFunction2() {
                                      app.alert("You clicked Button2.");
                                  }
                                  
                                  this.getField("Button1").setAction("MouseDown", "testFunction1();");
                                  this.getField("Button2").setAction("MouseDown", "testFunction2();");
                                  

                                   

                                  And just so that i understand...when you mentioned  "This is a test box, replace it with your emailing script."   am I to copy my ENTIRE script (starting with: var e = this.getField("ccAdd");  and ending with: this.submitForm({cURL: cEmailURL, cSubmitAs:"PDF", cCharSet:"utf-8"});) and paste it within the " ", or replace the entire app.alert line with my ENTIRE script?   Or, am i to paste a specific section of my script?

                                  you got it . . . kinda . . . i think!  replace the app.alert line with your script starting with var e... and ending with utf-8"});)

                                  1 person found this helpful
                                  • 14. Re: How to set ALL fields in Acroform to readonly
                                    SI_MSD7 Level 2

                                    Ohhhh, OK..now I'm following you.

                                     

                                    At first, i thought I was suppsoed to add "this.getField("Button1").setAction("MouseDown", "testFunction1();");" to Button1 and have the CLICK event of the button look for and find testFunction1(), and simialrly add "this.getField("Button2").setAction("MouseDown", "testFunction2();");" to Button 2. So my question would have been...does it scroll through every listed item in "Document Javascripts" until it finds the correct function, or would you have to specifically name the item on the list the name of the function so it goes straight there.

                                     

                                    BUT, what you're doing is adding everything to the doc level script and letting the function itself look for and find the Buttons. That way you can name the item in the Document Javascript list whatever you want since the relevant function/action is listed within the code.

                                     

                                    I tell you...I'm so used to having the button trigger the action vs. letting a function "listen for" the MouseDown action of a button, that this would take some getting used to...lol

                                     

                                    Nevertheless, I get it now.

                                     

                                    So, technically, I could also have the button point to a specific function name on the list as such:

                                     

                                    Button 1: testFunction1(); //look for and fire this specific function when clicked (MouseUp)

                                     

                                    Function list name: testFunction1 // lsited on Document JavaScript List

                                     

                                    Code listed inside testFunction1:

                                     

                                    function testFunction1() {
                                        app.alert("You clicked Button1");
                                    }

                                     

                                    Thanks man - you've been extremely helpful.

                                    • 15. Re: How to set ALL fields in Acroform to readonly
                                      dtm-1979 Level 1

                                      yep, you're on board with it!  only thing is (an we keep throwing this line around - i'm no coder!) i don't think it's a matter of 'listening' as such.  the function is there sitting in the background, doing nothing; and the button is the trigger that executes the function.

                                       

                                      this part of the script though:

                                           this.getField("Button2").setAction("MouseDown", "testFunction2();");

                                      is just a way of setting the button's action from the document level.  it's quicker an easier than right-clicking on a button, and setting the mouse down event manually to execute some JS code (inthis case, if you manually added it to the button, all you add is "testFunction2();".

                                       

                                      that make sense?  there's a lot in the JS API reference regarding  this, it's just a matter of finding it! running things from the doc level is a good way to run complex code, and assign functions that will be used frequenty in the document.  PM me if you have any specific questions, more than happy to help out another cowboy coder!

                                       

                                      besides, we've gone a bit off track from your original questions!