7 Replies Latest reply on Sep 7, 2010 7:51 PM by mebash

    Replace special characters in FDF

    mebash

      Hello,

       

      I have searched everywhere and cannot find the answer to this simple problem.  I can manipulate javascript in an HTML environment by using free scripts, but it's not so easy in a PDF form.  I am using Adobe Acrobat 9 Pro and I have a form that when filled out and submitted, generates and emails an FDF file.  I am using a server-side script called FormGenie to do this, and the script hates parentheses and some other special characters, they break the FDF file.  All I want to do is set up a document level Javascript that will check for and replace all instances of these special characters with something allowed like a hyphen- or just a space.

       

      I have tried:

       

      function clearText() {
           document.formname.fieldname.value=filterNum(document.formname.fieldname.value)

       

           function filterNum(str) {
                re = /\$|#|~|\%|\*|\^|\(|\)|\+|\=|\[|\]|\[|\}|\{|\;|\"|\<|\>|\?|\||\\|\!|\$/g;
                // remove special characters like "$" and "," etc...
                return str.replace(re, "-");
           }
      }

       

      and

       

      function clearText2(str) {

       

      stringName = stringName.replace(/\$|#|~|\%|\*|\^|\(|\)|\+|\=|\[|\]|\[|\}|\{|\;|\"|\<|\>|\?|\||\\|\!|\$ /g,'-');
      }

       

      I know these aren't right, but I have to be at least barking up the right tree.

      Can someone help?

       

      Thank you

        • 1. Re: Replace special characters in FDF
          try67 MVP & Adobe Community Professional

          Your filterNum method is ok, but clearText is written for a browser and will

          not work in a PDF, which has its own special JS objects and methods. I

          suggest you have a look at the JavaScript for Acrobat reference files:

          http://www.adobe.com/devnet/acrobat/javascript.html

           

          Basically, you need a function that loops over all of the form fields in

          your file and applies filterNum to their value property.

          • 2. Re: Replace special characters in FDF
            mebash Level 1

            I'm glad at least filterNum is on the right track.  The Javascript reference files aren't really much help, there isn't anything there about validating a text field and replacing unwanted characters.  I am not versed enough in Javascript to write this myself, I usually look for a prewritten script and modify it to suit my needs.

             

            I did about every search I could on this and found several scripts for HTML use, but none for PDF.  I wish there was a page somewhere that said here is  how to modify your Javascript for PDF use.

             

            Any clues are appreciated,

            Thanks

            • 3. Re: Replace special characters in FDF
              George_Johnson MVP & Adobe Community Professional

              Are you creating the form in Acrobat or LiveCycle Designer?


              In general, you would want to place a keystroke filtering script in each field's text field's Keystroke script, assuming that replacing something the user typed with something else wouldn't cause the user concern. An alternative would to be set up a script that scans the relevant fields just before the submit form action takes place and does the replace.

               

              For example,you can use the following custom Keystroke JavaScript for text fields created in Acrobat:

               

               

              if (!event.willCommit) {


                  event.change = event.change.replace(/[\$#~%\*\*\^\(\)\+=\[\]\{\};\"\<\>\?\|\\\!]/g, "-");

              }

               

               

              and it will replace those characters as the user enters data in a text field. That may not include all of the character you want replaced, but you should get the idea.

              • 4. Re: Replace special characters in FDF
                mebash Level 1

                I appreciate your help.  I am working in Acrobat 9 Pro.  I don't need the special characters to change that the user would see.  I would rather do it after they hit submit and check the fields and do the replace just before sending the form to me.

                 

                I did add the suggested script to a text field and tried typing the unwanted characters, they did not replace with the hyphen.  I tried both under the Validate and Actions tabs.  At any rate, I would much rather do a short sweet document level script that I don't have to add to every field on the form.  It frustrates me that there isn't any resource out there that will just tell me how to modify a standard Javascript to suit a PDF.

                 

                Thanks

                • 5. Re: Replace special characters in FDF
                  George_Johnson MVP & Adobe Community Professional

                  Regarding the script I posted, it is designed to be used as the custom Keystroke script for a text field, as mentioned. It will not work as intended if used elsewhere.

                   

                  Regarding the script you'd like to create, here is a general outline.

                   

                  1. Loop through the fields in question, get their field values, and replace using a statement similar to the one shown.

                  2. Execute a doc.submitForm JavaScript statement after this loop is done.

                   

                  Looping through fields, getting their value, and submitting a form is documented and discussed in the Acrobat JavaScript reference. Give it a shot and post again if you have any problems.

                  1 person found this helpful
                  • 6. Re: Replace special characters in FDF
                    mebash Level 1

                    Hi George,

                     

                    Ah I get it now.  I wasn't putting the script you wrote in  the Format tab's Custom Keystroke window.  I did and it works GREAT.  I'm going to play around and see if I still can't put together a script that will perform the replace of all the field values on submit.  I did see the script components you mention in the reference files.  I'm guessing this script would be the action performed when the user clicks the Submit button, correct?

                     

                    Even if I can't figure it out, the challenge of which will be good for me, I can still use the script I have from you.

                     

                    Thanks SO much for your help and I'll let you know how I do.

                    Beth

                     

                    PS:  Is it just me, or is there nothing in the API reference for Acrobat 9??  There is a bunch of stuff about all the great information it is supposed to contain, but no actual information.  The version 8 reference has more, so I guess that's what I'll use??

                    • 7. Re: Replace special characters in FDF
                      mebash Level 1

                      OK, here is my first stab at putting together this  script from searching the API reference.

                       

                      To loop  through the text fields and get their values, I have:

                       

                      for  ( var i = 0; i < this.numFields; i++) {
                      var fname = this.getNthFieldName(i);
                      if ( fname.type = "text" ) f.value;   [Not sure if "f.value" is correct or legal here]
                      }

                       

                      If this is even getting close, where I get stuck  is how to get the original script to take those values and replace any unwanted characters found.

                       

                      if (!event.willCommit) {

                       

                          event.change = event.change.replace(/[\$\#\~\%\*\^\(\)\+\=\[\]\{\}\;\"\<\>\?\|\\\!]/g, "-");

                       

                      }

                       

                      I just need to put this together correctly and I don't know how.

                       

                      I have the form submission part down, that was already done, I just need to add the custom "Run a javascript" item above it so that is executed first.

                       

                      Need a little guidance,

                      Thanks