Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,3 +236,40 @@ This will print out this
"nascar" : "Off"
};
````

#### 5. Get Field JSON
````javascript
var pdfFiller = require('pdffiller');

var sourcePDF = "test/test.pdf";

// Override the default field name regex. Default: /FieldName: ([^\n]*)/
var nameRegex = null;

var FDF_data = pdfFiller.generateFieldJson( sourcePDF, nameRegex, function(err, jsonData) {
if (err) throw err;
console.log(jsonData);
});
````

This will produce an array of fields like the following.
```
[
{
"fieldFlags": "0",
"title" : "first_name",
"fieldValue": "",
"fieldType": "Text",
"fieldDefault": "",
"fieldOptions": []
}, {
"fieldFlags": "0",
"title" : "first_name",
"fieldValue": "",
"fieldType": "Button",
"fieldDefault": "",
"fieldOptions": ["On", "Off"]
},
]
```
This can be very helpful when trying to determine what a valid value is to make a checkbox be checked when filling out the PDF.
26 changes: 25 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@
var regName = /FieldName: ([^\n]*)/,
regType = /FieldType: ([A-Za-z\t .]+)/,
regFlags = /FieldFlags: ([0-9\t .]+)/,
regValue = /FieldValue: ([^\n]*)/,
regDefault = /FieldValueDefault: ([^\n]*)/,
regOptions = /(FieldStateOption: ([^\n]*))/g,
fieldArray = [],
currField = {};

Expand Down Expand Up @@ -81,7 +84,28 @@
currField['fieldFlags'] = '';
}

currField['fieldValue'] = '';
if(field.match(regValue)){
currField['fieldValue'] = field.match(regValue)[1].trim()|| '';
}else{
currField['fieldValue'] = '';
}

if(field.match(regDefault)){
currField['fieldDefault'] = field.match(regDefault)[1].trim()|| '';
}else{
currField['fieldDefault'] = '';
}

if(field.match(regOptions)){
var matches = []
field.replace(/(FieldStateOption: ([^\n]*))/g, (m)=>{
var match = m.match(/FieldStateOption: ([^\n]*)/)[1].trim() || false;
if(match) matches.push(match);
})
currField['fieldOptions'] = matches
}else{
currField['fieldOptions'] = [];
}

fieldArray.push(currField);
});
Expand Down
Loading