Skip to main content
Known Participant
September 11, 2023
Question

Need a file upload button, that shows a thumbnail and attaches to Attachments window

  • September 11, 2023
  • 1 reply
  • 8813 views

Hello. I need major help with this one. I will definitely Tip if you already have the knowledge or can figure this out for me. In Adobe Acrobat Pro forms, I am trying to create a button that will let my tech insert fuel and other purchase receipts to the form when asking for reimbursement. The button needs to be just a rectangle box, that when clicked, will open their desktop(windows) and let them select a file to add. For example. If they select a gas receipt jpeg, it will show a thumbnail size pic of the receipt as well as attach the pic to the form. I can then see the pic in my attachment view when reviewing the submitted form. Also, a popup saying the attachment was successful would be nice too. I hope you can help me. I actually have a button with the JavaScript created on a practice form I created, but it makes me choose the file two times. I used Document java script and Run a java script under button properties. Apparently, there is a discrepancy somewhere in my script. Even though it works, the choosing the file twice thing is a problem. I prefer to do both commands in one click. Thanks in advance for any assistance you render.

This topic has been closed for replies.

1 reply

Thom Parker
Community Expert
Community Expert
September 11, 2023

No code is provided for the "import_11()" and "import_pre_11()" functions.  Post the code for these functions.

 

Scripts in Acrobat are sandboxed for security reasons. The code for silently loading an image file (i.e. not showing the file browser) requires trust, which is one of the reasons that you need to call the two separate functions which each display the file browser.  The other reason for this setup is that neither of these functions provides the folder path for the loaded file.  Acquiring the folder path requires a different mechanism. 

 

Look at these articles:

https://acrobatusers.com/tutorials/popup_windows_part4/

https://www.pdfscripting.com/public/Trust-and-Privilege-in-Acrobat-Scripts.cfm

 

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Known Participant
September 11, 2023

Thanks for responding.  I will add my script below.  I must admit, I am totally new to javascript and both of those links you sent me does me no justice.  I have read over them but don't understand anything on those pages.   Hopefully my script below can shine some light on my issues.

 

Under Button Properties/Run A Javascript, I listed:

event.target.buttonImportIcon();


if (app.viewerVersion < 11) {

import_pre_11();

} else {

import_11();

}

 

Then under Javascript/Document Javascript; Script Name: Import_file

// 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_image);

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"

});

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

 

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

});

}

 

}

Thom Parker
Community Expert
Community Expert
September 11, 2023

The scripts are exactly what I suspected.  Each adds a different type of attachment.  

The test for the version number is unnecessary. A more important factor is whether the viewer is Acrobat Pro or Reader. The "import_pre_11()" function can't be used in Reader on any version, because Reader can't import real file attachments. 

 

To do both actions (display and import) with a single file select requires the scripts to be executed from a trusted location.

Document scripts are not generally trusted locations.

 

There are a few options, but the 2 simplest are

1.  Create a folder level script and define your functions as "app.trustedFunction()".  This is what I'd do. Anyone who uses the form must also install this folder level JavaScript file into Acrobat. 

 

2. Put all the code into the MouseUp script (no function calls). Then add the file to the list of trusted documents in the Enhanced Security Preference.  Anyone who wants to use the file will need to add it to the Enhanced Security Preference on thier system. 

 

Now, the functions you have, "import_11()" and "import_pre_11()" won't work. New code will need to be written to use the "field.browseForFileToSubmit()" function to acquire the file path and then use this path to display the preview and import the file attachment. 

 

 

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often