我正在使用Node服务器,我使用pdftk从PDF中提取字段数据以注入表单数据。
我正在尝试使用Field Names的JSON对象进行迭代,但我似乎无法正确使用它。 它可能需要是一个循环,因为它将根据PDF中的字段进行更改。
这是我输出的字符串。
---
FieldType: Text
FieldName: topmostSubform[0].Page1[0].p1-t17[0]
FieldFlags: 8388608
FieldValue:
FieldJustification: Center
FieldMaxLength: 10
---
FieldType: Text
FieldName: topmostSubform[0].Page1[0].p1-t20[0]
FieldFlags: 8388608
FieldValue:
FieldJustification: Center
FieldMaxLength: 10
---
FieldType: Button
FieldName: topmostSubform[0].Page1[0].p1-cb7[0]
FieldFlags: 0
FieldValue:
FieldJustification: Left
FieldStateOption: 1
FieldStateOption: Off
---
FieldType: Text
FieldName: topmostSubform[0].Page1[0].p1-t38[0]
FieldFlags: 8388608
FieldValue:
FieldJustification: Center
---
FieldType: Text
FieldName: topmostSubform[0].Page1[0].p1-t50[0]
FieldFlags: 8388608
FieldValue:
FieldJustification: Left
你会推荐正则表达式吗?解决这个问题的最佳方法是什么?
答案 0 :(得分:0)
反复使用split
将其分解为其组件:
var input = '---\n\
FieldType: Text\n\
FieldName: topmostSubform[0].Page1[0].p1-t17[0]\n\
FieldFlags: 8388608\n\
FieldValue:\n\
FieldJustification: Center\n\
FieldMaxLength: 10\n\
---\n\
FieldType: Text\n\
FieldName: topmostSubform[0].Page1[0].p1-t20[0]\n\
FieldFlags: 8388608\n\
FieldValue:\n\
FieldJustification: Center\n\
FieldMaxLength: 10\n\
---\n\
FieldType: Button\n\
FieldName: topmostSubform[0].Page1[0].p1-cb7[0]\n\
FieldFlags: 0\n\
FieldValue:\n\
FieldJustification: Left\n\
FieldStateOption: 1\n\
FieldStateOption: Off\n\
---\n\
FieldType: Text\n\
FieldName: topmostSubform[0].Page1[0].p1-t38[0]\n\
FieldFlags: 8388608\n\
FieldValue:\n\
FieldJustification: Center\n\
---\n\
FieldType: Text\n\
FieldName: topmostSubform[0].Page1[0].p1-t50[0]\n\
FieldFlags: 8388608\n\
FieldValue:\n\
FieldJustification: Left';
var fields = [];
var field_strings = input.split(/[\r\n]*---[\r\n]*/);
for (var i = 0; i < field_strings.length; i++) {
if (field_strings[i] == '') { // Skip blank field at beginning
continue;
}
var obj = {};
var props_strings = field_strings[i].split('\n');
for (var j = 0; j < props_strings.length; j++) {
var keyvalue = props_strings[j].split(':');
obj[keyvalue[0]] = keyvalue[1].trim();
}
fields.push(obj);
}
console.log(fields);
&#13;