Acrobat Pro Javascript is unstable, working and not working
Copy link to clipboard
Copied
I've created a series of forms in Acrobat Pro that the Javascript works works only some of the time, or stops working, or works differently on different platforms.
Created and tested in Acrobat Pro on my computer, but also Acrobat Pro on MacBookPro, Windows 11, and using Acrobat Reader on all platforms. All software has Javascript enabled.
The simple form only has 1 recurring script attached to 8 similar fields to change the field color depending on the value of what's in the field. It works initially, then make any other changes to the form (no other Javascript) and save again... then the field scripts (sometimes) stop working when testing or opening it again to fill. Sometimes they don't work at all, even though nothing from those fields has changed.
I thought maybe saving a version that is Reader optimised (Save as Other -> Reader Extended PDF -> Enable more Tools) might work, but it's just as unstable as with Acrobat Pro.
The finished form is intended to be used as a template for a client and repair orders. So it will definitely need to be opened repeatedly and Saved As a new document each time (Buttons with Save As, Print, and Reset events are also on the form)
Question: how to save the PDF so that the Javascript always works and as many times it needs to be opened. And across Acrobat Pro and Reader with Javascript enabled, regardless of computer platform.
My Computer:
Apple M1 Max
Acrobat Build: 23.6.20380.0 (Latest Version)
Script attached to a text field->format:
// Custom Format Script
var MyGreen = ["RGB", 210/255, 255/255, 207/255];
var MyYellow = ["RGB", 255/255, 254/255, 176/255];
var MyOrange = ["RGB", 255/255, 206/255, 153/255];
var MyRed = ["RGB", 255/255, 178/255, 178/255];
var fieldValue = event.value;
if (fieldValue === "1") {
event.target.fillColor = MyGreen; // Light green
} else if (fieldValue === "2") {
event.target.fillColor = MyYellow; // Yellow
} else if (fieldValue === "3") {
event.target.fillColor = MyOrange; // because shade of orange
} else if (fieldValue === "4") {
event.target.fillColor = MyRed; // Red
} else {
// Reset the background color to default if not 1, 2, 3, or 4
event.target.fillColor = color.transparent;
}
Notes:
1. I have tried removing the "else" statement at the end. It works, but still unstable;
2. In a separate form being developed at the same time, I have the same main issue, but also Button scripts to spawn pages, page number scripts... sometimes work, sometimes don't, and again sometimes stop after reopening the same PDF.
Most grateful if anyone can help how I should save these forms so that the Javascript predictably works... ALL OF THE TIME..
Copy link to clipboard
Copied
Hi,
Try using the loose equality (==) instead of the strict equality (===)!
// Custom Format Script
var MyGreen = ["RGB", 210/255, 255/255, 207/255];
var MyYellow = ["RGB", 255/255, 254/255, 176/255];
var MyOrange = ["RGB", 255/255, 206/255, 153/255];
var MyRed = ["RGB", 255/255, 178/255, 178/255];
var fieldValue = event.value;
if (fieldValue == "1") {
event.target.fillColor = MyGreen; // Light green
} else if (fieldValue == "2") {
event.target.fillColor = MyYellow; // Yellow
} else if (fieldValue == "3") {
event.target.fillColor = MyOrange; // because shade of orange
} else if (fieldValue == "4") {
event.target.fillColor = MyRed; // Red
} else {
// Reset the background color to default if not 1, 2, 3, or 4
event.target.fillColor = color.transparent;
}
Let me know!
@+
Copy link to clipboard
Copied
Earlier, I was able to use the original script and save a final PDF that is working as expected here.
@bebarth - Thank you for the fast reply! I've changed the script and saved as a new version -- and it also seems to be stable. I've zipped both and put them on a colleagues Dropbox for further testing. Our thinking is that possibly Dropbox is screwing with our PDF files (security), similar to the way a lot of email providers by stripping the Javascript. If this is the case, we're out of luck being able to use these forms anyway.
I'm awaiting my colleague's reply, and will report the result. Odd if just that "strictness" even caused buttons to break.
Copy link to clipboard
Copied
@bebarth - Once again Thank You. My colleague has now tested both PDFs.
1. Oddly enough, the original with my "strict equality" AND your edited script are both working.
2. However, if I add dynamic page numbering (text field + javascript; see below), it all stops working again, plus numbering works on my side, but not for my collegue (starts counting at 2 instead of 1 with no spawned templates yet added)
This script, found here in the forum:
event.target.value = this.pageNum+1;
or
event.target.value = this.pageNum+1 + " / " + this.numPages;
==> You must add "calculateNow()" at the end of the spawning script to update these fields.
We're attempting to use the one with " / "... and it causes the PDF to become unstable again, including the other scripts (buttons stop working)
3. One thing did seem to help, at least before page numbering, was to save the PDF, then save a copy with "Save As". The Save As copy works correctly (almost) every time.
So once again.... WHY are Acrobat Pro forms so unstable?
Is this just the nature of the Acrobat software?
Or Javascript being inherantly "unsecure" and possibly being blocked at system level?
Copy link to clipboard
Copied
Sorry, but I don't think "the pdf become unstable"... but I can't tell you what's happening!
Try to share a file then I'll let you know if it works fine for me.
@+
Copy link to clipboard
Copied
Acrobat JS is pretty reliable in some things, but not so much in others. But what you're trying to do falls within the first category, IF the file is opened in a compliant application, such as Adobe Acrobat or Reader.
If this is a calculation script then you need to replace event.target.value with event.value, though.
Also, you need to understand the event model. Spawning a page does not cause fields to re-calculate, so you have to do it manually by calling the calculateNow method. This does not mean it's "unstable", only that it doesn't work the way you expect it to.
Copy link to clipboard
Copied
There are only the same format script on 8 fields in 8 rows on page 1... and 12 fields/12rows in the named template page, which is spawned from an "Add Page" button.
I tried to add dynamic page numbers from a script (above) that I found here in the forums; also adding "calculateNow()" to both page 1, the template (spawned) page, and a last page which is also a named template and spawned.
This causes the field format scripts, and the add page buttons, to stop working after I save the file, and reopen it.
Unfortunately, I can't attach that client file for others to check, but I can see if I can make one without identifying info during the holiday break.
Thank you for all of your info and help, and wishing all a Happy Holiday Season!

