Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Script for deleting attachments

Community Beginner ,
Mar 04, 2020 Mar 04, 2020

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

 

 

TOPICS
Acrobat SDK and JavaScript
2.4K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct answers

Community Expert , Mar 04, 2020 Mar 04, 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);
	}
}
Translate
Community Expert , Mar 04, 2020 Mar 04, 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();
	}
}
Translate
Community Expert ,
Mar 04, 2020 Mar 04, 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_Acro...

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 04, 2020 Mar 04, 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 04, 2020 Mar 04, 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);
	}
}
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 04, 2020 Mar 04, 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();
	}
}
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 04, 2020 Mar 04, 2020

This works perfectly! Thanks a lot!!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 18, 2022 May 18, 2022
LATEST

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

 

 


Acrobate du PDF, InDesigner et Photoshopographe
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines