在Adobe Acrobat DC 中复制表单域后新的表单域和旧的表单域里的内容会自动同步。取消同步必须手动给新表单域逐个改名,在一次复制一整页几十几百个表单域时非常麻烦。我在网上找到一个叫做David Vielhuber的人的博客帖子1,里面提到Adobe Acrobat可以用javascript给表单里的元素批量重命名。他写了一段把表单名里的.改成#的方法,我把他的代码稍微修改了一下,就可以实现让复制的新表单和旧表单取消链接,并且删除新表单的内容了。

具体步骤:

  1. 正常复制带表单的页,此时两页的表单内容会自动同步。
  2. 在Acrobat的首选项(Ctrl + K)里勾选启用Acrobat JavaScript

  1. 然后Ctrl+J打开Javascript控制台。在下面的“查看”里选择“控制台”
  2. 然后粘贴下面的代码,Ctrl+A全选,再Ctrl+Enter运行,几页的表单就可以分别填写不同的内容而不会自动同步啦。代码参考了Adobe的官方说明文件2
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
var fields = [];
for (var i = 0; i < this.numFields; i++) {
    fields.push(this.getNthFieldName(i));
}
for (var i = 0; i < fields.length; i++) {
    var source_name = fields[i];

    // modifications to name
    var target_name = source_name;
    // other replacements (examples)
    /*
    target_name = source_name.replace(/\./g, '#');
    target_name = target_name.toLowerCase();
    target_name = target_name.replace(/\s/g, '');
    target_name = target_name.replace(/\ß/g, 'ss');
    target_name = target_name.replace(/\-|\/|\*|\.|\;|\:/g, '_');
    */

    var source_field = this.getField(source_name);

    if (source_field !== null && source_field.page.length > 1) {
        // debug output (if needed)
        //console.println(JSON.stringify([target_name, source_field.type, source_field.page, source_field.rect]));
        for (var j in source_field.page) {
            target_name = source_name + "#" + j;
            var target_field = this.addField(
                target_name,
                source_field.type,
                source_field.page[j],
                source_field.rect
            );
            var props = [
                'alignment',
                'borderStyle',
                'buttonAlignX',
                'buttonAlignY',
                'buttonFitBounds',
                'buttonScaleHow',
                'buttonScaleWhen',
                'comb',
                'display',
                'doNotScroll',
                'editable',
                'exportValues',
                'fileSelect',
                'fillColor',
                'highlight',
                'lineWidth',
                'multiline',
                'multipleSelection',
                'numItems',
                'password',
                'readonly',
                'richText',
                'richValue',
                'rotation',
                'strokeColor',
                'style',
                'textColor',
                'textFont',
                'textSize',
                'userName',
            ];
            for (var p = 0; p < props.length; p++) {
                if (testField(source_field, props[p])) {
                    target_field[props[p]] = source_field[props[p]];
                }
            }
            if (j === '0') {
                target_field.value = source_field.value;
            }
        }
        this.removeField(source_name);
    }
}

function testField(field, prop) {
    try {
        var tprop = field[prop];
        return true;
    } catch (e) {
        return false;
    }
}

Acrobat里的Javascript控制台


  1. https://vielhuber.de/zh-cn/blog-zh-cn/pdf-form-fields-en-masse/ ↩︎

  2. https://opensource.adobe.com/dc-acrobat-sdk-docs/acrobatsdk/pdfs/acrobatsdk_jsdevguide.pdf ↩︎