Skip to main content
Participant
March 4, 2020
Answered

Script for deleting attachments

  • March 4, 2020
  • 3 replies
  • 2362 views

Hi All, 

 

I have got a form with a button which adds attachments which works great, however i have also got a button to reset the form which clears all the fields however it does not delete the attachments.

Is there a script i can use in the reset button which can delete attachments same time it clears the form?

 

Thanks in advance

 

 

This topic has been closed for replies.
Correct answer try67

Follow up: It seems you want to use this in Reader. In that case the attachments are actually comments, and can be removed using this code:

 

this.syncAnnotScan();
var annots = this.getAnnots();
if (annots!=null) {
	for (var i=annots.length-1; i>=0; i--) {
		var annot = annots[i];
		if (annot.type=="FileAttachment") annot.destroy();
	}
}

3 replies

JR Boulay
Community Expert
Community Expert
May 18, 2022

- - - - - - -- - - - -

 

 

Acrobate du PDF, InDesigner et Photoshopographe
try67
Community Expert
Community Expert
March 4, 2020

You can use this code to do it:

 

if (this.dataObjects!=null) {
	for (var i=this.dataObjects.length-1; i>=0; i--) {
		this.removeDataObject(this.dataObjects[i].name);
	}
}
try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
March 4, 2020

Follow up: It seems you want to use this in Reader. In that case the attachments are actually comments, and can be removed using this code:

 

this.syncAnnotScan();
var annots = this.getAnnots();
if (annots!=null) {
	for (var i=annots.length-1; i>=0; i--) {
		var annot = annots[i];
		if (annot.type=="FileAttachment") annot.destroy();
	}
}
Participant
March 4, 2020

This works perfectly! Thanks a lot!!

Thom Parker
Community Expert
Community Expert
March 4, 2020

In Acrobat scripting, attachments are called "DataObjects". Use the "doc.removeDataObject()" function. 

Here's the reference entry

https://help.adobe.com/en_US/acrobat/acrobat_dc_sdk/2015/HTMLHelp/#t=Acro12_MasterBook%2FJS_API_AcroJS%2FDoc_methods.htm%23TOC_removeDataObjectbc-81&rhtocid=_6_1_8_23_1_80

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Participant
March 4, 2020

Thanks for your reply. However i want to delete all attachments without specifically naming them in the script as i want it so that when someone fills out the form and presses "Clear" button the form resets and all attachments (could be named anything) are deleted. Would love to know whether this is possible and if there is any other way to get round this? 

Much appreciated