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

New Here ,
May 08, 2018 May 08, 2018

Copy link to clipboard

Copied

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

TOPICS
Acrobat SDK and JavaScript, Windows

Views

221

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
May 08, 2018 May 08, 2018

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
May 08, 2018 May 08, 2018

Copy link to clipboard

Copied

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

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
May 08, 2018 May 08, 2018

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
May 08, 2018 May 08, 2018

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 08, 2018 May 08, 2018

Copy link to clipboard

Copied

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();

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
May 08, 2018 May 08, 2018

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 08, 2018 May 08, 2018

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
May 08, 2018 May 08, 2018

Copy link to clipboard

Copied

The clearItems method only works on list- or combo-box fields.

Do you want to clear the field's value entirely, or just remove a part of it (the line with the name of the annotation you're deleting)?

And you didn't fix the issue I mentioned regarding the loop that deletes those annotations. If you have more than one it will likely not work correctly!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 08, 2018 May 08, 2018

Copy link to clipboard

Copied

Oh Man

Could I send you the form so you could help me perfect it?

Please please, pretty please??

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
May 08, 2018 May 08, 2018

Copy link to clipboard

Copied

I usually charge for private consultations and help.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 08, 2018 May 08, 2018

Copy link to clipboard

Copied

I do have PayPal

And willing to pay

It obviously depends on how much though

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 08, 2018 May 08, 2018

Copy link to clipboard

Copied

I could send you the pdf form and tell you exactly how I would like it to work.

You can then write the JavaScript for my specific need and send it back to me.

Whould also like to talk on the phone with you if possible. I can call.

What do you say? Come on buddy, help a Swedish guy out?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
May 08, 2018 May 08, 2018

Copy link to clipboard

Copied

Send me an email to try6767 at gmail.com and we'll discuss it there, please.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 08, 2018 May 08, 2018

Copy link to clipboard

Copied

Hey,

I just sent you an email.

Did you get it?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
May 08, 2018 May 08, 2018

Copy link to clipboard

Copied

LATEST

Yes. Will reply there...

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines