12 Replies Latest reply: Jun 5, 2015 1:53 PM by try67 RSS

    How can I add form field value to the file name in save as dialog box

    Owlbert358 Community Member

      I do not want the form to be saved automatically, just want the form to auto populate the "file name" only.

       

      A little background on the forms I want to use:  My company has 70 retail outlets, I'll use one of our pdf forms called an "Incident Report" as an example.  I would like for a store manager to be able to complete the form, then email the form to the main office (I already have javascript to add field values and form name to the email subject line), once the main office receives it, I want for them to be able to file the pdf electronically on our server.  We have mutliple forms that we use so I do not want any of the forms to automatically save anywhere, (at this time anyway) I just want the office personnel to be able to click "save as" (or whatever they will need to click) and the form automatically add certain field values from the pdf they have received, of which will be different each time the form is sent to the office (Date, store #, employee name etc.) in addition to the name of the form in the "File name" of the "Save As" dialog box.  The main office employees will decide into which server file the pdf should be saved.

       

      I'm using Acrobat 8 professional, the stores and office personnel use Adobe reader.

       

      One little note:  We currently print and file a lot of paper on a daily bases, as soon as I can get this to work, we are going green.

       

      Me and a lot of trees in this will really apprecitate any help you can give with this!  :-)

        • 1. Re: How can I add form field value to the file name in save as dialog box
          try67 ACP/MVPs

          You can't pre-populate the name that appears in the Save As dialog.
          What you can do, though, is save the file using a new name. However, this requires a script to be installed on the local machine of each user who's going to be using this file. If that's a possibility for you, we could discuss it further.

          • 2. Re: How can I add form field value to the file name in save as dialog box
            Owlbert358 Community Member

            If that is the only option, it certainly is possible.  I'm really new to script writing so the more detailed you can be, the better for me for sure.  Thanks!

            • 3. Re: How can I add form field value to the file name in save as dialog box
              FrogThomas99 Community Member

              You might want to take a look at the document "Developing Acrobat Applications Using JavaScript" (js_developer_guide.pdf) which is part of the Adobe Acrobat SDK, which can be downloaded here. Read the "Privileged versus non-privileged context" (p. 45ff.). You will find an example for "Executing privileged methods in a non-privileged context". Should be almost exactly what you are looking for.

               

              Small Outline: For security reasons ("the user always has to know what's going on") you are not allowed to use the "Doc.saveAs"-method without the user permission (--> in a non-privileged context). In order to reach the goal of a privileged context you can use a trusted function. You do this by creating a JavaScript file (*.js) in either the Application-JavaScript-Folder (default location on Windows systems: "%ProgramFiles%\Adobe\Acrobat 10.0\Acrobat\Javascripts") or the User-JavaScript-Folder (default location on Windows systems: "%AppData%\Adobe\Acrobat\10.0\JavaScripts"). Add the following content to the new file:

               

              myTrustedBrowseForDoc = app.trustedFunction( function ( oArgs ) {
                   app.beginPriv();
                        var myTrustedRetn = app.browseForDoc( oArgs );
                   app.endPriv();
                   return myTrustedRetn;
                   }
              );
              
              myTrustedSaveAs = app.trustedFunction( function ( doc, oArgs ) {
                   app.beginPriv();
                        var myTrustedRetn = doc.saveAs( oArgs );
                   app.endPriv();
                   return myTrustedRetn;
                   }
              );
              

               

              The developer guide actually wants you to add this content to the existing "config.js" file. I recommend creating a new file, since its easier to deploy in a network. Either way, every client/user who needs to be able to save documents this way, needs this JavaScript Code in his Application/User-JavaScript-Folder.

               

              Within the Acrobat Document, which you want to obtain a certain file name, you can now use the trusted functions "myTrustedBrowseForDoc" and "myTrustedSaveAs" to store the file. To call the trusted functions and deliver the file name you can either you use a form field (button) or you add a new menu item. Add the following JavaScript Action to the button/menu item and change "Roller Coaster" to the name of the field which contains the value which you want to become the new file name:

               

              var fileName = this.getField("Roller Coaster").valueAsString;
              
              try {
                   var oRetn = myTrustedBrowseForDoc({bSave: true, cFilenameInit: fileName + ".pdf"});
                   try {
                        myTrustedSaveAs(this, { cPath: oRetn.cPath, cFS:oRetn.cFS });
                   }
                   catch(e) {
                        console.println("Save not allowed, perhaps readonly.");
                   }
              }
              catch(e) {
              console.println("User cancelled Save As dialog box");
              }
              

               

              Good Luck!

              • 4. Re: How can I add form field value to the file name in save as dialog box
                GKaiseril ACP/MVPs

                The OP is using Acrobat 8, so the location of files as needed by Acrobat X will not work.

                 

                If you are using scripts with the  "privileged"  method in Acrobat X you should be aware of the JavaScript Changes for Acrobat X and Reader X, version 10.1.1 and User JavaScript Changes for 10.1.1 (Acrobat | Reader).

                 

                How to save a PDF with Acrobat JavaScript by Thom Parker provides a lot of information you need to know like the safe paths, how to get file folder locations for JavaScript through use of the JavaScript console, the universal path directory format, and other items you need to be aware of.

                • 5. Re: How can I add form field value to the file name in save as dialog box
                  rthompson@fullsail.com Community Member

                  I found this old forum post... and could use some help.

                  The code in FrogThomas99 works great. I wanted to create a version that saved the file as a .JPGs.  Or simply didn't lock drop-down in the save as window to PDF. I have done some research and found that you can add "com.adobe.acrobar.jpeg" using the cConvID part of the saveAs command.

                   

                  I can't figure out how to modify the code sample to make it work. Does it need to be passed to the trusted function?

                  • 6. Re: How can I add form field value to the file name in save as dialog box
                    try67 ACP/MVPs

                    You need to add the cConvID parameter as one of the arguments of the saveAs method. Also, you need to make sure that the file path ends with the correct file extension.

                    • 7. Re: How can I add form field value to the file name in save as dialog box
                      rthompson@fullsail.com Community Member

                      Wow- thanks for the speedy reply @try67. I am a bit of a newbee when it comes to JavaScript. I have changed the file name part see below. Where would I place the cConvID? Can you show me?

                       

                      Folder Level Code:

                      1. myTrustedBrowseForDoc = app.trustedFunction( function ( oArgs ) {
                      2.      app.beginPriv();
                      3.           var myTrustedRetn = app.browseForDoc( oArgs );
                      4.      app.endPriv();
                      5. return myTrustedRetn; 
                      6.      }
                      7. );
                      8. myTrustedSaveAs = app.trustedFunction( function ( doc, oArgs ) {
                      9.      app.beginPriv();
                      10.           var myTrustedRetn = doc.saveAs( oArgs );
                      11.      app.endPriv();
                      12. return myTrustedRetn; 
                      13.      }
                      14. );

                       

                      Button Code:

                      1. var fileName = this.getField("Roller Coaster").valueAsString; 
                      2. try { 
                      3.      var oRetn = myTrustedBrowseForDoc({bSave: true, cFilenameInit: fileName + ".jpg"}); 
                      4.      try { 
                      5.           myTrustedSaveAs(this, { cPath: oRetn.cPath, cFS:oRetn.cFS }); 
                      6.      }
                      7.      catch(e) { 
                      8.           console.println("Save not allowed, perhaps readonly."); 
                      9.      }
                      10. }
                      11. catch(e) { 
                      12. console.println("User cancelled Save As dialog box"); 
                      13. }
                      • 8. Re: How can I add form field value to the file name in save as dialog box
                        try67 ACP/MVPs

                        Change this line:

                        myTrustedSaveAs(this, { cPath: oRetn.cPath, cFS:oRetn.cFS }); 


                        To:

                        myTrustedSaveAs(this, { cPath: oRetn.cPath, cFS:oRetn.cFS, cConvID: "com.adobe.acrobat.jpeg"}); 

                        • 9. Re: How can I add form field value to the file name in save as dialog box
                          rthompson@fullsail.com Community Member

                          I tried that I am getting this error...

                           

                          oRetn is undefined

                          6:AcroForm:SaveAsPDF:Annot1:MouseUp:Action1Save not allowed, perhaps readonly.

                           

                          It works fine the other way, so I don't think it is the button script. Do I need to add something to to the folder level code so it will pass it in to it? It looks like the folder level function is only accepting 2 arguments (doc, oArg)-- which I think is the path and file name information.

                          • 10. Re: How can I add form field value to the file name in save as dialog box
                            try67 ACP/MVPs

                            It's a deeper issue. The browseForDoc method only lets you select PDF files. Even if you select a non-PDF file using it, it will stick a ".pdf" extension on it. You can't use it get the file name for the JPG image file. A different approach is required.

                            • 11. Re: How can I add form field value to the file name in save as dialog box
                              rthompson@fullsail.com Community Member

                              So that is getting the current file, not just getting the current file path. Is that what is forcing it to be PDF? Is there another way to get the file path then?

                              • 12. Re: How can I add form field value to the file name in save as dialog box
                                try67 ACP/MVPs

                                this.path will return the current file path.