16 Replies Latest reply on Nov 7, 2016 2:11 PM by try67

    Javascript to Embed file attachment

    tracyy42935226

      I found a Javascript on the forum that allows me to have a form user attach a file in Adobe Reader to a PDF form.  The script is below and works great...as long as I am viewing the saved document on a computer.  I have discovered that Adobe Reader Mobile doesn't recognize file attachments added as annotations (only embedded files).  Can someone help me modify this script so that the file is actually embedded and not added as an annotation?  My follow-up question is the javascript to detach the file with a button.  I am using Adobe Acrobat DC to program the form.  Thank you so much for your help!!!

       

      The way this is programmed is that on the button itself with a Mouse Up action, the following Javascript is run:

       

       

      if (app.viewerVersion < 11) {

          import_pre_11();

      } else {

          import_11();

      }

       

      Then there is a document level javascript for attaching the file that is programmed like this:

       

      // Initialize attachment number

      attachment_num = 1;

       

      // Initial location of file attachment icon

      // The x value is incremented below

      var oFAP = {x_init: -72, x: -72, y: -72};  // Below lower-left corner of the page

       

      function import_pre_11() {

       

          if (app.viewerType === "Reader") {

       

              app.alert({

                  cMsg: "You must user Reader version 11 or later to attach files to this form.",

                  cTitle: "Attach File Error",

                  nIcon: 3,

                  nType: 0

              });

         

              return;

          }

       

          // Prompt user to import a file

          app.alert({

              cMsg: "Click the OK button to select a file from your system.",

              cTitle: "Attach File",

              nIcon: 3,

              nType: 0

          });

         

          try {

         

              var rc = this.importDataObject("Attachment" + attachment_num);

              if (rc) {

                  attachment_num += 1;

                  app.alert({

                      cMsg: "Attachment successful.\r\rOpen the Attachments panel to access the attached file(s).",

                      cTitle: "Attachment Successful",

                      nIcon: 3,

                      nType: 0

                  });

                 

              } else {

                  app.alert({

                      cMsg: "Attachment cancelled.",

                      cTitle: "Attachment Cancelled",

                      nIcon: 3,

                      nType: 0

                  });

              }

             

             

          } catch (e) {

              app.alert({

                  cMsg: "Could not attach file.",

                  cTitle: "Could not attach file",

                  nIcon: 3,

                  nType: 0

              });

          }

       

      }

       

      function import_11() {

       

          try {

       

              var annot = addAnnot({

                  page: event.target.page,

                  type: "FileAttachment",

                  author: "Form user",

                  name: "File Attachment",

                  point: [oFAP.x, oFAP.y],

                  contents: "File attachment on: " + util.printd("yyyy/mm/dd HH:MM:ss", new Date()),

                  attachIcon: "Paperclip"

              });

       

              annot.cAttachmentPath; // Prompt user to select a file to attach

             

              oFAP.x += 18;  // Increment the x location for the icon

             

             

          } catch (e) {

       

              app.alert({

                  cMsg: "Could not attach file.\r\rPlease report this error.",

                  cTitle: "File attachment error",

                  nIcon: 3,

                  nType: 0

              });

       

          }

         

      }

        • 1. Re: Javascript to Embed file attachment
          George_Johnson MVP & Adobe Community Professional

          Reader can't add embedded file attachments unless the document has been Reader-enabled with LiveCycle Reader Extensions and the embedded file attachments usage right has been applied.

          • 2. Re: Javascript to Embed file attachment
            try67 MVP & Adobe Community Professional

            Actually, using Reader XI or higher you can attach a file to the PDF using the Attach File commenting tool.

            This is what the script above is using and it should work, if used correctly.

            • 3. Re: Javascript to Embed file attachment
              tracyy42935226 Level 1

              I'm using Adobe Reader DC.  The button works and attaches the file but when I go to view it on an iPad, there is no attachment showing.  The only way I can get an attachment to show on the iPad is to use the actual Add a New Attachment paperclip which I can only find in Adobe Acrobat DC.  Searching the web, I found somewhere that said Reader Mobile could only see attachments that were embedded.  I'm trying to figure out a way so that the user can attach the file in Reader and then the end user can see the finished form with attachment in Reader on the iPad.  Is this possible?  I have a large number of files and could run an action on them if that was necessary...that is what I am having to do so the layered file can be viewed on the iPad so adding another action step wouldn't cause additional work if that is what needs to happen. 

              • 4. Re: Javascript to Embed file attachment
                try67 MVP & Adobe Community Professional

                Most scripts, including this one, are not going to work on mobile devices.

                 

                On Sun, Feb 21, 2016 at 9:58 PM, tracyy42935226 <forums_noreply@adobe.com>

                • 5. Re: Javascript to Embed file attachment
                  tracyy42935226 Level 1

                  I'm not sure that I asked this question clearly enough (or rather, try67 I KNOW if there is a way, you'll know it so I'm just going to clarify my question).  I do not need the 'attach document' button to work on the iPad...the user will attach the document on a desktop computer using Reader DC.  What I need is for that attachment to be viewable on an iPad. 

                   

                  When I use the above script to attach a document, it isn't showing up on the iPad.  However, if I use Adobe Acrobat DC to attach the file by manually pushing the paperclip Add Attachment button, then the iPad will show the attachment.

                   

                  For example, my form has two attachments- 1 added with the script above (Attachment A) and 1 added with paperclip (Attachment B).  In Adobe Acrobat Mobile, it only shows that the form has one attachment (Attachment B)...Attachment A is no where to be seen.

                   

                  I'm deducting that somehow this paperclip attaching process is embedding the attachment differently than the script...I'm trying to figure out what that difference is so I can add that to the javascript.    Is this still impossible?

                  • 6. Re: Javascript to Embed file attachment
                    gkaiseril MVP & Adobe Community Professional

                    Can you name any computer that uses the chip set of the iPone 5 or 6?

                     

                    Mobile devices are very different from a full computer and they do now have all needed resources to perform the tasks that user computers do. They also have a very different file system so any directory reference might not be same as in Windows or a computer iOS.

                     

                    You mobile device does not support all the services needed by your script.

                    • 7. Re: Javascript to Embed file attachment
                      George_Johnson MVP & Adobe Community Professional

                      I'm the one that created that script, and I programmed it the way I did because Reader cannot add the type of file attachments (embedded, as opposed to comment) you want unless the document has been Reader-enabled, as I explained above. So what you want to do doesn't work with Reader since Reader can't (normally) add embedded files.

                      • 8. Re: Javascript to Embed file attachment
                        George_Johnson MVP & Adobe Community Professional

                        I know, I wrote it. Reader cannot add embedded files, even manually, unless the file is Reader-enabled with the embedded file attachment usage right.

                        • 9. Re: Javascript to Embed file attachment
                          tracyy42935226 Level 1

                          Thank you George_Johnson for the clarification.  Does the form itself AND the attachment have to be altered to allow for the attachment to be embedded?  It may be worth it to me to explore LiveCycle Reader Extensions if only the form itself must be altered (I won't have access to change the attachments themselves prior to the user needing to attach them). 

                          • 10. Re: Javascript to Embed file attachment
                            Bernd Alheit Adobe Community Professional & MVP

                            The form must have the usage rights.

                            • 11. Re: Javascript to Embed file attachment
                              dr-joachim-weber

                              Thank you for publishing this script. My question is: how can you link the script to a specified file on the computer / network??

                              • 12. Re: Javascript to Embed file attachment
                                George_Johnson MVP & Adobe Community Professional

                                I'm not sure what you mean by "link the script to a specified file". Can you elaborate on how you want it to work?

                                • 13. Re: Javascript to Embed file attachment
                                  dr-joachim-weber Level 1

                                  Thanks for the quick reply. I want to connect a folder with a collection of files ( pdf, jpg etc) coming with the form. By filling the form it should be possible to attach a file from the folder by chosing f.e. a (radio) button. just like your script but a "given/set" file per button. I want to create a patient inform file: a info sheet should be attached for each desease the patient chooses. Thanks for your help!

                                  • 14. Re: Javascript to Embed file attachment
                                    George_Johnson MVP & Adobe Community Professional

                                    That's possible with Acrobat, but the code that attaches the files would have to be included in a folder-level JavaScript file. It's only possible with Reader if the document is Reader-enabled to include the Embed File Attachments usage right, something Acrobat cannot do.

                                    • 15. Re: Javascript to Embed file attachment
                                      abrittis

                                      See below... Once you create the annotation of type "FileAttachment", you can "link" the newly created object to a file by using the "magical" Annotation.cAttachmentPath.  This will launch a file-selector and prompt the user to select a file from their computer. 

                                       

                                      I for one am still confused as to how this works AND I am also confused as to how to access this path from the Annotation object after it's created. 

                                       

                                      Question to the experts... Since Document.importDataObject() is not an option due to the cost, complexity, etc. of bypassing the security issues... HOW CAN I later ACCESS the cAttachmentPath of the annot object once is has been selected?

                                       

                                      var annot = addAnnot({

                                                  page: event.target.page,

                                                  type: "FileAttachment",

                                                  author: "Form user",

                                                  name: "File Attachment",

                                                  point: [oFAP.x, oFAP.y],

                                                  contents: "File attachment on: " + util.printd("yyyy/mm/dd HH:MM:ss", new Date()),

                                                  attachIcon: "Paperclip"

                                              });

                                       

                                              annot.cAttachmentPath; // Prompt user to select a file to attach

                                      • 16. Re: Javascript to Embed file attachment
                                        try67 MVP & Adobe Community Professional

                                        You can't.