Copy link to clipboard
Copied
Greetings –
I am looking for a way to delete files added to a form using the below script I obtained from a forum. Wish I could remember which forum so I could give proper kudos; as the below works perfectly allowing me to attach any type of file to the form.
What I need now is a way to delete these files? Have been unable to locate anything on how to delete a file, attached in the manner below. Anyone have any ideas?
Thanks in advance for your time. Any assistance will be greatly appreciated.
Script to attach file(s) - (works great!):
******into form actions Java Script********
if (app.viewerVersion < 11) {
import_pre_11();
} else {
import_11();
}
******into document java script; Import_File (create/add new)******
// 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\rUse the Attachments panel (on the left) 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 });
}
}
All of the "attachment names" are listed in "this.dataObjects".
So you can delete all of them by going through this list.
First, copy all the names to a new array, then delete each
this.dataObjects.map(function(a){return a.name;}).forEach(function(a){this.removeDataObject(a);});
Note that there is no exception handling and I haven't tested this code. It just shows the sequence of actions that need to take place to remove all attachements.
Note also that removing attachments is a restricted operation
...Copy link to clipboard
Copied
There is no "delete annotation", one uses the annotation object "destroy" method.
You will have to change the type of annotation to FileAttachment.
Copy link to clipboard
Copied
Thank you for your response gkaiseril! However, I do not believe the attachment method being used is attaching the file as an annotation; but rather as a file attachment? Please excuse as I am not efficient with Java Scripting 🙂
Copy link to clipboard
Copied
doc.removedataobject
Copy link to clipboard
Copied
Hi Test Screen Name. Thank you for your response. Although I am not quite sure what to do with your recommendation, I will play around with it a bit and see if I can figure it out. Lol...may reach back out if it starts taking too long or I get too frustrated 😉
Thank you
Copy link to clipboard
Copied
Hi Test Screen Name. Thanks again for your response/suggestion. As I am not too familiar with java Scripting, I have tried numerous ways to incorporate you suggestion; to no avail 😞
Might you be able to provide me an example of how your suggestion might work? I am usually pretty good about figuring this stuff out and coming up with a way that works, but this one is stumping me. Lol...probably using a lot of my brain power on an Access project I am also currently working.
Anyhow, if you might be able to provide me an example of how your suggestion could work, I can probably figure out/make it work from there.
Thanks again Test Screen Name.your time and assistance is much appreciated
Copy link to clipboard
Copied
doc.removeDataObject() is a function in the Acrobat JavaScript API.
You can read about it here:
Copy link to clipboard
Copied
Hi Thom -
Thank you for your response. I did read into the article you suggested. Most of what I saw looks like it requires a document/attachment name? I am looking to delete all doc's/attach's; without having to provide a name or select the doc. This procedure will be part of resetting the form after the user submits the form.
I have tried the below by themselves:
//this.removeDataObject("Attachment" + attachment_num);
//this.doc.removeDataObject();
Lol...also played around a little (see below) with the way I imported the attachments...just trying to reverse the process using the removeDataObject method...lol..now you can see I don't really know what I am doing here lol...none of which I got to work 😞
try {
var rc = this.removeDataObject("Attachment" + attachment_num);
//var rc = this.removeDataObject();
//var rc = this.doc.removeDataObject("Attachment" + attachment_num);
//var rc = this.doc.removeDataObject();
if (rc) {
attachment_num += 1;
app.alert({
cMsg: "Attachment deletion successful.",
cTitle: "Attachment deletion successful",
nIcon: 3,
nType: 0 });
} else {
app.alert({
cMsg: "Attachment deletion cancelled.",
cTitle: "Attachment deletion cancelled",
nIcon: 3,
nType: 0 });
}
} catch (e) {
app.alert({
cMsg: "Could not delete file.",
cTitle: "Could not delete file",
nIcon: 3,
nType: 0 });
}
Lol...any other suggestions, hints, etc I may be able to play around with?
Copy link to clipboard
Copied
All of the "attachment names" are listed in "this.dataObjects".
So you can delete all of them by going through this list.
First, copy all the names to a new array, then delete each
this.dataObjects.map(function(a){return a.name;}).forEach(function(a){this.removeDataObject(a);});
Note that there is no exception handling and I haven't tested this code. It just shows the sequence of actions that need to take place to remove all attachements.
Note also that removing attachments is a restricted operation in Acrobat Reader.
Copy link to clipboard
Copied
OMG...wow you sure made that look easy! Works like a charm! You are Da Man!!!!! Thank you so much Thom! Man...I have a long way to go with this Java stuff. Lot different than vb scripting 😉
Thanks again Thom. Your assistance is MUCH appreciated!
Copy link to clipboard
Copied
You're welcome JavaScript is a much better language overall than VB. By learning it, you will open up many more doors than with VB.
Copy link to clipboard
Copied
Thanks again Thom. I am sure I will be heading down this road in more depth fairly soon. I have one more little thing to overcome (lol - post coming soon) on this particular project; which will most likely set a precedence for future projects.
One completed, I will post a copy of the project here for others to see. Lol...am sure it can be done in a more efficient manner, but it is what I was able to piece together with the help of folks like you!
Can't thank you enough Thom!
Best regards
Copy link to clipboard
Copied
OK, so as promised in post #11, I am providing a copy of my project. Lol...all you pro's out there go easy on me! 🙂 I am def no expert at this Java stuff. Tis a lot different than VB; in which I am more accustom to. But I can usually figure out enough to be dangerous! Having pros like Thom available for assistance when needed is absolutely wonderful! Would not have been able to complete this project, at least in Adobe, without this kind of assistance. Thank you, thank you, thank you to all you pros out there assisting us wanna-bee's and making us look good!
Enjoy
Let me know if anyone has any trouble accessing the file!