The script places the line correctly when I run it on the original file (media box is 1/2" larger than the crop box on all sides). When I run the same script on the same file after is has had the media box set to match the crop box, the line is not in the correct location.
I am only changing the media box to match the crop box because some of the files I will need this script for may have already had the boxes monkeyed with by others before I get them and I'm trying to account for all scenarios.
How do I post files? I'm not seeing where I can do that.
Ok, so after looking over your documents I remember this one. The Acrobat JS DOM is misinterpreting the page geometry and translating coordinates incorrectly by the amount the media origin is shifted. I investigated this a long time ago and I'm pretty sure I filed a bug report. I'll file another one. I have not seen it as an issue for a very long time, so it must be rare. Why are you seeing it so often?
I do not believe there is any way to detect this situation from JavaScript, but you are correct that the preflight "Set Media Box Origin" fixup does the trick. It shifts all page content, including annotations by the origin offset.
So you can execute a preflight profile in JavaScript using the "doc.preflight()" function. However, to do this you have to first create a new named Preflight Profile that has the media box fixup as it's operation. The preflight function is blocking, so the addAnnot() functions can be called after and should work. You should create this all as either a folder level automation, or a Acrobat DC Command