Copy link to clipboard
Copied
How to rename attached files with Adobe JS? Or at least where the name of the attachment is kept? annots.name returns different value.
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++)
co
Copy link to clipboard
Copied
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.
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
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
No, only the contents property holds that information.
Copy link to clipboard
Copied
try running your script with this variation (assuming all of your annots are file attachments)
console.println(util.printf(msg, annots.attachment.name));
or
console.println(util.printf(msg, annots.attachment.pathname));
Copy link to clipboard
Copied
it returns "TypeError: annots.attachment is undefined", but thank you for the hint. I'll experiment some more.
Copy link to clipboard
Copied
Well then, you must not be using the File attachment annotation. Please refer to my first post, there are two different methods of attaching a file to a PDF. I cannot help you if you do not tell me which method you are using. What is the code that does that actual attachment?
Copy link to clipboard
Copied
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
});
}
}
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Your last comment is not true. When used on its own it will indeed prompt the user to select the attachment file for the comment, if one doesn't already exist. This strange behaivour is even documented in the API:
cAttachmentPath (Optional) Represents the device-independent path of the file to be attached. If this property is not set, the
user is prompted to select a data file using the File Open dialog box.
Copy link to clipboard
Copied
The user is prompted when the attachment annot is created with addAnnot. So in this case the line does nothing. In fact, if the user cancels the file open dialog, then "addAnnot" throws an exception and the annot is never created.
Copy link to clipboard
Copied
OK, I see what you mean... Yes, that is correct.
Copy link to clipboard
Copied
So do I get a "Correct Answer" for that one
Copy link to clipboard
Copied
Yes!