Skip to main content
Participant
July 2, 2024
Question

Export of a file annotation located in PDF page

  • July 2, 2024
  • 2 replies
  • 407 views

I want to export a file attachment located in a page with a javascript.

I tried like that:

var annotObj = this.getAnnots(this.pageNum)[0];
var attachObj = annotObj.attachment;

console.println(attachObj.name);

this.exportDataObject({cName: attachObj.name});

And I expected the typical security popup, which does not appear.

What am I doing wrong here?

This topic has been closed for replies.

2 replies

Thom Parker
Community Expert
Community Expert
July 2, 2024

Try67 is correct. There's no JS functionality for directly saving the file attachment on a file attachment annotation. The contents of the file are however exposed and can be used to create a new PDF that can be saved. There are a couple of ways to do this. Here's some code using an undocumented function to shortcut this process. 

 

var attachObj = this.getAnnots(this.pageNum)[0].attachment;

// This line creates a new PDF in the tmp folder, with a temporary name, 

var oAttDoc = Collab.streamToDocument(attachObj.contentStream);

// Save to attachment name in same file folder as the current PDF 

oAttDoc.saveAs(this.path.replace(/[^\/]+$/,attachObj.name));

oAttDoc.closeDoc();

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
try67
Community Expert
Community Expert
July 2, 2024

Even though the attached file appears in the Attachments panel it is not a Data Object and therefore can't be exported using the exportDataObject command. You can see that this.dataObjects returns null.

I think the only way to do what you're after is to read the contents of the attachment's contentStream property and then write them out to a new file, and export it.