JavaScript - FillColor change menu - does not work
Copy link to clipboard
Copied
Hi everyone,
I've tried to find my solution on tons of topics, but it is not solving my problem.
I have a JavaScript on Adobe Acrobat to create a drop down menu with colors. Anyone using this PDF should be able to have this menu on each text box to be able to can change the background color individually.
The menu shows and you can select an element, but the visual effect does not work. Looks like it can be the "c[result]" code, but I don't know what could replace it.
My code is this one:
______
var c = {
'White':['255,255,255'],
'Red':['255,0,0'],
'Green':['0,255,0'],
'Grey':[170,170,170],
}
var n = new Array('Background color');
for (var i in c) n.push(i);
var result = app.popUpMenu(n);
if (result) {
this.fillColor = c[result].toString();
}
______
Pleas help! Thanks
Copy link to clipboard
Copied
Are there any messages in the JavaScript console? Also, try adding an else so if result is unexpected you get an alert.
Copy link to clipboard
Copied
Also I could add a an else, but I don't know what line would suits best my interest.
Original script was this original didn't work either):
var c = {
'White (RGB)':['255,255,255'],
'Red (RGB)':['255,0,0'],
'Green (RGB)':['0,255,0'],
'Blue (RGB)':['0,0,255'],
'Dust (RGB)':['153,26,15'],
'Dark blue (RGB)':['0,15,92'],
'Purple (RGB)':[120,0,119],
'Grey (RGB)':[200,200,200],
}
var n = new Array('Fill color :');
for (var i in c) n.push(i);
var result = app.popUpMenu(n);
if (result) {
this.fillColor = c[result].toString();
this.border.corner.presence = "hidden";
}
Copy link to clipboard
Copied
Also, what is this color value - ['255,255,255'] - this is an array containing one string element, not at all what a color value needs to be (it is an array of one string and some numbers in the range 0 to 1 - no 255 in it anywhere! See https://acrobatusers.com/tutorials/using-colors-acrobat-javascript/
Copy link to clipboard
Copied
Copy link to clipboard
Copied
No, they are not the RGB values for white in Acrobat JavaScript. You can't guess, read the document, which is much more accessible than the JavaScript API in this.
Copy link to clipboard
Copied
JavaScript console, Ctrl+J. Virtually impossible to get a program right without it, even for people with decades of experience in writing it...
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Ah, I think I found the code you copied at https://answers.acrobatusers.com/Change-background-color-on-mouse-click-q289742.aspx Unfortunately, this is JavaScript for LiveCycle Designer, not Acrobat. A nasty trap for the unwary, because it also works with PDF, but the scripting is completely different. You NEED the Acrobat JavaScript Reference. https://www.adobe.com/content/dam/acom/en/devnet/acrobat/pdfs/js_api_reference.pdf (warning: this is very large, 769 pages to study)
Copy link to clipboard
Copied
You have a rather simple issue here. The color values specified in your array are not Acrobat JavaScript Colors. All you need to do is change the color values to use the correct specification:
Here's an article on the topic:
https://acrobatusers.com/tutorials/using-colors-acrobat-javascript/
Use the Acrobat JavaScript Reference early and often

