Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.

Import data from text file

Avatar

Former Community Member
We are trying to import data from a text file into fields in a form using the importTextData method for the Doc object. The script we are using looks like this:



var doc = event.target;

var returnCode = doc.importTextData("datafile.txt",0);



The text file "datafile.txt" contains tab separated values in a header row (which corresonds to field names in the form) followed by tab separated datarows - all according to instructions in the API.



The returnCode will result in an integer value, which in our case represent "Error: Invalid Row". It would help us a lot if anyone could give us a hint on what may be wrong or even better post a working solution.



We have also tried using the importTextData method without any parameters (the user is then prompted to select the text file and then the specific row) but the result remains the same. The fields in the form are not populated with data and the message "Error: Invalid Row" is returned.



Annika Lindqvist
7 Replies

Avatar

Former Community Member
Have you tryed it on a plain Acrobat form (not Designer) form?

The field hierarchy is different!



You can use "xfa.data.loadXML("/C/Data/importData v2.1.xml", false, true);" to import XML



Or I can build a VB import function for you

ulf.a@dtp-tjanst.se

Avatar

Former Community Member
Thanks for your answer but unfortunately it does not solve our problem. We only work with Adobe LiveCycle Designer so we have not tried the solution in an Acrobat form but the method is supposed to work in Designer when called upon as above.



We have also tried the loadXML method (as you wrote it) but can not get it to work in a Designer form designed for version 7.0.7 of Acrobat/Reader. Do you have an example of the loadXML function working you could enclose?

Avatar

Former Community Member
The problem you're experiencing is that importTextData is an Acrobat API call. As such, it executes on the AcroForm field equivalents to the XFA fields you've placed on the form. What's not indicated in the API for importTextData is that the names in the fields in the header of the text file must be the
full AcroForm SOM expression.



I've created a working sample to explain this.



First, I created a form with a table with one row in it containing 3 columns named FirstName, LastName and Country.



Then I created a data.txt file like this:



FirstName LastName Country

Fred Jones USA

Kyle Francis Canada

Sam Roberts UK


I then placed a button on the form which calls "event.target.importTextData();" and ran the form in Preview.



Of course, when I selected a row for import, nothing happened.



I then used the "Avanced | Forms | Export Data from Form..." menu option in Acrobat after filling-in the fields in the table with some text and looked at the generated text file (note that you have to specify text as the output format). In there, I was able to figure-out what the full AcroFrom SOM expression was for each field I wanted to import data into.



I changed my data file to look like this:



form1[0].#subform[0].Table1[0].Row1[0].FirstName[0] form1[0].#subform[0].Table1[0].Row1[0].LastName[0] form1[0].#subform[0].Table1[0].Row1[0].Country[0]

Fred Jones USA

Kyle Francis Canada

Sam Roberts UK


Afther that, importTextData worked as expected without any errors.



Stefan

Adobe Systems

Avatar

Level 2

Thanks that really helps. I was stumped on this for 3 days!

Now my next problem still with importTextData is that when I supply the path and row, it returns a resuls of -2 (User Cancelled Row Select).

Is there anything different I need to do when indicating the path?

My call looks like:

var cPath = "/C/temp/importfile.txt";

var row = 1;

var Result = oDoc.importTextData(cPath, row);

Any help anyone can offer is much appreciated!

Paul

Avatar

Former Community Member
Yes Stefan is right, The field hierarchy is different!

/ulf.a@dtp-tjanst.se

Avatar

Former Community Member
Thanks for your quick and informative reply Stefan. The solution works perfectly.



/Annika

Avatar

Level 1
Ok, the button works for me. But when I double click on the row nothing gets imported and when I single click the row and then select ok nothing gets imported. Am I doing something wrong? Is there a way I can import the data without selecting the file?