Skip to main content
Inspiring
January 4, 2017
Answered

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

  • January 4, 2017
  • 17 replies
  • 190091 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

LitPres
New Participant
September 11, 2020

Hi, i need to attach a vcf to a pdf so that a user can click a button and open it. I've tried everything (including the above) and it just links back to my C: drive as the location of the file instead of a file within the document. Once i distribute it the link to my drive will be broken. I've got a big pitch to a law firm in a couple days and would really like this to make it easy for them. I can't do a link to dropbox or other sites because of the strict security. HELP!

New Participant
August 10, 2020

Thank you sooooo very much. It is 2020 and this resource still applies.

New Participant
July 13, 2020

Hi, is there a way to unattach a file and/or see which files have been attached

New Participant
July 27, 2020

You can manually go to the navigation pane>attachments (the menu on the left side) to see existing attachments and add or delete.

OR

I made an action so that when a user clicks the attach button the navigation pane automatically opens.

  1. Click on your button and go to properties
  2. Go to actions
  3. Add an action
    Select trigger: mouse up
    Select action: execute a menu item
  4. Click "add"
  5. In the pop up, scroll down and select "View>Show/Hide>Navigtation Panes>Attachments then click OK

 

New Participant
April 7, 2020

Is there a way to show an Icon on the right for each document submitted with their labeling so that they know they submitted it?

New Participant
March 3, 2020

I am trying to use the same attach button I did before on the current version of Adobe and it does not work. Has something changed with the java script or Adobe?

 

Please advise and thanks so much,

New Participant
March 3, 2020

Please ignore my original questions. I noticed that the version was updated. I tried the solution and it worked!

Lu_AleGri
New Participant
September 28, 2020

Hi @15502153 ! I am having the same issue. what is the solution you tried?

laurac8992207
New Participant
June 15, 2019

Is there a way to make the attachment a required field?

This is a wonderful Script BTW.

stephaniea10802084
New Participant
May 17, 2019

I'm trying to use the code written by the user candicem62357049 and I'm on her step in her instructions where it says to:

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

When I click "Add" I get this script already in place:

function import_file()

{

}

Do I delete this? then add what she wrote?

When I do that I get an error:
SyntaxError: missing } after try block

149: at line 150

So, I went to line 150 and inserted the } and nothing happened....

Did I do something wrong???

try67
Community Expert
Community Expert
May 17, 2019

Yes, remove that code and paste the one from this thread, instead.

New Participant
February 14, 2019

This has helped me so much.

I'm just curious to know how I could then check if a user has actually attached a document (using function import_11)?
I'm need to add a submit button, but I don't want it to be able to be submitted if there's no attachment.

Thanks,

try67
Community Expert
Community Expert
February 14, 2019

Since you know the name of the annotation it adds ("File Attachment") and the page number, you can use the getAnnot method to try and retrieve it. If it returns null, then you know that no such annotation exists.

erinr28811028
New Participant
October 13, 2018

Hi

This is working but how do you add a message to appear to the user once the document is uploaded so they know it has worked?

Thanks

Erin

try67
Community Expert
Community Expert
October 14, 2018

The code given above already does that...

jewelnique
New Participant
June 28, 2018

This script is great! is there a way to show a text field that says "Item attached" once an actual file is attached?