Skip to main content
Participating Frequently
May 8, 2018
Question

JavaScript for a "Clear" Button for the attached files in a pdf-form

  • May 8, 2018
  • 5 replies
  • 1218 views

Hello Guys,

I need help with my 2 buttons I have created in a PDF-form so that the person filling the for can Attach a file from his Browser and the file name will be visible in another field. However I need one more button so that it "Clears" the attached files.

This is my JavaScript that works for attaching a file and so that the file name is shown:

try {

    var annot = this.addAnnot({

        page: 0,

        type: "FileAttachment",

        point: [500, 500],

        noView: true,

        author: "Attachment"

    });

    annot.cAttachmentPath;

    var attachmentObj = annot.attachment;

    if (attachmentObj !== null) {

        app.alert("Add this file: " + attachmentObj.name);

        var l = this.getField("lst1");

        if (l.value == "") {

            l.value = attachmentObj.name;

        }

        else {

            l.value = l.value + "\n" + attachmentObj.name;

        }

    }

}

catch(e) {

    if (e.name == "NotAllowedError") {

        // do nothing

    }

}

--------------------------------------------------

Now I need another button so that if the person want to take the attached file off he can do it with a button click.

And my JavaScript, that does not work for that is:

this.syncAnnotScan () ;

var annots = thisgetAnnots({

    nPage: 0,

    nSortBy: ANSB_Type

});

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

    if (annots .type =="FileAttachment") {

        annots.destroy();

  }

}

var 1 = this.getField ("lst1") ;

l.ClearItems();

---------------------------------------------------------

When I run this JavaScript in the form I get the: SyntaxError: missing variable name

                                                                            12: at line 13                     Ln13, Col16

----------------------------------------------------------------------------------------------------------------

Can someone please help me make this "Clear" button I need to work.

What have I done wrong?

I got my info from this video on youtube:

How to attach a file to PDF form with Adobe Acrobat - YouTube

I would appriciate it SOOOOO MUCH!!!!

Kind Regards

Stefan.S from Sweden

This topic has been closed for replies.

5 replies

SpirovskiAuthor
Participating Frequently
May 8, 2018

Hello guys,

I am so lucky to have you. Because of you I managed to fix it and it now works to delete an attachment :-)

Thank you so much. For those who need the JavaScript for this, here it is:

It deletes the actual attachment but not the file name. Do you know how I could have that done as well???

I am pushing my luck, I know but it never hurts to ask :-)

this.syncAnnotScan () ;

var annots = this.getAnnots({

    nPage: 0,

    nSortBy: ANSB_Type

});

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

    if (annots .type =="FileAttachment") {

        annots.destroy();

  }

}

var l = this.getField ("lst1") ;

l.clearItems();

try67
Community Expert
Community Expert
May 8, 2018

Is "lst1" a text field or a list-box (or combo-box), or what?

SpirovskiAuthor
Participating Frequently
May 8, 2018

yes,

"lst1" is a text field

As I said I do get the JavaScript to actually remove the attached file but the text that it prints, the file name,

i snot deleted like on his video on youtube. What am I missing??? How to attach a file to PDF form with Adobe Acrobat - YouTube

Thank you in advance

Inspiring
May 8, 2018

Also, you'd need to use clearItems, not ClearItems.

try67
Community Expert
Community Expert
May 8, 2018

Also, this command is not valid: thisgetAnnots . You're missing a period after "this".

And if you're going to delete annotations you have to start from the end of the array and move backwards.

Bernd Alheit
Community Expert
Community Expert
May 8, 2018

You can't use the number 1 as variable name.

var 1 = this.getField ("lst1") ;

try67
Community Expert
Community Expert
May 8, 2018

"1" is not a valid variable name. Change it to something else.