How to create multiple buttons and link them to a single file attachment?

How to create multiple buttons and link them to a single file attachment?

Please use the following script in the button's JavaScriptAction


function saveAttachedPdfDocument(attachmentName)
    var isDocumentModified = this.dirty;
    var dataObj = findDataObject(attachmentName);    

    if (dataObj == null)
        dataObj = createDataObjectFromFileAttachment(attachmentName);

    if (dataObj != null) { 
        this.exportDataObject({ cName:, bAllowAuth: true, nLaunch: 0 });
        // remove temporary data object
        // restore document state
        this.dirty = isDocumentModified;
        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)
    var annots = this.getAnnots();
    for (var i = annots.length - 1; i >= 0; i--)
            if (annots[i].type == 'FileAttachment' && annots[i] == attachmentName) 
    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.

Have more questions? Submit a request


Please sign in to leave a comment.