我有5个字段。数量、零售、折扣、总折扣和总计。我想在 1 列中显示总折扣,然后在总列中显示折扣总额。这是在 adobe PDF 文档中执行的 javascript。
数量 | 零售(零售价) | 折扣%(DiscRow1) | T 折扣(DiscountRow1) | 总计 |
---|---|---|---|---|
1 | 100 | .10 | 10 美元 | 90 美元 |
function calculateDiscount() {
var qty = this.getField("Qty").value;
var retailPrice = this.getField("RetailPrice").value;
var discountPerc= this.getField("DiscRow1").value;
retailPrice = parseFloat(retailPrice);
var totalPrice = qty * retailPrice;
var discountPercentage = discountPerc;
var discountAmount = totalPrice * discountPercentage;
this.getField("DiscountRow1").value = discountAmount.toFixed(2); // Fixed to 2 decimal places
}
this.getField("Qty").setAction("Calculate", calculateDiscount);
this.getField("RetailPrice").setAction("Calculate", calculateDiscount);
动态 PDF 的问题之一是它需要在所有阅读器中正常工作,而 JS 不是一个好主意,因为它会触发客户端设备中的重建。在这里,我们在选项卡的左上角看到警告 asterisk
你需要这个对象作为最后一个字段
4 0 obj
<</AA<</C<</JS(var get1 = this.getField\("Qty"\).valueAsString;\r\nvar get2 = this.getField\("Retail"\).valueAsString;\r\nvar get3 = this.getField\("TDiscount"\).valueAsString;\r\nvar total = \(get1*get2\)-\(get3\);\r\nevent.value = total;)/S/JavaScript>>/F<</JS(AFNumber_Format\(2, 0, 0, 0, "\\u0024 ", true\))/S/JavaScript>>/K<</JS(AFNumber_Keystroke\(2, 0, 0, 0, "\\u0024 ", true\))/S/JavaScript>>>>/AP<</N 11 0 R>>/DA(0 g /Helv 0 Tf)/F 4/Ff 1/FT/Tx/M(D:20240513200224+01'00')/MK<</BC[0 0 0]>>/P 11 0 R/Q 2/Rect[296.8653 503 356.8653 525]/Subtype/Widget/T(Total)/Type/Annot/V(90)>>
endobj
需要的是类似的输入
var get1 = this.getField("Qty").valueAsString;
var get2 = this.getField("Retail").valueAsString;
var get3 = this.getField("TDiscount").valueAsString;
var total = (get1*get2)-(get3);
event.value = total;