Skip to main content
Participant
August 25, 2022
Question

Adding a File to a PDF using Javascript

  • August 25, 2022
  • 3 replies
  • 1922 views

Good day everyone

 

I was wondering if there's a way to know via javascript, if at least one file is attached to said PDF?

Im prsently using this code to let someone attch a file:

var myDoc = event.target;
var sFile = "myFile"
myDoc.importDataObject(sFile);

app.execMenuItem("ShowHideFileAttachment");

 

But after that, how can i keep track of this? I could use right now a variable to tell me its been added... but what if a user deletes the attachement?? can i track that? if so, i could update said variable maybe??

 

Any ideas??? thx so much for the help again 🙂

 

Patrick

 

This topic has been closed for replies.

3 replies

try67
Community Expert
Community Expert
August 25, 2022

You can check the length of the dataObjects array, but this only works for actual Attachments. If they used the Attach File commenting tool, that won't appear there. To detect that you would need to scan the entire annotations array of the file, checking if any of them are of the "FileAttachment" type.

 

bebarth
Community Expert
Community Expert
August 25, 2022

Hi,

Unless I'm mistaken, this only works if the attachment has been imported manually... not with the importDataObject method!

@+

Thom Parker
Community Expert
Community Expert
August 25, 2022
quote

Hi,

Unless I'm mistaken, this only works if the attachment has been imported manually... not with the importDataObject method!

@+


By @bebarth

 

the importDataObject method does add to the standard data objects. It's the FileAttachment annot that doesn't add to the dataObjects (as try67 pointed out).  

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Thom Parker
Community Expert
Community Expert
August 25, 2022
Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Participant
August 30, 2022

Sounds good, i will take a look at the reference link just to help me understand better what is going on.

Thx for the input. Would you be able to capture via javascript, if the person did press the remove attachment button???

Thom Parker
Community Expert
Community Expert
September 9, 2022

No, There are no JavaScript events associated with actions taken at the application level. 

 

 

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