在pdf中使用javascript更改多颜色按钮

Multi-Color changing button using javascript in a pdf

本文关键字:颜色 按钮 javascript pdf      更新时间:2023-09-26

我似乎不能再进一步了。比起程序员,我更喜欢图形。
我需要一个按钮循环从白色到灰色到红色再到黑色到白色每次按下按钮。在一个pdf文件中将有超过一百个这样的按钮。
这是我到目前为止所尝试的:

if (this.getField("Button1").fillColor = ["RGB",1,1,1])
  {
  app.alert({ cMsg: "Switch to Grey", });
  this.getField("Button1").fillColor = ["RGB",.5,.5,.5];
  };
if (this.getField("Button1").fillColor = ["RGB",.5,.5,.5])
  {
  app.alert({ cMsg: "Switch to Red", });
  this.getField("Button1").fillColor = ["RGB",1,0,0];
  };
if (this.getField("Button1").fillColor = ["RGB",1,0,0])
  {
  app.alert({ cMsg: "Switch to Black", });
  this.getField("Button1").fillColor = ["RGB",0,0,0];
  };
if (this.getField("Button1").fillColor = ["RGB",0,0,0])
  {
  app.alert({ cMsg: "Switch to White", });
  this.getField("Button1").fillColor = ["RGB",1,1,1];
  };

这可能吗?由于

更新:

使用您自己的代码,它看起来像这样:
if (this.getField("Button1").fillColor == ["RGB",1,1,1])
 {
  app.alert({ cMsg: "Switch to Grey", });
  this.getField("Button1").fillColor = ["RGB",.5,.5,.5];
 }
else if (this.getField("Button1").fillColor == ["RGB",.5,.5,.5])
{
 app.alert({ cMsg: "Switch to Red", });
 this.getField("Button1").fillColor = ["RGB",1,0,0];
}
else if (this.getField("Button1").fillColor == ["RGB",1,0,0])
{
 app.alert({ cMsg: "Switch to Black", });
 this.getField("Button1").fillColor = ["RGB",0,0,0];
}
else if (this.getField("Button1").fillColor == ["RGB",0,0,0])
{
 app.alert({ cMsg: "Switch to White", });
 this.getField("Button1").fillColor = ["RGB",1,1,1];
}

我之前的回答:(仅供参考)

下面这个是纯粹的JavaScript和HTML,而不是PDF,但你会看到它是如何完成的。

你可以在JavaScript中使用getElementByIdrgb:

  document.getElementById("button1").style.backgroundColor == "rgb(0, 255, 0)"

我在这里举个例子。比如你有一个蓝色的按钮,比如

<div>
<button id="button1" onclick="getcolor()" style="background-color:#0000FF; padding:5px;">Colored Button - Blue</button>
</div>

我们想把它改成绿色如果它是绿色我们想把它改成蓝色,这样你就会有这样的JavaScript:

function getcolor()
{
  var button_color = document.getElementById("button1").style.backgroundColor;
  if (button_color == "rgb(255, 255, 255)")
  {
    alert("Switch color to Grey");
    document.getElementById("button1").style.backgroundColor = "rgb(128, 128, 128)";
 }
 else if (button_color == "rgb(128, 128, 128)")
 {
    alert("Switch color to Red");
    document.getElementById("button1").style.backgroundColor = "rgb(255, 0, 0)";       
 }
 else if (button_color == "rgb(255, 0, 0)")
 {
     alert("Switch color to Black");
     document.getElementById("button1").style.backgroundColor = "rgb(0, 0, 0)";       
 }
 else if (button_color == "rgb(0, 0, 0)")
 {
     alert("Switch color to White");
     document.getElementById("button1").style.backgroundColor = "rgb(255, 255, 255)";       
 }
}

顺便说一下,在比较时要小心rgb中的空格,例如蓝色,在比较时不应该是rgb(0,0,255),它在颜色的每个十进制值之后没有空格,如:

 if (button_color == "rgb(0,0,255)")

但是应该是这样的:

 if (button_color == "rgb(0, 0, 255)")

参见my Updated Demo