Populate pdf forms with data from spreadsheets

Something like this?

// Import the first row of data from "myData.txt".

if (typeof cnt == "undefined") cnt = 0;

this.importTextData("/c/desktop/textdata.txt", cnt++ % 4)

// Specify the name of the template

var template_name = "Student Information Sheet";

// Get a reference to the template

var t = getTemplate(template_name);

// Add a new page based on the template

nPage: numPages, // Add the new page to end of document

bOverlay: false, // Create a new page, not an overlay

bRename: true // Rename the fields

app.alert("The template named \'" + template_name + " does not exist in this document.", 1);

But how would I tell the new page to take the second row from the chart, and will I have to click the button for each one of the fields in the data?

Community guidelines

Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Community Beginner ,

/t5/acrobat-discussions/import-data-populate-fields-in-pdf-from-excel/m-p/3231422#M7446 Jan 12, 2011 Jan 12, 2011

Copy link to clipboard

I have a tab-delimited txt file saved onto my desktop named 'DF Test.txt'. It has the following fields: Name, Q1, Q2, E1, S1. I created text fields and named them exactly like the fields in the .txt file and then created a button, selected Mouse up > java script and then added the following script.

// Import the first row of data from "DF Test.txt".

this.importTextData("/c/desktop/DF Test.txt", 0)

if (typeof cnt == "undefined") cnt = 0;

this.importTextData("/c/desktop/DF Test.txt", cnt++ % 4)

When I click on the button nothing happens. I don't get an error or anything. I know I've done something wrong, just not what.

Community guidelines

Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

/t5/acrobat-discussions/import-data-populate-fields-in-pdf-from-excel/m-p/3231423#M7447 Jan 12, 2011 Jan 12, 2011

Copy link to clipboard

There are a number of things wrong, but ignore that for the moment. What happens if you have just the following as the button's MouseUp script:

Community guidelines

Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Community Beginner ,

/t5/acrobat-discussions/import-data-populate-fields-in-pdf-from-excel/m-p/3231424#M7448 Jan 12, 2011 Jan 12, 2011

Copy link to clipboard

It opens up a menu to select a file. When I select the file I can pick a row in the document and then the information in the fields went into the proper text field.

Will the other script not work due to privileged vs non privileged events? I tried adding the trusted function in, and I even turned on the debugger and now have a console that pops up with errors. I tried

// Import the first row of data from "DF Test.txt".

this.importTextData("/c/desktop/DF Test.txt", 0)

if (typeof cnt == "undefined") cnt = 0;

this.importTextData("/c/desktop/DF Test.txt", cnt++ % 4)

but got a bunch of errors.

Community guidelines

Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

/t5/acrobat-discussions/import-data-populate-fields-in-pdf-from-excel/m-p/3231425#M7449 Jan 12, 2011 Jan 12, 2011

Copy link to clipboard

That's a big part of it. In order to open a particular file with JavaScript, the code that does that actual opening of the file needs to be executed in a trusted context. What that means for you is that the code needs to be placed in a folder-level JavaScript file. Code in a button can then call the code in the JavaScript file and the data in the text file can be read into the form.

The other problem is the path you're using is unlikely to be correct for a file on a Mac. Are you running this on Windows?

The other code (that uses the cnt variable) appears copied from the sample in the documentation and doesn't apply to your situation.

Community guidelines

Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Community Beginner ,

/t5/acrobat-discussions/import-data-populate-fields-in-pdf-from-excel/m-p/3231426#M7450 Jan 12, 2011 Jan 12, 2011

Copy link to clipboard

Yes, I'm on a mac. I have the file sitting on my desktop for easy access. Yes, the code was copied from the page. I changed what I thought I needed to change to make it apply to my situation. I've never done anything like this before, except for the other scripts you helped me with, so this is basically greek to me.

I need to cut out the

if (typeof cnt == "undefined") cnt = 0;

this.importTextData("/c/desktop/DF Test.txt", cnt++ % 4)

So I should have just

// Import the first row of data from "DF Test.txt".

this.importTextData("/c/desktop/DF Test.txt", 0)

"What that means for you is that the code needs to be placed in a folder-level JavaScript file"

Does this mean I would have to do something in a folder on each computer that uses this java script? Or is there a fix in the script itself that would allow anyone using the form on any computer to use it without problems?

Btw, does using '//' signify that I'm beginning a new set of instructions?

Community guidelines

Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

/t5/acrobat-discussions/import-data-populate-fields-in-pdf-from-excel/m-p/3231427#M7451 Jan 13, 2011 Jan 13, 2011

Copy link to clipboard

Reading a file is not allowed from a script in a document. This is a security restriction. The way around this restriction is to install a JavaScript file on each machine that would need to do so. So yes, you would need to install the file on any machine that would require this functionality. It's really not as bad as it may sound.

Note also that anything to do with templates will not work with Reader, so if you need this to work with Reader, you'll be limited to reading one record at a time from the data file. This might not be a big deal, as the user would simply click the button once for each record, view/print, and manually move on to the next.

What I've been trying to do here is introduce you to the concepts and point you to the documentation where you can learn more. But I also realize there's a lot to learn, especially when starting from scratch.

If installing a JavaScript file on each machine sounds viable, then I'll help you move forward.

1 1 Upvote Community guidelines

Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Community Beginner ,

/t5/acrobat-discussions/import-data-populate-fields-in-pdf-from-excel/m-p/3231428#M7452 Jan 13, 2011 Jan 13, 2011

Copy link to clipboard

I will have to ask around and see how many machines have acrobat on them.

1. In regards to installing a java script on each machine, (and this may be a stupid question), but would doing that cause any security risks on the machines, or when you mention security issue is it simply something to do with the program having the proper permission to execute the script. I'd hate to tick off the IT guys in my district.

2. "Note also that anything to do with templates will not work with Reader, so if you need this to work with Reader, you'll be limited to reading one record at a time from the data file." So, if I set up a button to generate a blank page from a template it won't work in Reader?y

3. I greatly appreciate all the help you've given me so far. I understand more than I did at the beginning, but, as you said, it is still a lot to learn.

If generating a template won't work in Reader there may not be any reason for me to do the code the other way, so how I proceed will probably depend on that.