Skip to main content
Inspiring
January 4, 2017
Answered

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

  • January 4, 2017
  • 17 replies
  • 190103 views

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!

Correct answer candicem62357049

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!

17 replies

Participant
June 13, 2018

This "Attach File" button script works great... but I want to use it together with the PDF form 'image field' that uses the script:

event.target.buttonImportIcon();

How can I combine it with your script so it will show the image and attach it in the same time:

if (app.viewerVersion < 11) {

    import_pre_11();

} else {

    import_11();

}

Thanks, JanB

Participant
February 13, 2018

Hello George,

Just wanted to know is there a conditional script (If/then) that will work with the above script to validate my hidden field value (If the click action is Cancel then the hidden text value= "" but if the click action is Select then the hidden text value is "File Attached")?

I hope that made sense.

Right now I running 2 separate script action on the Attach Button but It creates a value in the hidden filed even if the user selects "Cancel"

this.getField("hiddentextbox1").value = "file attached";

I apologize if I'm not making sense.

Participant
February 16, 2018

Can anyone help with the above?

Thank you

Roxanne Charney
Participant
June 13, 2017

Where is the 'ok' icon located?

JR Boulay
Community Expert
Community Expert
June 13, 2017

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]

Acrobate du PDF, InDesigner et Photoshopographe
JR Boulay
Community Expert
Community Expert
January 5, 2017

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://acrobat.adobe.com/link/track?uri=urn:aaid:scds:US:43b01ff3-a660-499b-a259-0e17c4998721

Acrobate du PDF, InDesigner et Photoshopographe
Participant
August 31, 2020

JR_Boulay, This is what I am looking for:

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

 

This is what I got.  Please help.

The files have been removed or you don't have access. Contact the document owner to request access.

candicem62357049AuthorCorrect answer
Inspiring
January 4, 2017

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!

Known Participant
May 7, 2017

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

Inspiring
January 4, 2017

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.

try67
Community Expert
Community Expert
January 4, 2017

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.

Inspiring
January 4, 2017

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!


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.

try67
Community Expert
Community Expert
January 4, 2017

Use the Attach File commenting tool to do that.

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