Skip to main content
Known Participant
February 21, 2016
Answered

Javascript to Embed file attachment

  • February 21, 2016
  • 2 replies
  • 11674 views

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

        });

    }

   

}

This topic has been closed for replies.
Correct answer George_Johnson

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.

2 replies

Participant
October 22, 2016

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

Inspiring
October 22, 2016

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?

Participant
October 22, 2016

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!

Inspiring
February 21, 2016

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.

try67
Community Expert
Community Expert
February 21, 2016

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.

Known Participant
February 21, 2016

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.