This repository has been archived by the owner on Oct 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
38 lines (33 loc) · 1.5 KB
/
main.js
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
'use strict';
const fse = require('fs-extra');
const pdfjsLib = require('pdfjs-dist');
const json2csv = require('json2csv');
const fields = ['question', 'fieldName', 'fieldID', 'fieldType', 'popupText', 'charLimit'];
process.argv.forEach(function (val, index, array) {
console.log(index + ': ' + val);
});
pdfjsLib.getDocument('test.pdf').then(async function (doc) {
let fieldarray = [];
let page = await doc.getPage(1);
for (let i=1; i<=doc.numPages; i++){
let page = await doc.getPage(i);
let annotations = await page.getAnnotations();
annotations.forEach(function(field) {
let fieldType = field.fieldType;
if (field.fieldType == 'Btn' && field.checkBox == true && field.radioButton == false) fieldType ='checkbox';
if (field.fieldType == 'Btn' && field.checkBox == false && field.radioButton == true) fieldType ='radio button';
if (field.fieldType == 'Ch' && field.combo == true) fieldType ='select list';
if (field.fieldType == 'Tx') fieldType = 'input text';
let fieldobj = { question: field.fieldName ,
fieldName: field.fieldName,
fieldID: field.id,
fieldType: fieldType,
popupText: field.alternativeText,
charLimit: field.maxLen
}
fieldarray.push(fieldobj);
}, this);
}
let csv = json2csv({ data: fieldarray, fields: fields, del: ';' });
await fse.writeFile('exportpdf.csv', csv);
});