Adobe Forms可填充PDF表单点击新增人员字段组功能的实现咨询
这个需求完全可以实现,通过Adobe Acrobat Pro内置的JavaScript表单脚本能力即可完成,具体操作步骤如下:
前置准备
首先完成基础字段配置:
- 用Adobe Acrobat Pro打开目标PDF,进入「准备表单」编辑模式
- 制作默认的第一组人员字段,按规则命名:
name_0(姓名)、DOB_0(出生日期)、address_0(地址) - 调整好第一组字段的大小、字体、位置、对齐方式,后续新增字段会直接复用该配置
步骤1:添加触发按钮
- 在「准备表单」模式的工具栏选择「按钮」工具,在表单合适位置绘制按钮控件
- 按钮命名为
addPersonBtn,按钮显示文本设置为「添加其他人员」 - 打开按钮属性面板,切换到「动作」标签,触发条件选择「鼠标松开」,动作为「运行JavaScript」,点击「添加」进入代码编辑界面
步骤2:写入新增字段逻辑
将以下代码粘贴到JavaScript编辑框中保存即可:
// 计算当前已有人员组数量,每组3个字段 var groupCount = Math.floor(this.numFields / 3); // 定义新字段组的纵向偏移量,单位为磅,可根据排版需求调整 var offsetY = 22; // 新增姓名字段 var newName = this.addField("name_"+groupCount, "text", 0, [ this.getField("name_0").rect[0], this.getField("name_"+(groupCount-1)).rect[1] - offsetY, this.getField("name_0").rect[2], this.getField("name_"+(groupCount-1)).rect[3] - offsetY ]); // 复用默认字段的样式配置 newName.textSize = this.getField("name_0").textSize; newName.textFont = this.getField("name_0").textFont; newName.alignment = this.getField("name_0").alignment; // 新增出生日期字段 var newDob = this.addField("DOB_"+groupCount, "text", 0, [ this.getField("DOB_0").rect[0], this.getField("DOB_"+(groupCount-1)).rect[1] - offsetY, this.getField("DOB_0").rect[2], this.getField("DOB_"+(groupCount-1)).rect[3] - offsetY ]); newDob.textSize = this.getField("DOB_0").textSize; newDob.textFont = this.getField("DOB_0").textFont; newDob.alignment = this.getField("DOB_0").alignment; // 新增地址字段 var newAddr = this.addField("address_"+groupCount, "text", 0, [ this.getField("address_0").rect[0], this.getField("address_"+(groupCount-1)).rect[1] - offsetY, this.getField("address_0").rect[2], this.getField("address_"+(groupCount-1)).rect[3] - offsetY ]); newAddr.textSize = this.getField("address_0").textSize; newAddr.textFont = this.getField("address_0").textFont; newAddr.alignment = this.getField("address_0").alignment;
注意:如果需要限制最大新增人员数量,可以在代码开头添加判断逻辑,超过阈值后弹出提示终止运行即可;如果字段是横向排列,修改
rect数组的X轴偏移参数即可。
效果验证
保存PDF后用Adobe Acrobat或Adobe Reader打开,点击「添加其他人员」按钮即可自动生成新的字段组,字段名会按name_1/DOB_1/address_1、name_2/DOB_2/address_2的规则自动递增,不会和历史字段重名。
内容的提问来源于stack exchange,提问作者Sam Walker
相关产品推荐
相关产品推荐

