Skip to main content
leopro66
Participant
March 29, 2018
Answered

attachment name

  • March 29, 2018
  • 1 reply
  • 1612 views

How to rename attached files with Adobe JS? Or at least where the name of the attachment is kept? annots.name returns different value.

This topic has been closed for replies.
Correct answer Thom Parker

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.

1 reply

Thom Parker
Community Expert
Community Expert
March 29, 2018

Attachments are added to a PDF in two different ways, through the attachments panel and with the attachment annotation. So it seems you are using the annotation?  You need to be specific about this stuff.

The annot.name property is the name of the annotation. If you look up the annot properties in the Acrobat JavaScript reference you'll see there is an "attachment" property, which is the attachment object, which contains properties related to the attachment.

Acrobat DC SDK Documentation

None of these properties can be modified. I don't know of any method right now that can be used to control the attachment name used when adding an attachment with the annotation. However, if you were importing an attachment to the attachment panel, you do have control.

See this thread for more information

Acrobat DC SDK Documentation

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
leopro66
leopro66Author
Participant
March 29, 2018

I used Javascript code, invoked by pressing a button to attach files. They are shown in the 'attached' panel on the left and I can get the list of them, using this code

this.syncAnnotScan();

var annots = this.getAnnots({

nPage:0,

nSortBy: ANSB_Author,

bReverse: true

});

console.show();

console.println("Number of Annotations: " + annots.length);

var msg = "> %s";

for (var i = 0; i < annots.length; i++)

console.println(util.printf(msg, annots.contents));

But no property of these 'annots' objects return the true name of the attached file.
Is there a way to display 'annots.realFileName' instead of 'contents' or internal names returned by 'name' property?

try67
Community Expert
Community Expert
March 29, 2018

No, only the contents property holds that information.