I used a button, that calls the following JS code to add an attachment.
function import_11(numb) {
console.println(numb);
try {
var annot = addAnnot({
page: event.target.page,
type: 'FileAttachment',
author: 'Form user',
name: 'File Attachment',
point: [oFAP.x, oFAP.y],
contents: 'File attachment for the question ' + numb + ' made on: ' + util.printd('yyyy/mm/dd HH:MM:ss', new Date()),
attachIcon: 'Paperclip'
});
annot.cAttachmentPath; // Prompt user to select a file to attach
oFAP.x += 18; // Increment the x location for the icon
} catch (e) {
app.alert({
cMsg: 'Could not attach file. Please report this error.',
cTitle: 'File attachment error',
nIcon: 3,
nType: 0
});
}
}
So the reason for the error is that you have annotations that are not file attachments. In order for the script to work it needs to sort for the file attachment type.
This script will work for displaying all the attached file names
var annots = this.getAnnots({
nPage:0,
nSortBy: ANSB_Author,
bReverse: true
});
console.show();
var aAttach = annots.filter(function(a){return (a.type=="FileAttachment");});
console.println("Number of Annotations: " + aAttach.length);
for (var i = 0; i < aAttach.length; i++)
console.println("> " + aAttach.attachment.name);
On another note, this line in you code for adding the attachment does nothing.
annot.cAttachmentPath; // Prompt user to select a file to attach
The "cAttachmentPath" property is only used when setting a known attachment path in the "addAnnot()" function.
Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often