You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

无需手动操作Adobe Acrobat,程序化在PDF中添加自动求和计算字段的可行方案咨询

无需手动操作Adobe Acrobat,程序化在PDF中添加自动求和计算字段的可行方案咨询

我正在尝试在Adobe PDF中插入一个名为SummedAmount的计算求和字段,这个字段需要对以下文本字段的值求和:perc1 + perc2 + perc3 + perc4 + perc5

在Adobe Acrobat桌面端,我可以通过UI轻松创建这个计算,但我现在用Python(搭配pdfrw等库)做PDF表单填充和处理的自动化,这个过程会移除SummedAmount字段的计算逻辑,而且我没办法通过编程把它加回去。

我怀疑Adobe有安全限制阻止我完全自动化这个操作。我尝试过通过注释注入基于JavaScript的求和计算,虽然能成功插入,但除非我手动在Adobe表单设置里编辑文本字段,否则计算不会触发——这就失去了自动化的意义。

我已经尝试过的方法

我尝试过通过注释注入JavaScript计算,确保了以下几点:

  • SummedAmount字段已存在且设置为只读
  • 通过AcroForm动作添加了计算脚本
  • 注入了文档级JavaScript

尽管做了这些,计算还是只有在手动用Adobe Acrobat编辑表单后才会运行。

我的问题

  1. 有没有办法不用手动操作Adobe Acrobat桌面端,就能程序化插入求和字段?
  2. 有没有其他Python库或方法可以设置表单字段的计算逻辑?
  3. Adobe是不是因为安全原因阻止这种修改?如果是,有没有可行的变通方案?

编辑:更新了代码,修复了百分比字段和SummedAmount的文本对齐、字体大小以及继承的字段位置问题。感谢建议!

fields= {
    "SummedAmount": (10, 10, 100, 30),  # Arbitrary position
    "Percentage1": (10, 40, 100, 60),
    "Percentage2":(10, 70, 100, 90),
    "Percentage3":(10, 100, 100, 120),
    "Percentage4":(10, 130, 100, 150),
    "Percentage5":(10, 160, 100, 180)

}

import pymupdf

def add_calculation_to_pdf(input_pdf, output_pdf, fields):
    """Adds calculation fields to an existing PDF using provided field positions."""
# Open the PDF document
doc = pymupdf.open(input_pdf)
page = doc[0]  # Assuming calculations are on the first page
page_height = page.rect.height  # Get the page height for Y-axis flipping

# Create widgets for Percentage fields
percentage_fields = ["Percentage1", "Percentage2", "Percentage3", "Percentage4", "Percentage5"]

for field_name in percentage_fields:
    if field_name in fields:
        x1, y1, x2, y2 = fields[field_name]
        x1, y1, x2, y2 = float(x1), float(y1), float(x2), float(y2)
        w = pymupdf.Widget()
        w.field_name = field_name
        w.rect = pymupdf.Rect(x1, page_height - y2, x2, page_height - y1)
        w.field_type = pymupdf.PDF_WIDGET_TYPE_TEXT
        w.field_value = "0"
        # w.text_align = 1  # Center the text
        w.field_flags = 2  # Editable field
        w.text_font = "Times-Roman"  # Set font to Times-Roman
        w.text_fontsize = 12  # Set font size to 12
        page.add_widget(w)

# Create SummedAmount widget using inherited field positions
if "SummedAmount" in fields:
    x1, y1, x2, y2 = fields["SummedAmount"]
    x1, y1, x2, y2 = float(x1), float(y1), float(x2), float(y2)
    w = pymupdf.Widget()
    w.field_name = "SummedAmount"
    w.rect = pymupdf.Rect(x1, page_height - y2, x2, page_height - y1)
    w.field_type = pymupdf.PDF_WIDGET_TYPE_TEXT
    w.field_value = "0.00"
    # w.text_align = 1  # Center the text
    w.text_font = "Times-Roman"  # Set font to Times-Roman
    w.text_fontsize = 12  # Set font size to 12
    w.script_calc = 'AFSimple_Calculate("SUM", new Array("Percentage1", "Percentage2", "Percentage3", "Percentage4", "Percentage5"));'
    page.add_widget(w)

# Save the updated PDF
doc.save(output_pdf, pretty=True)

备注:内容来源于stack exchange,提问作者Jacob Lamkey

火山引擎 最新活动