Highlighted

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

Community Beginner ,
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!

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!

Topics

PDF forms

Views

39.1K

Likes

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

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

Community Beginner ,
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!

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!

Topics

PDF forms

Views

39.1K

Likes

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
Most Valuable Participant ,
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>

Likes

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
Reply
Loading...
Community Beginner ,
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.

Likes

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
Reply
Loading...
Most Valuable Participant ,
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.

Likes

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
Reply
Loading...
Community Beginner ,
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!

Likes

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
Reply
Loading...
Adobe Community Professional ,
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

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.

Likes

2 Likes

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
Reply
Loading...
Community Beginner ,
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?

Likes

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
Reply
Loading...
Adobe Community Professional ,
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.

Likes

2 Likes

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
Reply
Loading...
New Here ,
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!

Likes

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
Reply
Loading...
Most Valuable Participant ,
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?

Likes

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
Reply
Loading...
New Here ,
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!

Likes

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
Reply
Loading...
Most Valuable Participant ,
Feb 06, 2019

Copy link to clipboard

Copied

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

Likes

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
Reply
Loading...
New Here ,
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.

Likes

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
Reply
Loading...
Community Beginner ,
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.

Likes

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
Reply
Loading...
New Here ,
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

Likes

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
Reply
Loading...
Community Beginner ,
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!

Likes

8 Likes

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
Reply
Loading...
Participant ,
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

Likes

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
Reply
Loading...
Community Beginner ,
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?

Likes

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
Reply
Loading...
Adobe Community Professional ,
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.

Likes

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
Reply
Loading...
Community Beginner ,
Aug 29, 2018

Copy link to clipboard

Copied

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

Likes

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
Reply
Loading...
Community Beginner ,
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.

Likes

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
Reply
Loading...
New Here ,
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?

Likes

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
Reply
Loading...
Most Valuable Participant ,
Nov 06, 2018

Copy link to clipboard

Copied

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

Likes

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
Reply
Loading...
New Here ,
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?

Likes

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
Reply
Loading...
Most Valuable Participant ,
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.

Likes

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
Reply
Loading...
New Here ,
Dec 13, 2018

Copy link to clipboard

Copied

Very helpful! Thanks much. A follow-up question: is it possible to have multiple file attachment buttons, each with a corresponding text box that displays the file name for whatever file was attached using the button?

Likes

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
Reply
Loading...
New Here ,
Mar 14, 2019

Copy link to clipboard

Copied

Hello,

Would it be possible for the function import_11 section to also include the helpful messages such as "attachment successful" popup message? It looks like the only message that is associated with that function is an error message. Maybe im not understanding it correctly, but using reader DC i don't get any of the helpful messages except the error one.

Ty for the great solution, it still works, just no user messages.

Likes

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
Reply
Loading...
Most Valuable Participant ,
Mar 14, 2019

Copy link to clipboard

Copied

It is possible, yes.

Replace this line (it's not necessary because the dialog to select a file appears automatically when the comment is added, as far as I can see):

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

With this:

if (annot) app.alert("Attachment successful.",3);

Likes

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
Reply
Loading...
New Here ,
Mar 14, 2019

Copy link to clipboard

Copied

Thank you so much for the quick response. This worked perfectly. I appreciate it very much.

Likes

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
Reply
Loading...
New Here ,
Sep 18, 2019

Copy link to clipboard

Copied

I thank all that have replied to this threas but I have a problem with one line:

"5. Close out of Prepare Form mode and go to Tools>Java Script"  I cannot find that in my tools of Adobe Acrobat Pro DC.  Has it been turned off?

Likes

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
Reply
Loading...
Adobe Community Professional ,
Jan 05, 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

Placing the icon outside of the page is a good idea!

Also see this sample file that let user import several attachments and manage them: https://files.acrobat.com/a/preview/12abdfeb-b387-4a4d-8e50-c24bf3e018ea

Likes

1 Like

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
Reply
Loading...
New Here ,
Jun 12, 2017

Copy link to clipboard

Copied

Where is the 'ok' icon located?

Likes

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
Reply
Loading...
Adobe Community Professional ,
Jun 13, 2017

Copy link to clipboard

Copied

should I not see an indicator in the pdf?

Where is the 'ok' icon located?

[bis]

Placing the icon outside of the page is a good idea!

[/bis]

Likes

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
Reply
Loading...
Resources
One Stop Solution for Acrobat
Edit a PDF
Add a group