在输入文本时如何更改 PDF 表单域背景?

问题描述

我正在创建一个 PDF 表单以分发给人们。当没有在表单的任何字段中输入文本时,我希望它保持透明,但是当有人向其中输入文本时,我希望该字段的背景颜色变为白色。

I found this page 描述了如何使用 JavaScript 完成此操作,因此我将其添加到我的 PDF 中的 JavaScript 文档中,但它不起作用。这些字段继续具有认情况下相同的透明背景。

如何使用或不使用 JavaScript 实现我的目标?我正在使用 Acrobat Pro DC。谢谢!

解决方法

将此脚本放在字段的自定义格式脚本中

/* Turns off default field highlighting. Normally you'd put this in a doc level script it's just here for completeness */
app.runtimeHighlight = false;
/* The rest of this belongs in the custom format script */
var field = event.target;
if (field.value == field.defaultValue) {
    /* set the fillColor is the field value is the same as the default (generally an empty string) */
    field.fillColor = color.ltGray
}
else {
    field.fillColor = color.transparent
}

我有一个正常运行的 example file here