Skip to main content
bebarth
Community Expert
Community Expert
October 1, 2020
Question

open a FileAttachment type annot via JavaScript

  • October 1, 2020
  • 2 replies
  • 2593 views

How could I open a FileAttachment type annot via JavaScript?
I read all the annot proprieties but I don't understand which one I have to use for opening such as a double-click on the icon!!!

Thanks

 

This topic has been closed for replies.

2 replies

Thom Parker
Community Expert
Community Expert
October 1, 2020

The file attachment annotations cannot be accessed from the regular data object methods. This is because the file in the annot is not in the name tree, where all the regular attachments live. The embedded file stream is in the actual annotation. 

So, the file attachment annot has a property named "attachment", which is the actual file attachment. The content of this file is available through the undocumented "contentStream" property of the attachment property. 

 

Streams in Acrobat JavaScript have a "read" function for reading the raw file bytes as hex digits. Use the util function to convert to text. Like this, for a selected file attachment annot:

 

var strFileText = util.stringFromStream(selectedAnnots[0].attachment.contentStream);
 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
bebarth
Community Expert
bebarthCommunity ExpertAuthor
Community Expert
October 9, 2020

Hi,

Thank you for your answer.

Sorry for this late reply, but I just got the notification...
Since this is an undocumented property, do you have any idea what the attachment.contentStream feedback represents.

Can some information be usable in JavaScript?
Below is the feedback of a test.

 

%PDF-1.6
%
7 0 obj
<</Linearized 1/L 4797/O 9/E 725/N 1/T 4512/H [ 422 118]>>
endobj
                         
10 0 obj
<</DecodeParms<</Columns 3/Predictor 12>>/Filter/FlateDecode/ID[<8BD9D490C05043E8BFEEFA3F4DFBEAEF><1D69BE13C95A45A3886378EFA78CEE33>]/Index[7 5]/Info 6 0 R/Length 30/Prev 4513/Root 8 0 R/Size 12/Type/XRef/W[1 2 0]>>stream
hbbd`b`ab`ab'h`
true

 

@+

Legend
October 9, 2020

That content stream contains the whole of a normal PDF. I don't think there is any JavaScript method to say "open this string as a PDF file", and security restrictions stop the obvious solution "Write this string to a file, then open this as a PDF file."

Inspiring
October 1, 2020

You can use the doc.openDataObject method, but only for attachments that are PDFs. An exception will be thrown if you attempt to use it to lauch other file types, which is a good thing for security reasons.

bebarth
Community Expert
bebarthCommunity ExpertAuthor
Community Expert
October 1, 2020

Thanks for your answer,

so it is not possible?

@+

Inspiring
October 1, 2020

Sorry, my previous response was not entirely correct. You can attempt to launch a file attachment using the doc.exportDataObject JavaScript method and include the nLaunch parameter, but there are certain restrictions as documented here: https://help.adobe.com/en_US/acrobat/acrobat_dc_sdk/2015/HTMLHelp/#t=Acro12_MasterBook%2FJS_API_AcroJS%2FDoc_methods.htm%23TOC_exportDataObjectbc-31&rhtocid=_6_1_8_23_1_30