Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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();