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!
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!
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?
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.
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);
Copy link to clipboard
Copied
Thank you so much for the quick response. This worked perfectly. I appreciate it very much.
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?
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
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.
Copy link to clipboard
Copied
Where is the 'ok' icon located?
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]
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.
Copy link to clipboard
Copied
Can anyone help with the above?
Thank you
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
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?
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
Copy link to clipboard
Copied
The code given above already does that...
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,
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.
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???
Copy link to clipboard
Copied
Yes, remove that code and paste the one from this thread, instead.
Copy link to clipboard
Copied
Is there a way to make the attachment a required field?
This is a wonderful Script BTW.
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,
Copy link to clipboard
Copied
Please ignore my original questions. I noticed that the version was updated. I tried the solution and it worked!
Copy link to clipboard
Copied
Hi @mariawb32 ! I am having the same issue. what is the solution you tried?
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?
Copy link to clipboard
Copied
Hi, is there a way to unattach a file and/or see which files have been attached