9 Replies Latest reply on Mar 17, 2014 3:39 AM by a_rawson

    Auto-populate Field value From a File Picker Window

    a_rawson

      I have a PDF form with many fields where my users are required to enter the filename of documents. I am using Adobe Acrobat 9 Pro

       

      I've been trying to find a way to create a field Action that does the following:

       

      1. User clicks field
      2. File browser window opens
      3. User selects relevant file
      4. Filename and extension is placed into the field that was clicked

       

      Any tips or suggestions on how to implement this would be welcome.

       

      Additionally, if it is possible, could it support multiple field/documents in a single interactions. Example: User clicks field1 and selects 5 files. The files filenames are placed in fields1, field2, field3, field4, and field5 respectively.

       

       

      P.S.

       

      I am not trying to attach files to the PDF. I just want the filenames to be placed into the form.

        • 1. Re: Auto-populate Field value From a File Picker Window
          try67 MVP & Adobe Community Professional

          There's a built-in way of doing that... Simply create a text field and

          under Properties, Options tick the "Field is used for file selection" box

          (you will need to remove the "Check Spelling" box to make it available).

          Then add this code as the MouseUp event of the field:

          event.target.browseForFileToSubmit()

           

          Note that the field's value will only update when you exit it using this

          method.

           

          You can also use a button field to run this command on the text field, and

          it will then update immediately.

          1 person found this helpful
          • 2. Re: Auto-populate Field value From a File Picker Window
            a_rawson Level 1

            Thank you very much for the quick reply Gilad.

             

            I am fairly new to Acrobat and didn't even realize that was supported already. Is there a way to remove the filepath from the selection? By default it returns the entire path for the local machine.

            • 3. Re: Auto-populate Field value From a File Picker Window
              try67 MVP & Adobe Community Professional

              Not directly, but you can do it with another text field... Let's say the main field is called FilePath and you create another field where you want to have just the file's name, so you call it FileName.

              Enter this code as the custom calculation script of FileName:

               

              var filePath = this.getField("FilePath").value;

              event.value = filePath.substring(filePath.lastIndexOf("\\")+1);

              • 4. Re: Auto-populate Field value From a File Picker Window
                try67 MVP & Adobe Community Professional

                Of course, you can then hide the FilePath field...

                • 5. Re: Auto-populate Field value From a File Picker Window
                  a_rawson Level 1

                  Is it possible to use a button like you mentioned earlier to perform the entire operation without needing hidden fields? I am not sure my users would appreciate the filepaths being included in the forms.

                   

                  Also, the form has about 800 fields that I'd have to create a duplicate field for when using that method.

                  • 6. Re: Auto-populate Field value From a File Picker Window
                    try67 MVP & Adobe Community Professional

                    Are the files being selected PDF files, or other types? There's another method that can be used for file selection, but it only lists PDF files. It is possible (at least on Windows computers) to use it to select any file-type, but it's a bit tricky...
                    If not, using a hidden field is your only option. But you can reset if after copying the file-name to the other field, like this:

                     

                    this.getField("FilePath").browseForFileToSubmit();
                    var filePath = this.getField("FilePath").value;
                    this.getField("FileName").value = filePath.substring(filePath.lastIndexOf("\\")+1);
                    this.getField("FilePath").value = "";

                     

                    If you use this code (as the button's Mouse Up action), make sure you remove all other codes associated with the text fields. And you don't need to create a duplicate field for each file selection field, only one will do... It can even be placed in a doc-level function so you only have to call it from each button and have the code itself in a single location.

                    • 7. Re: Auto-populate Field value From a File Picker Window
                      a_rawson Level 1

                      That works perfectly. I thought I was going to have to use the calculated value on each field. The button implementation with a hidden field is much more friendly.

                       

                      Thank you very much Gilad.

                      • 8. Re: Auto-populate Field value From a File Picker Window
                        try67 MVP & Adobe Community Professional

                        Here's how to do it from a doc-level function. Add the following function under Tools - JavaScript - Document JavaScripts:

                         

                        function selectFile(fileNameField) {

                            this.getField("FilePath").browseForFileToSubmit();

                            var filePath = this.getField("FilePath").value;

                            this.getField(fileNameField).value = filePath.substring(filePath.lastIndexOf("\\")+1);

                            this.getField("FilePath").value = "";

                        }

                         

                        And then from your button all you have to do is call the function and specify the name of the "target" field, where the file-name should appear. For example:

                         

                        selectFile("FileName");

                        1 person found this helpful
                        • 9. Re: Auto-populate Field value From a File Picker Window
                          a_rawson Level 1

                          That's even more useful. Was slightly dreading needing to dig through all the code 800 times. (Now it's slight less dreadful haha)

                           

                          Thanks once again.