Copy link to clipboard
Copied
Let me apologize up front for this being a bit of complicated question. I found a thread on this forum that helped me attach a file with a click of a button- now I want to detach this same file. Below is how far I've gotten.
I created a form in Adobe Acrobat DC that allows a user to attach a document to the form with a click of a button. The way this is programmed is that on the button itself with a Mouse Up action, the following Javascript is run:
if (app.viewerVersion < 11) {
import_pre_11();
} else {
import_11();
}
Then there is a document level javascript for attaching the file that is programmed like this:
// 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 button to select a file from your system.",
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
});
}
}
That process works flawlessly. What I want to do now is detach that same document with the click of another button. I understand very basic javascript but had help from the forum creating the above script. Can anyone provide me a solution (if there is one), to help me detach the file that the user attached with the script above? Thank you so much in advance!!!
Sorry, that's my bad... It needs to be:
this.syncAnnotScan();
Copy link to clipboard
Copied
If it helps, there will only be one attachment to every form and that attachment will always have the same name of Resume.pdf
Also, I'm just figuring out that most of the script from above is to alert users with old versions of Reader how to attach the file. For the purpose of deleting the file, I can assume all users will be using the most current version of Reader.
Copy link to clipboard
Copied
You can remove an annotation object using its destroy method, but first you need to find it.
Is this file attachment annotation the only one on the page?
Copy link to clipboard
Copied
Yes, and it will always have the same filename and be in the same location.
Copy link to clipboard
Copied
Well, you don't have access to the file path but if it's the only FileAttachment annotation on a specific page you can use this code to remove it (let's say it's the first page of the file):
this.syncAnnotsScan();
var annots = this.getAnnots();
if (annots) {
for (var i=annots.length-1; i>=0; i--) {
var annot = annots;
if (annot.type=="FileAttachment" && annot.page==0) {
annot.destroy();
}
}
}
Copy link to clipboard
Copied
I tried to use this code try67 and two different places and neither worked. Could you clarify where to place the code?
I added a new button with this scrip to run on the Mouse Up action. Nothing happened.
Then I tried to add it to the 'Reset Form' button as a javascript to be run after the original file is saved and the rest of the form reset. No error message, just didn't do anything either.
Thank you try67! If anyone can figure it out, you can! 🙂
Copy link to clipboard
Copied
Did you check the JS Console? Also, did you adjust the page number in the code, if necessary?
Copy link to clipboard
Copied
I think you are giving me too much credit for knowing what exactly I'm doing!
I checked the JS console and this is the error message:
TypeError: this.syncAnnotsScan is not a function
1:Field:Mouse Up
There is only one page to the attachment and only one attachment, so do I need to adjust the code? If so, what part of the code needs to change?
Thank you for your patience with my lack of knowledge!
Copy link to clipboard
Copied
Sorry, that's my bad... It needs to be:
this.syncAnnotScan();
Copy link to clipboard
Copied
Once again try67, THANK YOU! Works perfectly!
Find more inspiration, events, and resources on the new Adobe Community
Explore Now