简体   繁体   English

如何使用 Javascript 计算 Acrobat PDF 中的字段?

[英]How to calculate fields in Acrobat PDF using Javascript?

Calculating fields Acrobat pdf Javascript the total amount displays incorrectly when I skip a field but It works correctly when each field has a value from top to down.计算字段 Acrobat pdf Javascript 当我跳过一个字段时,总金额显示不正确,但是当每个字段从上到下都有一个值时它可以正常工作。 What is wrong?怎么了?

var fields = this.getField("amount");
var a = fields.getArray();
var sum = 0;
for (i = 0; i < a.length; i++){
sum += a[i].value;

//I tried to place zeros in the fields if there are no values
if (fields.value == null){
   fields.value = 0;
}
}
//This code accept the total amount and also hides the default zeros 
if (sum > 0){
    event.value = sum;
}

else {
    event.value = "";
}



不正确

这有效

In such a loop, before adding up the running total, I usually check whether a field has a value by comparing its value with its defaultValue .在这样的循环中,在将运行总数相加之前,我通常通过将其value与其defaultValue进行比较来检查字段是否具有值。 The line looks like this:该行如下所示:

if (this.getField("myField").value != this.getField("myField").defaultValue) {

Normally, comparing on value level is sufficient, but if 0 is a legal value, the comparison has to include the type.通常,在值级别上进行比较就足够了,但如果 0 是合法值,则比较必须包括类型。 Then it looks like this:然后它看起来像这样:

if (this.getField("myField").value !== this.getField("myField").defaultValue) {

And that should do it.那应该这样做。

To fixed this, Javascript Addition operator ``+``` can also be used to add (concatenate) strings.为了解决这个问题,Javascript 加法运算符 ``+``` 也可用于添加(连接)字符串。 The empty field was acting as string so you will need to escape the string by appending + to the values.空字段充当字符串,因此您需要通过将 + 附加到值来转义字符串。

sum += +a[i].value;

It is the same as var fields = +this.getField("amount");var fields = +this.getField("amount");

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

相关问题 如何在Acrobat中使用javascript获取PDF表单字段的所有键值对? - How to get all key-value pairs of a PDF form fields using javascript in Acrobat? 在Adobe Acrobat X Pro中,如何使用javascript以acrobat pdf格式预填充文本字段? - In Adobe Acrobat X Pro, how do I use javascript to prefill text fields in an acrobat pdf form? 在Apple上使用Acrobat Javascript打开pdf - Opening pdf using Acrobat Javascript on Apple 使用Acrobat Pro和Javascript制作PDF - Using Acrobat Pro and Javascript to make PDF 如何在Acrobat PDF上应用密码提示JavaScript - How to apply password prompt javascript on acrobat pdf Adobe Acrobat-使用JavaScript遍历PDF中的所有字段时出错 - Adobe Acrobat - Error Iterating Over All Fields in a PDF with JavaScript Acrobat PDF-使用Javascript,如何在打开文档时生成唯一编号(仅当字段为空时)? - Acrobat PDF - How to generate unique number when document opens - only if field is blank - using Javascript? 如何使用Adobe Acrobat JavaScript从pdf的特定层读取文本 - How to read text from a certain layer of a pdf using adobe acrobat javascript 如何在Adobe Acrobat Reader 2017中使用Javascript访问3d pdf中的对象? - How to access the object in 3d pdf using Javascript in Adobe Acrobat Reader 2017? 如何使用acrobat pdf中的javascript从一页到另一页获取值? - How to fetch values from one page to another using javascript in acrobat pdf?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM