These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Importing data from a .txt file and have Acrobat create new pages

gwh
Registered: May 19 2008
Posts: 47

Hi everyone,

I've created a form in Acrobat 9 to be used as an invoice. I want to be able to import data from an excel spreadsheet or .txt file into all the form fields. I know that I can go Forms > Manage form Data > Import Data to import 1 record at a time, but say I have about 50 invoices to populate, is there a way to import all the records at once and have acrobat create a new invoice for each of the customer's records?

If there is a way, will it be compatible for Acrobat Reader users since this form will be used by someone who only has the Reader?

Really appreciate any help.

My Product Information:
Acrobat Pro Extended 9.1, Macintosh
nixopax
Registered: Feb 6 2009
Posts: 105
Reader does not have the capability to append pages, or create new pages.

To create new pages this.newPage() would be used. If you read the API reference it has clear distinction of what is allowed in what versions and what types of program.

http://www.adobe.com/devnet/acrobat/pdfs/js_api_reference.pdf
gwh
Registered: May 19 2008
Posts: 47
Thanks for the reply,

The end user may end up getting the full version of acrobat so I guess I'm wondering if there's a way to do this using Acrobat Pro. I know that you can spawn new pages from a template but I don't know how I could get this to happen automatically when importing multiple records from a .txt file.

Would someone know if it's possible - perhaps using javascript?
nixopax
Registered: Feb 6 2009
Posts: 105
Well, there would have to be a distinction between each record within the .txt file. Take for example, a .csv file. That means comma separated value (csv). A table of values would have multiple columns separated by commas(,) and the end of the row would be signified by a carriage return/line feed (\n, 0x0A, \u000A, etc). Or a Tab Delimited File: Values separated by tabs, and the end of a record can be a carriage return/line feed or a semicolon (;)

These two are common formats that Databases use when exporting data. The issue one can run into however is if the separating character is used in the value itself. The machine isn't as smart as the human and just does what its told. This is where escape characters come in. With a csv, you need to escape your commas that are real data "John\, what are you doing?" Then in your input script set the escaped comma to be a part of real data.

Other ways to do it would be to use data that you would be pretty sure most people would never use to signify the start/end of a record. I've seen things like BOF & EOF (which means beginning of file & end of file). So how ever you set up your data records for import it's important to think about separation conventions to retain data integrity.Next comes the importing of the data. On Page 320 of the reference I linked to, it has a function meant specifically to import a tab delimited text file and will populate the fields that you set as the header of the text file. They have a text file that looks like this:

Quote:
First Middle Last
A. C. Robat
T. A. Croba
A. T. Acrob
B. A. Tacro
The function to import this is: this.importTextData();
It accepts cPath and nRow as arguments.
this.importTextData("/c/data/myData.txt", 0);
This would import the above text file, and import the first row. It's a zero based row index, and ignores the header when it comes to importing the values. The headers are the field names it would import into. It would import A. into the field named [b]First[/b], T. into the field named [b]Middle[/b], Robat into the field named [b]Last[/b].If you didn't set the cPath argument, it'd prompt you with the file to select(Note, if you DO set the cPath, it can only run during batch and console events). If you didn't set the nRow argument, it'd prompt you with the row you wanted to import.

Now, here's where the fun part starts. :)

If you run a regex search for the number of line feeds in the text file: [i]/\n\r/[/i]
Then do a for loop using the number as the end point. [i]for(n = 0; n <= endOfFile; n++)[/i]
Create a new page [i]this.newPage();[/i]
Create each field [i]var first = this.addField();[/i]
Import each row one at a time. [i]this.importTextData({nRow:n});[/i]
Populate that field from the parent field [i]first.value = this.getField("First").value;[/i]
Empty parent field [i]this.getField("First").value = "";[/i]
Start over again until the text file is fully imported.

The actual code itself would take some working to get done, but that would be the general idea. Reading the reference can give you a good insight about the functions you'll need to accomplish this.

If anyone else has some input on this, I'd love to hear how you guys would do it.
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4308
If you are pulling in detail line items, you could look at using the template object to add additional pages with form fields into which you could import the various rows from you excel or text file. But this will require a substantial programing effort as each row would need to be imported into some hidden fields and then moved into specific rows on a given page. You should be aware that each page will be prefixed to each field name on a spawned page from a template.

Because the use of templates is limited to full versions of Acrobat, the end user will require at least a standard version of Acrobat.

See [url=http://www.planetpdf.com/developer/article.asp?ContentID=a_lesson_in_templates_for_adob&gid=6466]A Lesson in Templates for Adobe Acrobat] by Dave Wraight for more information.

George Kaiser

gwh
Registered: May 19 2008
Posts: 47
Thanks guys, my javascript programming is really very limited so I may have to look into purchasing livecycle designer as apparently this automates much of what I'm looking to do.
acoginthewheel
Registered: Nov 9 2009
Posts: 11
I know i'm getting to this late, but...

I'm curious, Allan, where you say
"If you run a regex search for the number of line feeds in the text file: /\n\r/"

Are you doing that from a javascript within the PDF?