javascriptadobeacrobat

Acrobat - Reject comment using Javascript


I have a huge pdf file with a lot of people comment. I would like to have a script (like a shortcut) to reject the selected comment (it seems not possible now to have a shortcut to this action directly).

The pdf is NOT a form.

I have been through the documentation of Adobe for Javascript (page 106 in Chrome it's not opening at the page for me) : https://opensource.adobe.com/dc-acrobat-sdk-docs/acrobatsdk/pdfs/acrobatsdk_jsdevguide.pdf#page106

and the API guide page 68 : https://opensource.adobe.com/dc-acrobat-sdk-docs/acrobatsdk/pdfs/acrobatsdk_jsapiref.pdf#page68

My test pdf (with only one comment highlithed - we do highlight text when commenting) : Screen shot of test pdf

You can download the file here is needed : Test pdf to download

The red arrow is showing the status I want to set as rejected.

(When I first start, I was trying to make a loop on all the comments, then show a dialog box which is showing the comment and add three butons to accept, reject, do nothing - but too difficult for me :) ).

I come with several version of my code, but none of them are working (I also try Bard, ChatGPT with no luck).

function rejectSelectedComment() {
  // Get the currently open PDF document
  var doc = app.activeDocument;

  // Get the currently selected comment
  var selectedComment = doc.activeAcroForm.currentPage.comments.getSelectedComment();

  // Check if a comment is selected
  if (selectedComment) {
    // Reject the selected comment
    selectedComment.reject();
  } else {
    // Alert the user that no comment is selected
    app.alert("Please select a comment to reject.");
  }
rejectSelectedComment()
}

Always getting either a doc undefined or a this.getComments is not a function

I have tried that from the console, and in an action but I can't get the doc selected. I have tried to get the script as a .js file in the Javascript folder. I am able to show some app alert. I have tried with the method this.syncAnnotScan(); describes in the Adobe pdf with no more luck;

I have check some projects on github.

The expected result should be to have the comment status set as rejected. So the user can go the the next one and apply script.

Thanks for reading !


Solution

  • The answer (from some colleagues, so I post here in case it helps someone).

    function modifyStateOfSelectedComments(){
    annots =  this.selectedAnnots
    for ( var i= 0; i< annots.length; i++) {
       states = annots[i].getStateInModel("Review");
    var a = annots[i].transitionToState("Review", "Accepted");
    }
    }
    
    // add tool button to handle the script
    app.addToolButton({cName:"myToolButton",cExec: "this.modifyStateOfSelectedComments()",cTooltext: "Push Me!",cEnable: true,nPos: 0});
    

    The last 4 lines will add a "Javascript floating Window" so you have a button to click when needed.

    For running that, I set up a Action via the action wizard > new action > More tools > execute Javascript.

    Now just to figure out if I can link the script to a keyboard shortcut (At the moment I can just select all comment I want and then press the button)