问题
I have been given a PDF file with a form. The form is not formatted as a table. My requirement is to extract the form field values, and write them to a CSV file which can be imported into Excel. I have tried using the automated "Merge data files to Spreadsheet" menu item in Acrobat Pro, but the output includes both the labels and form field values. I am interested in mostly just the form field values.
I would like to use JavaScript to extract the form data, and instruct JavaScript how to write the CSV (since I know what the end spreadsheet should look like). I got as far as extracting the form fields:
this.getField("Today_s_Date").value;
And following this post: How to write a text file in Acrobat Javascript , I tried to write to CSV using:
var cMyC = "abc";
var doc = this.createDataObject({cName: "test.txt", cValue: cMyC});
but I get the following error:
"SyntaxError: syntax error 1:Console:Exec"
Ideally, I do not want to use an online third party tool to do this, because the data is sensitive. But please let me know if you have suggestions. The ideal output will be a CSV file that an end business user can open in Excel to see the spreadsheet format of her choice.
Has anyone done this before? Open to hearing any alternative solutions as well. Thanks in advance!
回答1:
Your code should work, make sure you are selecting the entire code when running it in the console.
For security reasons you are limited in what you can output from Acrobat without user interaction. There is a good discussion of what can be output from PDF's here, and if you haven't already, be sure to check out what's possible with exportDataObject()
in the reference.
An example to get you going -- you could place a button on the form that would iterate through each of the fields in the form, adding them to an array that could then be output as a csv.
Something like:
var fieldValues = [];
for (var i = 0; i < this.numFields; i++)
fieldValues.push(this.getField(this.getNthFieldName(i)).value);
this.createDataObject('output.csv', fieldValues.join());
this.exportDataObject({ cName:'output.csv', nLaunch:'2'});
In this example the .csv would be opened as a temporary file by the default csv program on the machine. Alternatively you could omit nLaunch, and give the user a file save dialog.
来源:https://stackoverflow.com/questions/30579408/extract-pdf-form-data-using-javascript-and-write-to-csv-file