Skip to main content
Participant
Jul 2, 2024 3:57 PM UTC
Question

Export of a file annotation located in PDF page

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 is closed to new replies. Start a new post to keep the conversation going.

2 replies

Thom Parker
Community Expert
Community Expert
Jul 2, 2024 5:50 PM UTC

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

 

 

Translate
Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
try67
Community Expert
Community Expert
Jul 2, 2024 4:10 PM UTC

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.

Translate