Import data from Excel to PDF form using Javascript

Copy link to clipboard
Copied
Hello Everyone,
I need to import some data from an excel with at least 250 rows, and 7 columns.
the first try was saving the document as tab delimited and then open and import the data manually from Adobe, IT WORKS.
now i need a step more, doing it automatically with all the rows.
I've found an user which has a solution in his page: Batch-Import Excel Data into PDF Forms - KHKonsulting LLC
he used this code to explain how to do it:
// specify the filename of the data file
var fileName = "/Users/username/tmp/data.txt"; // the tab delimited text file containing the data
var outputDir = "/Users/username/tmp/"; // make sure this ends with a '/'
var err = 0;
var idx = 0;
while (err == 0) {
err = this.importTextData(fileName, idx); // imports the next record
if (err == -1)
app.alert("Error: Cannot Open File");
else if (err == -2)
app.alert("Error: Cannot Load Data");
else if (err == 1)
app.alert("Warning: Missing Data");
else if (err == 2)
app.alert("Warning: User Cancelled Row Select");
else if (err == 3)
app.alert("Warning: User Cancelled File Select");
else if (err == 0) {
this.saveAs(outputDir + this.getField("Text1").value + "_" + this.getField("Text2").value + ".pdf"); // saves the file
idx++;
}
}
i tried the same solution (changing something) to my problem.
the path to my files was:
var file = "/c/Users/usuario/Desktop/folder/data.txt";
var fileOut = "/c/Users/usuario/Desktop/folder/"
the scripts fails with the error = -2 Cannot open File
whats wrong?
thank you all and sorry if i didn't explain well.

Copy link to clipboard
Copied
Anyone?
Copy link to clipboard
Copied
Did you get a popup window?
If so, what did it display?
Have you made sure the files and directory exist?
Have you verified that the tab delimited file has the exact internal format needed by Acrobat to import the data?
The first row needs the exact field names as in the PDF form with the proper syntax for string values.
Each row after the first row must have valid data with proper syntax for the type of data.
How did you create your data file?

Copy link to clipboard
Copied
Hello gkalseril,
My data file das created saving muy excel file as tan delimited txt. The fields has the dame name because i can import the data from adobe using the import option manually.
The pop up say: cannot open file.
This make me think about the posibility of an error in the path. But i checked lot of times and nothing.
The path goes to a folder in desktop. And im using "/" instead of "\".
Copy link to clipboard
Copied
Make sure the file you're trying to open is not open in Excel when you're
running the script.
On Mon, May 16, 2016 at 11:13 PM, Anselmoti <forums_noreply@adobe.com>
Copy link to clipboard
Copied
Also make sure no other application has the file open. That include Excel when exported, any file viewer, Windows Explorer with the "Preview" option enabled and the file highlighted, etc. Any of these can put the input file into the update mode, even if you are not changing the data, the system assumes it could be changed and locks the disk file for any changes by other programs. This warns Acrobat that the file is subject to change and Acrobat might not want to process the data because of the possibility of change.

Copy link to clipboard
Copied
Here is a link with a txt file which contains the data i want to import and the pdf form i want to fill,
the idea is to create a pdf for each row in the txt.
the code is the same i put on the OP, would anyone try to execute it?
the pdf form has more fields than the txt but it doesnt matter when i fill the form manually.

Copy link to clipboard
Copied
Can anyone send me an example which works?
Copy link to clipboard
Copied
It is properly working for me.
Ensure the proper field name in place of "Text1" - this.getField("Text1").

