Skip to main content
May 11, 2016
Question

Import data from Excel to PDF form using Javascript

  • May 11, 2016
  • 3 replies
  • 9733 views

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.

This topic has been closed for replies.

3 replies

May 25, 2016

Can anyone send me an example which works?

Known Participant
February 28, 2017

It is properly working for me.

Ensure the proper field name in place of "Text1" - this.getField("Text1").

May 16, 2016

Here is a link with a txt file which contains the data i want to import and the pdf form i want to fill,

https://mega.nz/#F!FRMUmaQ

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.

May 16, 2016

Anyone?

Inspiring
May 16, 2016

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?

May 16, 2016

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 "\".