Skip to main content
Rstreet
Inspiring
April 11, 2019
Answered

Delete Attachment Assistance

  • April 11, 2019
  • 1 reply
  • 4100 views

Greetings –

I am looking for a way to delete files added to a form using the below script I obtained from a forum. Wish I could remember which forum so I could give proper kudos; as the below works perfectly allowing me to attach any type of file to the form.

What I need now is a way to delete these files? Have been unable to locate anything on how to delete a  file, attached in the manner below. Anyone have any ideas?

Thanks in advance for your time. Any assistance will be greatly appreciated.

Script to attach file(s) -  (works great!):

******into form actions Java Script********

if (app.viewerVersion < 11) {

    import_pre_11();

} else {

    import_11();

}

******into document java script;  Import_File (create/add new)******

// Initialize attachment number

attachment_num = 1;

// Initial location of file attachment icon

// The x value is incremented below

var oFAP = {x_init: -72, x: -72, y: -72};  // Below lower-left corner of the page

function import_pre_11() {

    if (app.viewerType === "Reader") {

        app.alert({

            cMsg: "You must user Reader version 11 or later to attach files to this form.",

            cTitle: "Attach File Error",

            nIcon: 3,

            nType: 0 }); 

        return; }

    // Prompt user to import a file

    //app.alert({

        //cMsg: "Click the OK/Open button after selecting the //file from your system that you want to attach.",

        //cTitle: "Attach File",

        //nIcon: 3,

        //nType: 0

    //});  

    try {

        var rc = this.importDataObject("Attachment" + attachment_num);

        if (rc) {

            attachment_num += 1;

            app.alert({

                cMsg: "Attachment successful.\r\rUse the Attachments panel (on the left) to access the attached file(s).",

                cTitle: "Attachment Successful",

                nIcon: 3,

                nType: 0 });

        } else {

            app.alert({

                cMsg: "Attachment cancelled.",

                cTitle: "Attachment Cancelled",

                nIcon: 3,

                nType: 0 });

        }

    } catch (e) {

        app.alert({

            cMsg: "Could not attach file.",

            cTitle: "Could not attach file",

            nIcon: 3,

            nType: 0 });

    }

}

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

Hi Thom -

Thank you for your response. I did read into the article you suggested. Most of what I saw looks like it requires a document/attachment name? I am looking to delete all doc's/attach's; without having to provide a name or select the doc. This procedure will be part of resetting the form after the user submits the form.

I have tried the below by themselves:

     //this.removeDataObject("Attachment" + attachment_num);

     //this.doc.removeDataObject();

Lol...also played around a little (see below) with the way I imported the attachments...just trying to reverse the process using the  removeDataObject method...lol..now you can see I don't really know what I am doing here lol...none of which I got to work :-(

try {

    var rc = this.removeDataObject("Attachment" + attachment_num);

    //var rc = this.removeDataObject();

    //var rc = this.doc.removeDataObject("Attachment" + attachment_num);

    //var rc = this.doc.removeDataObject();

        if (rc) {

            attachment_num += 1;

            app.alert({

                cMsg: "Attachment deletion successful.",

                cTitle: "Attachment deletion successful",

                nIcon: 3,

                nType: 0 });

        } else {

            app.alert({

                cMsg: "Attachment deletion cancelled.",

                cTitle: "Attachment deletion cancelled",

                nIcon: 3,

                nType: 0 });

        }

    } catch (e) {

        app.alert({

            cMsg: "Could not delete file.",

            cTitle: "Could not delete file",

            nIcon: 3,

            nType: 0 });

    }

Lol...any other suggestions, hints, etc I may be able to play around with?


All of the "attachment names" are listed in "this.dataObjects".

So you can delete all of them by going through this list.

First, copy all the names to a new array, then delete each

this.dataObjects.map(function(a){return a.name;}).forEach(function(a){this.removeDataObject(a);});

Note that there is no exception handling and I haven't tested this code. It just shows the sequence of actions that need to take place to remove all attachements.

Note also that removing attachments is a restricted operation in Acrobat Reader.

1 reply

Inspiring
April 11, 2019

There is no "delete annotation", one uses the annotation object "destroy" method.

You will have to change the type of annotation to FileAttachment.

Rstreet
RstreetAuthor
Inspiring
April 11, 2019

Thank you for your response gkaiseril! However, I do not believe the attachment method being used is attaching the file as an annotation; but rather as a file attachment? Please excuse as I am not efficient with Java Scripting :-)

Legend
April 11, 2019

doc.removedataobject