• 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

174.9K

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
New Here ,
Dec 13, 2018 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?

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 ,
Mar 14, 2019 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.

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 ,
Mar 14, 2019 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);

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 ,
Mar 14, 2019 Mar 14, 2019

Copy link to clipboard

Copied

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

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 ,
Sep 18, 2019 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?

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

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 ,
Aug 31, 2020 Aug 31, 2020

Copy link to clipboard

Copied

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.

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 ,
Jun 12, 2017 Jun 12, 2017

Copy link to clipboard

Copied

Where is the 'ok' icon located?

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 ,
Jun 13, 2017 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]

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 ,
Feb 13, 2018 Feb 13, 2018

Copy link to clipboard

Copied

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.

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 ,
Feb 16, 2018 Feb 16, 2018

Copy link to clipboard

Copied

Can anyone help with the above?

Thank you

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 ,
Jun 13, 2018 Jun 13, 2018

Copy link to clipboard

Copied

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

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 ,
Jun 28, 2018 Jun 28, 2018

Copy link to clipboard

Copied

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

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 ,
Oct 13, 2018 Oct 13, 2018

Copy link to clipboard

Copied

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

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 ,
Oct 14, 2018 Oct 14, 2018

Copy link to clipboard

Copied

The code given above already does that...

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 13, 2019 Feb 13, 2019

Copy link to clipboard

Copied

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,

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 14, 2019 Feb 14, 2019

Copy link to clipboard

Copied

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.

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 ,
May 16, 2019 May 16, 2019

Copy link to clipboard

Copied

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???

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 ,
May 17, 2019 May 17, 2019

Copy link to clipboard

Copied

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

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 ,
Jun 15, 2019 Jun 15, 2019

Copy link to clipboard

Copied

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

This is a wonderful Script BTW.

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 ,
Mar 03, 2020 Mar 03, 2020

Copy link to clipboard

Copied

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,

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 ,
Mar 03, 2020 Mar 03, 2020

Copy link to clipboard

Copied

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

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 ,
Sep 28, 2020 Sep 28, 2020

Copy link to clipboard

Copied

LATEST

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

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 07, 2020 Apr 07, 2020

Copy link to clipboard

Copied

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?

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 ,
Jul 13, 2020 Jul 13, 2020

Copy link to clipboard

Copied

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

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