Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Please help Creating a Detach Button that detaches a document I have attached with an Attach Button

Community Beginner ,
Jan 30, 2016 Jan 30, 2016

Let me apologize up front for this being a bit of complicated question. I found a thread on this forum that helped me attach a file with a click of a button- now I want to detach this same file.  Below is how far I've gotten.   

I created a form in Adobe Acrobat DC that allows a user to attach a document to the form with a click of a button.  The way this is programmed is that on the button itself with a Mouse Up action, the following Javascript is run:

if (app.viewerVersion < 11) {

    import_pre_11();

} else {

    import_11();

}

Then there is a document level javascript for attaching the file that is programmed like this:

// 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 button to select a file from your system.",

        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\rOpen the Attachments panel 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

        });

    }

}

function import_11() {

    try {

        var annot = addAnnot({

            page: event.target.page,

            type: "FileAttachment",

            author: "Form user",

            name: "File Attachment",

            point: [oFAP.x, oFAP.y],

            contents: "File attachment 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.\r\rPlease report this error.",

            cTitle: "File attachment error",

            nIcon: 3,

            nType: 0

        });

    }

   

}

That process works flawlessly.  What I want to do now is detach that same document with the click of another button.  I understand very basic javascript but had help from the forum creating the above script.  Can anyone provide me a solution (if there is one), to help me detach the file that the user attached with the script above? Thank you so much in advance!!!

TOPICS
Acrobat SDK and JavaScript
1.4K
Translate
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

correct answers 1 Correct answer

Community Expert , Jan 31, 2016 Jan 31, 2016

Sorry, that's my bad... It needs to be:

this.syncAnnotScan();

Translate
Community Beginner ,
Jan 30, 2016 Jan 30, 2016

If it helps, there will only be one attachment to every form and that attachment will always have the same name of Resume.pdf

Also, I'm just figuring out that most of the script from above is to alert users with old versions of Reader how to attach the file.  For the purpose of deleting the file, I can assume all users will be using the most current version of Reader.

Translate
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
Community Expert ,
Jan 30, 2016 Jan 30, 2016

You can remove an annotation object using its destroy method, but first you need to find it.
Is this file attachment annotation the only one on the page?

Translate
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
Community Beginner ,
Jan 30, 2016 Jan 30, 2016

Yes, and it will always have the same filename and be in the same location.

Translate
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
Community Expert ,
Jan 31, 2016 Jan 31, 2016

Well, you don't have access to the file path but if it's the only FileAttachment annotation on a specific page you can use this code to remove it (let's say it's the first page of the file):

this.syncAnnotsScan();

var annots = this.getAnnots();

if (annots) {

    for (var i=annots.length-1; i>=0; i--) {

        var annot = annots;

        if (annot.type=="FileAttachment" && annot.page==0) {

            annot.destroy();

        }

    }

}

Translate
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
Community Beginner ,
Jan 31, 2016 Jan 31, 2016

I tried to use this code try67 and two different places and neither worked.  Could you clarify where to place the code?

I added a new button with this scrip to run on the Mouse Up action.  Nothing happened.

Then I tried to add it to the 'Reset Form' button as a javascript to be run after the original file is saved and the rest of the form reset.  No error message, just didn't do anything either.

Thank you try67!  If anyone can figure it out, you can!  🙂

Translate
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
Community Expert ,
Jan 31, 2016 Jan 31, 2016

Did you check the JS Console? Also, did you adjust the page number in the code, if necessary?

Translate
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
Community Beginner ,
Jan 31, 2016 Jan 31, 2016

I think you are giving me too much credit for knowing what exactly I'm doing!

I checked the JS console and this is the error message:

TypeError: this.syncAnnotsScan is not a function

1:Field:Mouse Up

There is only one page to the attachment and only one attachment, so do I need to adjust the code?  If so, what part of the code needs to change?

Thank you for your patience with my lack of knowledge!

Translate
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
Community Expert ,
Jan 31, 2016 Jan 31, 2016

Sorry, that's my bad... It needs to be:

this.syncAnnotScan();

Translate
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
Community Beginner ,
Jan 31, 2016 Jan 31, 2016
LATEST

Once again try67, THANK YOU!  Works perfectly! 

Translate
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