24 Replies Latest reply on Jul 8, 2012 2:06 PM by TBNeilson

    Filename from field

    AccessII

      I have seen in past discussions about the difficluty and even impossibility of creating a SAVE AS filename from a field value in an Adobe form.  These discussions were posted back in 2006-2008.

       

      I am hoping that someone knows a relatively simple way to do this (if it's possible in Acrobat 10)  I am relatively new to Adobe and Javascripts.

       

      All I want to do is have a button that does a SAVE AS (I can do this), but pull the filename from the "NAME" field in the form and add the .pdf extension and place it in a particular directory on the server.

       

      Seems like this would be possible...  can anyone point me in the right direction?

       

      Thanks a million!!

       

      DMD    

        • 1. Re: Filename from field
          George_Johnson MVP & Adobe Community Professional

          You just have to build a path string and pass it to the saveAs method. Something like:

           

          // Get the field value
          var fn = getField("filename").valueAsString;
          
          // Specify the folder
          var fldr = "/c/folder1/folder2/";
          
          // Determine the full path
          var fp = fldr + fn;
          
          // Save the file
          mySaveAsFunction(fp);
          

           

          Replace "filename" with the name of the field containing the file name. The "mySaveAsFunction" shold be a function defined in a folder-level JavaScript file, something like that shown in example 1 here: http://livedocs.adobe.com/acrobat_sdk/9.1/Acrobat9_1_HTMLHelp/JS_API_AcroJS.88.168.html

           

          You should add more code to validate that "fn" isn't blank of otherwise invalid, but you should get the idea.

          • 2. Re: Filename from field
            AccessII Level 1

            George,

             

            OK, I did not follow you on all of the instructions, but I gave it a try.

             

            I am not sure where to put the code that you gave me.  I placed your code as a MOUSE UP action of my save button.

             

            I used the code from example 1 in a document level JS.

             

            Nothing happens when I click save.

             

            Would you mind step-by-step instructions???  Again, I am a newbie with both Acrobat and JS... but willing to try anything.

             

            Can I post the form somewhere for you to look at?

             

            MUCH APPRECIATED,

             

            DMD

            • 3. Re: Filename from field
              George_Johnson MVP & Adobe Community Professional

              The code from example 1 needs to go in a folder-level JavaScript file. For another resource, see: http://acrobatusers.com/tutorials/how-save-pdf-acrobat-javascript

               

              For more information on folder-level JavaScripts, see: http://acrobatusers.com/tutorials/2006/folder_level_scripts

              • 4. Re: Filename from field
                AccessII Level 1

                George,

                 

                Thanks for your patience.  I think I am getting close on this (but I could be way off, not sure).

                 

                I think that my problem may lie in the blue highlighted line(s) below.  I am not so sure.

                 

                I do not get any errors, but I do not get a saved file either.

                 

                I hope I am not being a pain in your side, any help is appreciated.

                 

                Thanks,

                Dan

                 

                 

                This is what I have

                 

                 

                1. This is the folder level JS that I have saved in c:\program files\adobe\adobe 10.0\adobe\javascripts\mySaveAs.js

                 

                mySaveAs = app.trustPropagatorFunction(function(fn,fldr)
                {
                     app.beginPriv();

                          doc.saveAs(fldr);
                     app.endPriv();
                })
                myTrustedSpecialTaskFunc = app.trustedFunction(function(fn,fldr)
                {
                     // Privileged and/or non-privileged code above
                     app.beginPriv();
                          mySaveAs(fp);
                     app.endPriv();
                });

                 

                 

                2. I have this in a Mouse Up, Run a Javascript action of a button (code that you provided)

                 

                // Get the field value
                var fn = getField("PatientName").valueAsString;

                // Specify the folder
                var fldr = "/c/test/";

                // Determine the full path
                var fp = fldr + fn;

                // Save the file
                mySaveAs(fp)
                ;

                • 5. Re: Filename from field
                  George_Johnson MVP & Adobe Community Professional

                  You're close. You need to call the TrustedSpecialTaskFunc from your code in the document, and change the folder-level script to the following:

                   

                  mySaveAs = app.trustPropagatorFunction(function(path)
                  {
                       app.beginPriv();
                           doc.saveAs(path);
                       app.endPriv();
                  });
                  
                  myTrustedSpecialTaskFunc = app.trustedFunction(function(file_path)
                  {
                       // Privileged and/or non-privileged code above
                       app.beginPriv();
                            mySaveAs(file_path);
                       app.endPriv();
                  });
                  

                   

                  You would then call it like:

                   

                  // Save the file
                  myTrustedSpecialTaskFunc(fp);
                  
                  • 6. Re: Filename from field
                    AccessII Level 1

                    George,

                     

                    I made the changes that you sent.  Question - Does the folder level JS need to have a specific filename?

                     

                    Right now I have it saved like this;

                    c:\program files\adobe\adobe 10.0\adobe\Javascripts\mySpecialTrustedTaskFunc.js

                     

                    I get the following error from the built-in debugger in Acrobat 10.0

                     

                     

                    Acrobat EScript Built-in Functions Version 10.0
                    Acrobat SOAP 10.0

                     

                    ReferenceError: doc is not defined
                    4:Field:Mouse Up

                     

                    All current coding shown below.

                     

                    Thanks AGAIN,

                    Dan

                     

                     

                    This is what I have as of now;

                     

                    1. Folder leve JS saved as noted above;

                     

                    mySaveAs = app.trustPropagatorFunction(function(path)
                    {
                         app.beginPriv();
                             doc.saveAs(path);
                         app.endPriv();
                    });

                    myTrustedSpecialTaskFunc = app.trustedFunction(function(file_path)
                    {
                         // Privileged and/or non-privileged code above
                         app.beginPriv();
                              mySaveAs(file_path);
                         app.endPriv();
                    });

                     

                    2. Mouse Up, Run a Javascript

                     

                    // Get the field value
                    var fn = getField("PatientName").valueAsString;

                    // Specify the folder
                    var fldr = "/c/test/";

                    // Determine the full path
                    var fp = fldr + fn;

                    // Save the file
                    myTrustedSpecialTaskFunc(fp);

                    • 7. Re: Filename from field
                      George_Johnson MVP & Adobe Community Professional

                      Sorry about that, you need to pass a document object:

                       

                      mySaveAs = app.trustPropagatorFunction(function(doc, path)
                      {
                           app.beginPriv();
                               doc.saveAs(path);
                           app.endPriv();
                      });
                      
                      myTrustedSpecialTaskFunc = app.trustedFunction(function(doc, file_path)
                      {
                           // Privileged and/or non-privileged code above
                           app.beginPriv();
                                mySaveAs(doc, file_path);
                           app.endPriv();
                      });
                      
                      

                       

                       

                      And call it like this:

                       

                      // Save the file
                      myTrustedSpecialTaskFunc(this, fp);
                      

                       

                      You can name the JavaScript file whatever you want, as long as it has the ".js" extension.

                      • 8. Re: Filename from field
                        AccessII Level 1

                        Now I get the following error in the debugger.

                         

                        Acrobat EScript Built-in Functions Version 10.0
                        Acrobat SOAP 10.0

                        UnsupportedValueError: Value is unsupported. ===> Parameter cPath.
                        Doc.saveAs:4:Field Save:Mouse Up

                        • 9. Re: Filename from field
                          George_Johnson MVP & Adobe Community Professional

                          You might want to check waht the value of fp is. If the text field does not include the ".pdf" extension, you will have to append it in your code like:

                           

                          var fp = fldr + fn + ".pdf";
                          

                           

                          You should also check to make sure that the value of the file name text field represents a valid file name. For example, it isn't blank, does not contain any invalid characters, etc.

                          • 10. Re: Filename from field
                            AccessII Level 1

                            BINGO!!!!!

                             

                            THANK YOU!!!!

                             

                            It's working perfectly as long as I am using Acrobat 10.  I tried it in Reader X and it is not saving the file.

                             

                            I thought that if I saved it as a READER EXTENDED PDF (enable additional features) that it would allow for the save feature / privledged functions.

                             

                            Can you offer advice there, or should I open another thread?

                             

                            Thanks againg George!

                             

                            Dan

                            • 11. Re: Filename from field
                              George_Johnson MVP & Adobe Community Professional

                              You need to place the JavaScript file in the appropriate location for Reader for it to work. Have you done that?

                               

                              If it needs to work with Reader and you are unable to install a folder-level JavaScript on their system, you will have to save the PDF as a certified document and get the users to trust your digital certificate (derived from the digital signature that you'll have to include) to allow privileged JavaScript. The code would be different since you don't need to include the trusted function and beginPriv/endPriv stuff.

                              • 12. Re: Filename from field
                                AccessII Level 1

                                I had forgotten to place the folder-level JS in the Reader location.

                                 

                                I have now done that.  I went a step further and created an app alert in the same folder to let me know that I was using the correct folder.

                                 

                                I enabled the debugger console in reader and I am getting this error when I try to save the file using the button.

                                 

                                Acrobat EScript Built-in Functions Version 10.0
                                Acrobat SOAP 10.0

                                NotAllowedError: Security settings prevent access to this property or method.
                                Doc.saveAs:4:Field Save:Mouse Up

                                 

                                I looked in the security setting and nothing stood out that I should obviously change.  I googled the error and did not get much information.

                                 

                                Any ideas?

                                 

                                THANKS!

                                 

                                Dan

                                • 13. Re: Filename from field
                                  George_Johnson MVP & Adobe Community Professional

                                  Is the document Reader-enabled?

                                  • 14. Re: Filename from field
                                    AccessII Level 1

                                    I believe so, but I am not 100% certain where I should be checking that?

                                    ???

                                     

                                    Dan

                                    • 15. Re: Filename from field
                                      George_Johnson MVP & Adobe Community Professional

                                      With the file opened in Acrobat, select "File > Properties > Security" and you'll see a note at the bottom indicating the document has usage rights applied.

                                       

                                      Also, make sure the folder you're specifying exists and that the user has rights to save to that location. There is also the idea of safe paths (e.g., root directory, Windows directory, etc.) that Acrobat will not allow writing files to. What folder are you attempting to save to?

                                      • 16. Re: Filename from field
                                        George_Johnson MVP & Adobe Community Professional

                                        To check if usage rights are applied through code, you can execute the following in the interactive JavaScript console:

                                         

                                        // See if usage rights are applied
                                        appRightsValidate({nValidateOption: 1});
                                        

                                         

                                        It will return false if no rights, and true if there are.

                                        • 17. Re: Filename from field
                                          AccessII Level 1

                                          George,

                                          I think I should document exactly what is happening, so here goes…

                                          FROM ACROBAT X PRO - The form is working and saving via the mouse up JavaScript action coupled with the folder-level JS that contains;

                                          myTrustedSpecialTaskFunc   and  mySaveAs = app.trustPropagatorFunction

                                           

                                          1. In order to save it for READER users, I do a FILE, SAVE AS, READER EXTENDED PDF, ENABLE ADDITIONAL FEATURES.
                                          2. I copy the folder level JS over to the proper JS folder associated with READER
                                          3. I can open the file in READER and edit the form fields. 
                                          4. When I try to save the file using the Custom Save As button that works in ACROBAT, I get this error;

                                          NotAllowedError: Security settings prevent access to this property or method.

                                          Doc.saveAs:4:Field Save:Mouse Up

                                          NOTES:

                                          ·         I know that I am using the correct JavaScript folder for READER because I placed this JS code in the folder and it DOES display when I open READER-  app.alert("It works!");

                                          ·         I am saving the file in both instances to c:\temp\filename.pdf

                                          ·         When I distribute the form, I will be able to place the folder-level JS on the users machine.

                                           

                                          Per your last post, when I open the file that I have saved as above for READER in ACROBAT and go to file>properties>security I have the following displayed;

                                          Security: No Security

                                          Can be opened by: All versions of Acrobat

                                          Document Restrictions Summary

                                          Printing: Allowed

                                          Changing the document: Not allowed*

                                          Document assembly: Not allowed*

                                          Content copying: Allowed

                                          Content copying for accessibility: Allowed

                                          Page extraction: Allowed

                                          Commenting: Allowed

                                          Filling of form Fields: Allowed

                                          Signing: Allowed

                                          Creation of template pages: Not allowed*

                                          *This document restricts some Acrobat features in order to allow for extended features in Adobe Reader

                                           

                                          At this point, I am a a loss as to what is causing the error.  I hope this documentation helps you see where my problem is.

                                           

                                          Thanks,

                                          Dan

                                          • 18. Re: Filename from field
                                            George_Johnson MVP & Adobe Community Professional

                                            Dan,

                                             

                                            OK, with Reader 10 there's another thing to account for, which is Protected Mode.

                                             

                                            As a test, disable it in Reader's preferences to see if your code works.

                                             

                                            It probably will, but it would be a mistake to leave it disabled since you can instead configure Reader to allow writing to a specific directory by making a Windows registry edit and creating a custom policy, as documented in section 2.2.6 of the following documentation: http://learn.adobe.com/wiki/download/attachments/64389123/AcrobatApplicationSecurity.pdf?v ersion=1

                                             

                                            You would have to do this for every Reader user. Please report back to let us know if it works.

                                            • 19. Re: Filename from field
                                              AccessII Level 1

                                              YES!!!

                                               

                                              Unchecking the protected mode did the trick.  Everything is working in READER now.

                                               

                                              I have since had the opportunity to test it in READER 9 and it works as designed there too (of course there is no protected mode to deal with in ver 9).

                                               

                                              I cannot express my gratitude enough for your patience and willingness to help me.  I learned quite a bit though the process!!

                                               

                                              Regards,

                                              Dan

                                              • 20. Re: Filename from field
                                                AccessII Level 1

                                                George ROCKS!!

                                                • 21. Re: Filename from field
                                                  jamcwh07

                                                  Hey I have gone through the steps in this and cant seem to get it to work.  I have the folder specific

                                                  coding saved as java script in the user defined folder specific coding but still cannot get it to work.  Any ideas?

                                                  • 22. Re: Filename from field
                                                    TBNeilson

                                                    I have been following this thread closely, as I am trying to accomplish the same thing. The issue I have run into is that whenever I try and save the document via the button I have created, I get an error in the debugger console saying that mySpecialTrustedFunc is not defined. I assume that this means I have made some mistake or need to use a different avenue for installing my folder level javascript. At which point should I be sure to define mySpecialTrustedFunc?

                                                     

                                                    Thanks for your help,

                                                    -Tom

                                                    • 23. Re: Filename from field
                                                      George_Johnson MVP & Adobe Community Professional

                                                      If you get that message, you either didn't successfully install the folder-level JavaScript file correctly, the function is named something other than what you're using to call it, or you didn't restart Acrobat after installing the JavaScript file.

                                                      • 24. Re: Filename from field
                                                        TBNeilson Level 1

                                                        Thanks George, it turned out it was just a simple matter of restarting Acrobat. I have one more question, however, that I was hoping you might be willing to provide some guidance on. I have everything going correctly at this point (as far as I can tell), but when I attempt to use the save button I have created, I get this error in the debugger

                                                         

                                                        RaiseError: The file may be read-only, or another user may have it open. Please save the document with a different name or in a different folder.

                                                        Doc.saveAs:4:AcroForm:Save:Annot1:MouseEnter:Action1

                                                        ===> The file may be read-only, or another user may have it open. Please save the document with a different name or in a different folder.

                                                         

                                                        I have tried saving it in a different folder, as well as with different names, but it always returns this error. The same is true when I attempt to use the button in Reader as a Reader enabled file. I have the feeling that it is something that I have passed over in the settings or in the code, but I don't really have a firm base for where to start trouble shooting. Just to give you some background, in case these things matter, I am using Acrobat X, and Reader 10 on a Mac OS X.

                                                         

                                                        Thanks,

                                                        -Tom