Code to destroy annotations doesn't seem to be executed
DHeadrick Feb 21, 2012 4:03 AMHi,
My PDF document contains JavaScript that removes annotations as one of its initialization steps. This is because the main body of the program creates numerous annotations. If the user saves the PDF document (against the advice I provide), the just-created annotations will all be saved within the document. When the user next opens the document, the just-saved annotations will still be there and then the program will create new annotations, which will be "overlaid" on the existing annotations.
So, I've added the standard annotation-destroying code during my JavaScript initialization that everyone seems to use:
var annots = this.getAnnots( { nPage:0} ); // my document only has 1 page
if (annots != null)
{
console.println("Annots @ entry = " + annots.length); // print the number of annotations to be deleted
for (var i = 0; i < annots.length; i++)
annots[i].destroy();
}
The problem is that the above code isn't executed during initialization, and the number of sets of annotation embedded in the document just keeps climbing each time I open/save the document.
I can force the JavaScript to be re-executed by doing the following (in Acrobat 9):
Advanced > Document Processing > Document JavaScripts...
This brings up the "JavaScript Functions" dialog. When I close this dialog, the JavaScript is re-executed and the annotations are all deleted as evidenced by the console.println statement displaying how many annotations are deleted.
So, my question is: How do I make sure that the annotation-destroying code is executed when I open the document each time?
By opening/saving/closing the document 10 times, it will create 10 sets of duplicate annotations in the document. Only when I bring up and then close the "JavaScript Functions" dialog will the 10 sets of annotations be deleted as evidenced by the console.println statement. Of course, my Adobe Reader-based users won't have this option.
I think my question translates into: How do I make sure that the JavaScript engine correctly recognizes that there are pre-existing annotations when I open my PDF document? Currently, this recognition only seems to occur when I use the "JavaScript Functions" dialog to re-excute my code.
Thanks for any insights anyone has regarding my question!
Dave


