• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
Locked
8

Add an "Attach File" button to a PDF form.

Community Beginner ,
Jan 04, 2017 Jan 04, 2017

Copy link to clipboard

Copied

I need to add the ability to attach a file to a PDF form. The file type that needs to be attached would most likely be a pdf, docx, or doc (if that matters). I'm working in Acrobat Pro DC, but the form will need to work in Reader as well. I think Java Script can accomplish this, but I have no idea how to write Java.

Thanks in advance for your help!

TOPICS
PDF forms

Views

175.0K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
1 ACCEPTED SOLUTION
Community Beginner ,
Jan 04, 2017 Jan 04, 2017

Copy link to clipboard

Copied

Here's how to create an "Attach File" button in a form in Acrobat Pro DC (as of Jan. 4, 2017):

1. In Acrobat Pro DC, go to Tools>Prepare Form

2. Add a button to your form by clicking on the "OK" icon and click the desired location on your form for the button.

3. Right click on the button and go to Properties. Set whatever you want in the General, Appearance, Position, and Options tabs.

4. In the Actions tab, under Select Action choose Run a Java Script. Click Add and copy and paste this text:

if (app.viewerVersion < 11) {

    import_pre_11();

} else {

    import_11();

}

5. Close out of Prepare Form mode and go to Tools>Java Script

6. Click on Document Java Scripts, and in Script Name type import_file, click Add, and then copy and paste this text:

// 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/Open button after selecting the file from your system that you want to attach.",

        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

        });

    }

   

}

7. And you are done! To view attached documents, go to View>Navigation Pane>Attachments.

Special thanks to George_Johnson​ for his help!

View solution in original post

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 04, 2017 Jan 04, 2017

Copy link to clipboard

Copied

Use the Attach File commenting tool to do that.

On Wed, Jan 4, 2017 at 3:31 PM, candicem62357049 <forums_noreply@adobe.com>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 04, 2017 Jan 04, 2017

Copy link to clipboard

Copied

Sorry, to clarify, I want the user of the form to be able to click a button to attach a pdf file to the form and not have to use the Attach File feature.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 04, 2017 Jan 04, 2017

Copy link to clipboard

Copied

The Attach File commenting tool is the only way it can be done in Reader. You can use a button that mimics using that tool, but that's about it.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 04, 2017 Jan 04, 2017

Copy link to clipboard

Copied

Okay, makes sense. How do you create a button that mimics using the tool? That sounds like what I might be looking for.

Thanks for your help!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 04, 2017 Jan 04, 2017

Copy link to clipboard

Copied

Here's a link to a file that demonstrates how it can be done: http://acroscript.net/pdf/demos/importFileJS_v1.pdf

 

(Edit: above link is dead, you can find this file here: https://web.archive.org/web/20180713075728/http://acroscript.net/pdf/demos/importFileJS_v1.pdf)

 

You'll have to study it a bit to understand how it works, particularly with Reader. The method it uses with Reader requires Reader 11 or later. Most of the code is in functions in a document-level JavaScript. Note that it won't work in browser-based PDF viewers or mobile PDF viewers. It will work if Adobe Reader/Acrobat (Windows/Mac) is used.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 04, 2017 Jan 04, 2017

Copy link to clipboard

Copied

Thanks George_Johnson​. I opened your pdf, went to "Prepare Form" and copied and pasted your button into my form (also in "Prepare Form" mode) and it's not working. I'm not sure what you mean by "you'll have to study it a bit to under how it works." Is there some other setting I need to change to get it to work?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 04, 2017 Jan 04, 2017

Copy link to clipboard

Copied

Yes, as I mentioned earlier most of the code is in a document-level JavaScript, which you'll have to copy to your form as well. More information on document-level JavaScripts can be found here: https://acrobatusers.com/tutorials/js_document_scripts

Acrobat DC is different than Acrobat 9 in how you access document JavaScript, but it should get you started.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 05, 2019 Feb 05, 2019

Copy link to clipboard

Copied

George_Johnson I have been looking through Adobe EULA and release notes, I am trying to determine if there are any potential licensing issues that we are infringing if we use this code in order to allow users to attach files to PDF and submitting them back to us. I know that a lot changed starting with Acrobat 11 as far as allowing Reader users to save filled-out forms, but I am not finding anything definitive if it is OK to use these scripts to allow them to attach as well.

Any advice or direction would be appreciated. Thanks!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 05, 2019 Feb 05, 2019

Copy link to clipboard

Copied

I'm not a lawyer, but I don't think there's any issue here. Why would Adobe add a feature that enables you to break the EULA?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 06, 2019 Feb 06, 2019

Copy link to clipboard

Copied

My main concern is if this form is going to more than 500 recipients -- does this script technically make the file 'Reader Extended'? Our company hit a snag with that issue about 6 years ago and I don't want to go there again. Attachments are disabled in Reader by default unless a script like the one in this thread is used. Would this script technically be circumventing their intention?

Adobe Reader EULA:

3.4 Disabled Features. The Software may contain features or functionalities that are hidden or appear disabled or "grayed out" (collectively, "Disabled Features"). Disabled Features will activate only when you open a PDF document that was created using enabling technology available only from Adobe. You will not access, or attempt to access, any Disabled Features by means other than the use of such enabling technologies, nor will you rely on the Software to create a feature substantially similar to any Disabled Feature or otherwise circumvent the technology that controls activation of any such feature.

Thanks!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 06, 2019 Feb 06, 2019

Copy link to clipboard

Copied

Again, not a lawyer... But no, the extended features can't be applied with a script.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 05, 2019 Dec 05, 2019

Copy link to clipboard

Copied

I followed your instructions, but the messages built into the document javascript are not accessed.  My concern is when I cancel the attachment.  I get the standard File attachment error telling me to "Please report this error".  What would cause this problem.  I am using Acrobat Pro 2017 in Windows 11 to create the button.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 19, 2019 Dec 19, 2019

Copy link to clipboard

Copied

Download/Save that PDF to your desktop and run it from there.

 

If, when you are testing it, you don't actually attach a file it will report back that error.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 23, 2020 Apr 23, 2020

Copy link to clipboard

Copied

Hello George_Johnson!  Thank you for the script, it works great on Mac/PC, but since 2017 is there a way now to make this option functional on Mobile devices and web browser-based viewers?

Thank you much,

Gage

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 04, 2017 Jan 04, 2017

Copy link to clipboard

Copied

Here's how to create an "Attach File" button in a form in Acrobat Pro DC (as of Jan. 4, 2017):

1. In Acrobat Pro DC, go to Tools>Prepare Form

2. Add a button to your form by clicking on the "OK" icon and click the desired location on your form for the button.

3. Right click on the button and go to Properties. Set whatever you want in the General, Appearance, Position, and Options tabs.

4. In the Actions tab, under Select Action choose Run a Java Script. Click Add and copy and paste this text:

if (app.viewerVersion < 11) {

    import_pre_11();

} else {

    import_11();

}

5. Close out of Prepare Form mode and go to Tools>Java Script

6. Click on Document Java Scripts, and in Script Name type import_file, click Add, and then copy and paste this text:

// 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/Open button after selecting the file from your system that you want to attach.",

        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

        });

    }

   

}

