Skip to main content
Participating Frequently
December 21, 2023
Question

Acrobat Pro Javascript is unstable, working and not working

  • December 21, 2023
  • 1 reply
  • 767 views

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..

 

This topic has been closed for replies.

1 reply

bebarth
Community Expert
Community Expert
December 21, 2023

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!

@+

Participating Frequently
December 21, 2023

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.