Skip to main content
Participant
Mar 4, 2020 5:04 PM UTC
Answered

Script for deleting attachments

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 is closed to new replies. Start a new post to keep the conversation going.
try67
try67Correct answer
Community Expert
Mar 4, 2020 9:52 PM UTC

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();
	}
}

6 replies

JR Boulay
Community Expert
Community Expert
May 18, 2022 8:22 PM UTC

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

 

 

Translate
Acrobate du PDF, InDesigner et Photoshopographe
try67
Community Expert
Community Expert
Mar 4, 2020 9:49 PM UTC

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);
	}
}
Translate
try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
Mar 4, 2020 9:52 PM UTC

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();
	}
}
Translate
Participant
Mar 4, 2020 9:56 PM UTC

This works perfectly! Thanks a lot!!

Translate
Thom Parker
Community Expert
Community Expert
Mar 4, 2020 8:56 PM UTC

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

Translate
Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Participant
Mar 4, 2020 9:05 PM UTC

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

Translate