简体   繁体   English

在Adobe Acrobat XI中使用JavaScript修改PDF字段值

[英]Using JavaScript in Adobe Acrobat XI to modify a PDF field value

I have two fields in a PDF I'm editing in Adobe Acrobat XI, "Text2#0" and "Text2#1". 我在Adobe Acrobat XI中编辑的PDF中有两个字段:“ Text2#0”和“ Text2#1”。

I'm trying to make it so when you enter text into the second field (Text2#1), it shows up in the first field (Text2#0), but enclosed between a pair of asterisks (this is for a barcode generator). 我试图做到这一点,所以当您在第二个字段(Text2#1)中输入文本时,它会显示在第一个字段(Text2#0)中,但会被包围在一对星号之间(这是用于条形码生成器的) 。

In the 'Text Field Properties' window for Text2#1 on the 'Actions' tab, I set a MouseUp trigger to Run a JavaScript: 在“操作”选项卡上的Text2#1的“文本字段属性”窗口中,我将MouseUp触发器设置为运行JavaScript:

var barcodeField = this.getField("Text2#0");
var barcodeLength = barcodeField.length;


if( barcodeField.charAt(0) != "*" ){
  barcodeField = "*" + barcodeField;     
};

if( barcodeField.charAt(barcodeLength - 1) != "*" ){
  barcodeField = barcodeField + "*";
};  

However, the encapsulating asterisks are never added. 但是,永远不会添加封装星号。 What am I missing? 我想念什么?

Thanks for reading! 谢谢阅读!

EDIT: 编辑:

Thanks to Max for his answer below. 感谢Max在下面的回答。 What I did was change the names of the fields as he mentioned (the barcode field was changed to "barcode" and the text field to "Text2"), and then I made a 'On Blur' trigger (when the user stops interacting with the field) associated with Text2. 我所做的就是更改他提到的字段名称(条形码字段更改为“条形码”,文本字段更改为“ Text2”),然后我创建了“模糊”触发器(当用户停止与之交互时)字段)与Text2相关联。 Here is the JS that trigger runs: 这是触发运行的JS:

getField("barcode").value = getField("Text2").valueAsString;

var barcodeField = this.getField("barcode");
var textField = this.getField("Text2");

barcodeField.value = "*" + textField.value + "*";

No surprise that the asterisks are not added. 不加星号也就不足为奇了。

The barcodeField variable is a Field Object, and a Field Object can not add strings. 条形码字段变量是字段对象,并且字段对象不能添加字符串。

In order to do something with the content of the field, you would need the value property of the Field Object. 为了对字段的内容进行处理,您将需要Field对象的value属性。

That said, your line would have to look like this: 也就是说,您的行必须如下所示:

barcodeField.value = "*" + barcodeField.value ;

However, there is a much more fundamental issue. 但是,还有一个更根本的问题。

The #1 or #2 suffix to the field name is the widget number of the field. 字段名称的后缀#1#2是该字段的小部件编号。 In Acrobat JavaScript, the key to the field is its name. 在Acrobat JavaScript中,该字段的关键字是其名称。 When you have multiple fields with the same name, they are differentiated with the widget number. 当您有多个具有相同名称的字段时,它们将以小部件编号区分。

Some Field Object properties apply to the field (and are the same for all widgets), others apply to the widget (and are different from widget to widget). 一些字段对象属性适用于该字段(所有小部件都相同),另一些属性适用于该小部件(每个小部件都不同)。 For example, rect applies to the widget. 例如, rect应用于小部件。 But value applies to the field, meaning that all fields with that name show the same. 但是value适用于该字段,这意味着所有具有该名称的字段都显示相同。

So, if you want to have a field showing the asterisks, and one not showing them, you will have to use two fields, with different field names. 因此,如果要使一个字段显示星号,而又不显示星号,则必须使用两个字段,并且使用不同的字段名称。

The example could look like this: 该示例可能如下所示:

var bcf = this.getField("barcodeField") ; // has plain value
var bcfa = this.getField("barcodeFieldA") ; // has value with asterisks

bcfa.value = "*" + bcf.value + "*" ;

If you add this code to the Calculate event of the bcfa field, you would have to change the last line to 如果将此代码添加到bcfa字段的Calculate事件中,则必须将最后一行更改为

event.value = "*" + bcf.value + "*" ;

And that should do it. 那应该做到的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM