How to create multiple buttons and link them to a single file attachment?
Please use the following script in the button’s JavaScriptAction
//console.show();
saveAttachedPdfDocument('attachment.pdf');
function saveAttachedPdfDocument(attachmentName)
{
var isDocumentModified = this.dirty;
var dataObj = findDataObject(attachmentName);
if (dataObj == null)
dataObj = createDataObjectFromFileAttachment(attachmentName);
if (dataObj != null) {
this.exportDataObject({ cName: dataObj.name, bAllowAuth: true, nLaunch: 0 });
// remove temporary data object
this.removeDataObject(dataObj.name);
// restore document state
this.dirty = isDocumentModified;
}
else
console.println('The attachment was not found or could not be exported.');
}
function findDataObject(dataObjectName)
{
var dataObjectsCollection = this.dataObjects;
if (dataObjectsCollection != null) {
for (var i = 0; i < dataObjectsCollection.length; i++) {
if (dataObjectsCollection[i].name == dataObjectName) {
return dataObjectsCollection[i];
}
}
}
return null;
}
function createDataObjectFromFileAttachment(attachmentName)
{
this.syncAnnotScan();
var annots = this.getAnnots();
for (var i = annots.length - 1; i >= 0; i--)
if (annots[i].type == 'FileAttachment' && annots[i].attachment.name == attachmentName)
break;
if (i != -1) {
var attachmentObj = annots[i].attachment;
// create temporary data object
this.createDataObject(attachmentName, '');
this.setDataObjectContents(attachmentName, attachmentObj.contentStream);
var dataObj = this.getDataObject(attachmentName);
return dataObj;
}
else {
console.println('Attachment not found.');
return null;
}
}
Please note, you should insert the actual name of the attached file to the saveAttachedPdfDocument
function in the beginning of the script.
There are two different types of PDF attachments in PDF standard. A file can be attached as DataObject
(default method of Adobe Acrobat), or as FileAttachment
annotation (the only method currently supported in PDF SDK). The script supports opening both types of attachments.