7. And you are done! To view attached documents, go to View>Navigation Pane>Attachments.

Special thanks to George_Johnson​ for his help!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
May 07, 2017 May 07, 2017

Copy link to clipboard

Copied

First time trying this, appears to work in Acrobat XI. Silly question of the day. I browsed to a word doc and attached it, should I not see an indicator in the pdf of an attached file? When I saved the PDF, it did not copy the attached file with it?

Thanks

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 29, 2018 Aug 29, 2018

Copy link to clipboard

Copied

Works like a charm when using this button on a desktop.

I want to make this button mobile compatible as well. Is there a way to make it so that if I'm using Acrobat Reader on my iPhone, the button has the same functionality?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 29, 2018 Aug 29, 2018

Copy link to clipboard

Copied

No, as mentioned earlier, it will only work with Acrobat/Reader on Windows/Mac, no mobile PDF viewers.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 29, 2018 Aug 29, 2018

Copy link to clipboard

Copied

Is there a way that I can turn my Acrobat Form into a web-based form?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Sep 14, 2018 Sep 14, 2018

Copy link to clipboard

Copied

This is incredible. I feel like 99% of the time you have a copy and paste solution, it doesn't work. This works brilliantly. Well done.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 05, 2018 Nov 05, 2018

Copy link to clipboard

Copied

I followed the directions and the button works fine with the PDF form I created. All the prompts work correctly based on what you do after you click the button - if you click OK but then you don't add a file it says attachment canceled, if you click OK and then you do add a file it says attachment successful, etc. I can open up the attachments panel and see all my attachments there.

However, everything changes after I distribute the form. After that, when I click on the button and then OK to select a file, instead of being able to select a file I get an a error message titled "Warning:JavaScript Window - could not attach file."

Any ideas on what might be causing problems to this button post-distribution?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 06, 2018 Nov 06, 2018

Copy link to clipboard

Copied

Where are you opening the file? In what application (including version number)?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 06, 2018 Nov 06, 2018

Copy link to clipboard

Copied

Adobe Acrobat Pro. Glad you asked because I myself was kind of wondering if

the application I was using to test the distributed form mattered, since it

seems like the javascript in this button solution was geared towards

Reader. Had someone try filling out the distributed form using Reader and

the attachment button worked fine. Not sure why the button worked fine with

Acrobat prior to distribution.

Most of the form users I'll have completing this form will be using Reader,

but there may be the occasional user with Acrobat as their default. Is

there any javascript that could be included in this button solution so that

it works for both Reader and Acrobat users?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 06, 2018 Nov 06, 2018

Copy link to clipboard

Copied

If it works in Reader then it should work in Acrobat as well, but not the other way around, necessarily.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines