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

Populate Fileds in second PDF

kellston
Registered: Jul 5 2010
Posts: 30

Hi All,
The forum is great and has been a great deal of help.
So I have created a form in Designer company name, address, etc.
I have a button with java in that form that opens a second form.
This second has much of the same data company name, address, etc.
Is there a way to have the company name, address, etc from the first form auto complete the company name, address, etc fields on the second form?

Thank for any help!

My Product Information:
LiveCycle Designer, Windows
NK-INC.COM
Registered: Apr 17 2010
Posts: 93
You can probably do this server-side.

Instructions for submitting to a script.
Name the fields the same on both forms.
Set the submit action to a url of a server-side script
Store the FDF/XDP/XFDF in a variable
Change the /F attribute value to the new form
Send the FDF/XDP/XFDF back to the buffer
Your second form should show up in the browser

You may also need to parse the XDP,FDF,XFDF data to set the /F value.
Visit: http://www.fdftoolkit.net for more information.
kellston
Registered: Jul 5 2010
Posts: 30
Hi All,
I am not advanced enough to understand server side etc.
Could someone help me with the code to have the second PDF auto populate the fields with the same name?
I have a button that open the second PDF.

if(this.bAlreadyOpened == null)
{
app.openDoc("/C/Test/Parts Form_with_comment.pdf");
this.bAlreadyOpened = true;

gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
With LiveCycle Designer you can use the exprotdData and importData methods to transfer data between forms in an XDP or XMl file.

George Kaiser

kellston
Registered: Jul 5 2010
Posts: 30
can I have one button click export the data, open the second pdf and import the data?
I am really struggling with the code to do that.
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
There are several different ways you could accomplish this task.

1. Exporting data to a local file is a privileged operation and cannot be done from a document script. This functionality is only available from a trusted function in an automation script.

2. A script in one document can only access data in another document if that document is disclosed. This works great if you have control over the documents, otherwise forget it. What you need to do is to add this script to a document script in each PDF that participates in the process

this.disclosed = true.

This line of code gives all scripts permission to access the PDF. Now a script can use the "app.openDoc" function to open the other document and extract data.

3. An automation script may be the way you want to go. It could give you the ability to prepopulate data in any form using any of several different methods.
a) from a set of hard coded values
b) data from the an external data file
c) data from the internet
d) open and extract the data from another PDF

See this article: http://acrobatusers.com/articles/getting-external-data-acrobat-x-javascript

You'll find much much more info on this topic at Automating Acrobat

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

kellston
Registered: Jul 5 2010
Posts: 30
document javascript is not available in my PDF?
It is there but greyed out.
I like your option number two.
Any idea how I could add the diclosure another way?

OK I got the disclosure working and the button open the second PDF.
The fields are name the same is there a specific java code need to populate the same named fields?

Thanks so much for any help.
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Opps, I forgot you were using a LiveCycle form. Add this code to the FormReady event for the top level node in the form hierarchy.

event.target.disclosed = true;



The app.openDoc function returns the document object.

var oOtherDoc = app.openDoc(...);

The LiveCycle scripting model is inside the document and it's accessed with the xfa property. So, a value in a field named "Address" might be accessed like this.

var cAddress = oOtherDoc.xfa.form.form1.MyClientSubform.Address.rawValue;

This code uses the "Full" path from the document object, into the XFA model, and all the way down to the field value property.


Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

kellston
Registered: Jul 5 2010
Posts: 30
Hi Thom,
In the first PDF I have added:
event.target.disclosed = true;
in the form:ready of the top Hierarchy.

In click function of the button to open the second PDF i have:
var oOtherDoc=app.openDoc("/C/Test/Parts_Form_with_comment1.pdf");

In the pdf which opens with the button click I have:
In the filed I am trying to populate under the initialize function
var cCompany = oOtherDoc.xfa.c.test.Service_Report_May_26_11.Subform1.Subform2.Company.rawValue;

I have tried all I can think of.
The field I am trying to populate is "Company" it is in form1 - SubForm1 - SubForm2 - Company.
The doucument to get the data from is located on the local PC in the c drive, test folder.

Thanks for any help I think I am close but just cant get the data over.

thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Shooting in the dark is a bad idea. The first thing you need to do is get a handle on what's going on. To do that, use the Acrobat JavaScript Console Window. Watch the video linked in my signature block.

I suspect that the first place things are going wrong is immediately after the Second PDF is opened. The "app.openDoc" function only returns the "doc" object when the second doc is "disclosed". The console window should report that "oOtherDoc.xfa" is undefined.

The next problem may be in the SOM paths. Make sure there are no "Unnamed" subforms. Give each subform a short name. Do not use spaces or punctuation.

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

kellston
Registered: Jul 5 2010
Posts: 30
Hi Thom,
I appreciate the time you have spent on this.
I am not a programmer just a guys who is trying to make this work.
I have watched your videos and read a lot about this and it is how I have gotten this far.
While I understand this is not a forum to do the work for me any and all help is greatly appreciated.
Do I have to use export/import or should the opendoc and xfa do this

Thanks.
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
No matter which method you use there are details that have to be worked out. Opening the a form with a known structure is one of the simplest ways to copy form data. But for it to work the form has to be disclosed, which is a very simple change. It is also very limiting because the code is written to work on a single specific form. If you are not an experienced programmer then I think this is the most straight forward method to implement and maintain for right now.

Importing and exporting only works if the forms have compatible data structures. For example if all the forms had a data connection to the same XML file. In one sense this is simpler because data can be moved around using simple import/export operations. But the forms have to be setup correctly to do it. This is something that is most efficient to do up front when the form is designed. Going back and modifying all the forms to conform to a common data structure can be problematic and very time consuming. In this case it's not the parts that are common between the forms that's an issue. It's the parts that are not common that can cause a problems. When data is imported into an LC form Acrobat wants to put a value in all the fields. You have to make sure this isn't a problem for those other fields that are not covered by the imported data (more details).

There are much more sophisticated methods that can be used to do what you want. For example you could have an external database for saving customer data into from a form, and then retrieved when necessary for any form. In fact, LC forms are built for this kind of activity. I've created a whole set of videos and samples that show exactly how to do it on my website, www.pdfscripting.com. But, this is advanced PDF scripting material. I think the direct copy form to form is much more straight forward and easier to implement for a non-programmer.




Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

kellston
Registered: Jul 5 2010
Posts: 30
Is there sample code on your site to make this work if I subscribe?
I just can't get any data to copy.
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Yes, there are sample documents with code. And I think you could benefit greatly from the material at the site which covers all the basics for LiveCycle scripting and includes lots of sample PDFs (the ones used in the videos). This material would be a direct help in implementing your current methodology, i.e., copying data from another form.

The videos and samples that cover connecting a form to a database are a bit on the advanced side and will require a major rewrite of your current forms. Implementing your current methodology will bring you up to speed on the whole LC scripting model so you can implement a more advanced solution if needed.


Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

kellston
Registered: Jul 5 2010
Posts: 30
Hi Thomp,
So I have watched several of your videos and now know how to use the console and taking baby steps.
I now understand what you mean about The console window should report that "oOtherDoc.xfa" is undefined"
It is.
The other doc opens and retuns the error
"oOtherDoc is not defined
1:XFA:form1[0]:#subform[0]:#subform[1]:Address[0]:ready
undefined"

How do I define the oOtherDoc?

Thanks
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Excellent!! You are on your way.

The code that opens "oOtherDoc" is fine. The issue is with the document being opened (Parts_Form_with_comment1.pdf). In order to be accessible to the script, it must "disclose" itself to the script, i.e., the PDF being opened must contain the disclosure code in a top level script. Traditionally, in an AcroForm PDF, this would be a document level script. But in a LiveCycle (xfa) form you put it in the FormReady script for the top level form node.


Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

kellston
Registered: Jul 5 2010
Posts: 30
In the document being opened "parts_form_with_comment1"
in the form ready event of the form1 node I have event.target.disclosed = true;
Is that not what discloses the document?

I have the same statment in the same place in pdf from which the data is to come from. "service_report_May_26_11"
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Now it's time to do a little debug to make sure all the parameters are correct.

1. Open Acrobat and make sure all documents are closed.

2. Copy and paste this code into the JavaScript console window and then run it one line at a time to maker sure each piece works

oOtherDoc=app.openDoc("/C/Test/Parts_Form_with_comment1.pdf");
cCompany = oOtherDoc.xfa.c.test.Service_Report_May_26_11.Subform1.Subform2.Company.rawValue;

Note that this is exactly the same code in your button. If for example the path to the PDF file is incorrect, then Acrobat will report an error. Or it the Path to the form field is incorrect it will report an error.

In fact, the SOM path to the form field is incorrect. The first node after "xfa" is the DOM used to access the target node, which in this case (and most cases) is the form model. This is followed by the top level form node, which is usually "form1". So the SOM path should probably look like this.

oOtherDoc.xfa.form.form1.c.test.Service_Report_May_26_11.Subform1.Subform2.Company.rawValue

Use the console window to figure these details out before testing the code in the form button. You need to have a good understanding of this stuff before you can begin to start looking hooking the form into a DB.

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

kellston
Registered: Jul 5 2010
Posts: 30
I dont know if this progress but.
When I run the script
cCompany=oOtherDoc.xfa.form.form1.c.test.Service_Report_May_26_11.ken1.ken2.Company.rawValue;
Somtimes Acrobat crashes. asks to send error report
Sometimes I get oOtherDoc is not defined
1:Console:Exec
ReferenceError: oOtherDoc is not defined
1:Console:Exec
undefined
I did change the subform names.
radzmar
Expert
Registered: Nov 3 2008
Posts: 1202
Hi,

even if Thom already put a lot of blood and sweat in this thread I would suggest that you have a look at another solution too.

Shared Date in Packages - Part 1
Shared Date in Packages - Part 2

radzmar
LoveCycle Blog
Documents you need:
LiveCycle Designer ES2 Docs

kellston
Registered: Jul 5 2010
Posts: 30
Half as much blood and sweat as I have.
I looked at those links before and was unable to get it working that way either.
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Thats a good one, thanks Radzmar.

Now, the problem is oOtherDoc is not getting defined.

What is the value returned when the you run the "app.openDoc" code? That code has to run successfully first, before the next line can be run.

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

kellston
Registered: Jul 5 2010
Posts: 30
The document opens and the value returned is:
[object Doc]
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Ok, I messed up. I should have told you to open a PDF first, then run the code. It doesn't matter what PDF you open. It's only there to create a context in which the scripting variables can exist. You see, when a document is opened it creates is own scripting context. Anything you run in the console window is attached to that scripting context. When the "openDoc" code is run and the opened PDF comes to the foreground, the scripting context in the console window is now for the newly opened doc. The "oOtherDoc" variable doesn't exist in this context.

So all you have to do to correct the situation is to switch the Acrobat view back to the first doc. Which is why I should have told you to open a PDF first, before running the code.

So give it another try.

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

kellston
Registered: Jul 5 2010
Posts: 30
Not sure I completely understand what you are saying but that is not new.

I opened a completely different PDF. With that PDF open I opened the Javascript debugger.
I ran the code
oOtherDoc=app.openDoc("/C/Test/Parts_Form_with_comment1.pdf");

The pdf parts_form_with_comment1 opens in the forground and the value returned is:
[object Doc]
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Now switch back to the first document (so that its in the foreground) and then run the second line of code.

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

kellston
Registered: Jul 5 2010
Posts: 30
Error returned is:
oOtherDoc.xfa.form.form1.c is undefined
1:Console:Exec
TypeError: oOtherDoc.xfa.form.form1.c is undefined
1:Console:Exec
undefined
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
That is excellent progress. According to the error message the document object is valid and the code navigated into the XFA structure as far as the top level node, "form1". The problem is that "c" is not a node in this path. You need to construct the SOM path as it appears in the Hierarchy window in LiveCycle Designer.

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

kellston
Registered: Jul 5 2010
Posts: 30
Thomp
I can now get a return value of what is in the filed the the document.
THANK YOU SO MUCH!
It is not doing exactly what I want but light years ahead of where it was.
I will work on it a bit more to see if I can get the button working and refine it.
Maybe I'll have a couple more questions then but for now I have taken way to much of your time.
Thank you again!
kellston
Registered: Jul 5 2010
Posts: 30
Hi Thomp,
Thanks again everything is working just like i want the second PDF opens all the fields fill from the information the first pdf. Perfectly.
I even have it working in reader.

Hopefully my last problem is:
I want to be able to save the second form with all that data it got from the first PDF.

Right now I can save it no problem but when I open it it tries to get all that info again so it is blank.

IS there a way to when saved with a different file name it will not try to open the first pdf and pull the info again?
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
I thought your script was on a button?

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

kellston
Registered: Jul 5 2010
Posts: 30
The button that is a script opens the second document "parts form".

In that doc "parts form" there is a script under initialize to define the otherdoc "oOtherDoc=app.openDoc("/C/Forms/Service_Report.pdf");"

Then in the calculate function of each field in the "parts form" is the script to get the data from the otherdoc "cTechnician=oOtherDoc.xfa.form.form1.ken1.ken2.Technician.rawValue;"

Perhaps there is a different way or better way but right now the user open doc 1 "service report" completes the fields. The on that doc he clicks a button that button opens "parts form" automatically defines otherdoc and gets the data.
That is exactly what i want but when the "parts form is saved and then re-opened it looks for the data from "service report which is blank because it was saved with a different name.

I thought about putting a button on the "parts form" to do this but was try to achieve it with a one button click.
Is a button in the "parts form" to run the script the only way?
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
You've tied too many different parts of the forms into one another. Everything should be handled from a single button script. There should not be code for this process anywhere else. This way the data transfer only happens when the button is pressed, and if any changes need to be made, they are made in one place. By distributing the code you've created a complex situation where you have to either turn off calculations completely or enter code into each calculation to selectively block it depending on conditions which are poorly defined, i.e., how do you distinguish between loading data from the other form and not loading data?

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

NextWave
Registered: Jul 4 2011
Posts: 1
Hi Thom-
My question is a bit simpler than Kellston-
I have a registration form for a school- with student info- and parental info-
it's a 2 page printable form- that I need to deliver via PDF- instead of having them fill out a simple web form (don't ask).
Of course, a parent with multiple kids- would normally just add additional fields to the form and extend it- since the contact info etc is all the same- but, the school wants the same exact form- for every student.
So- at the end of the form- I want to have two buttons- additional student or submit/print.
For the additional student- I'd want to generate the same form- with all the parental, guardian, contact info filled out- but the student specific parts- blank.
at the end of the form would be again- additional student submit/print
I am on a Mac- with Acrobat X professional. No Life Cycle designer for me. The document was designed in InDesign.
Suggestions on how to proceed most efficiently?
Thanks

David Esrati
The Next Wave
/ 937·228·4433   
Marketing • Innovation

thomp
Expert
Registered: Feb 15 2006
Posts: 4411
It sounds like you are in a bit of a pickle:( If the forms are filled out in Reader then there is no way to create an AcroForm that Adds additional form pages/fields. AcroForms are for the most part, static. You could create a form that has 5 pages, each one a duplicate so the user would fill out as many pages as they have children. Name all the common fields with the same field names, and all the child specific fields with a unique ID, such as "StudentName_1", "StudentName_2". After all, how many kids can one couple have. If they've got more than 5 then they are used to doing extra work, and will just have to fill out a new form.

There are lots of other ways to approach a solution. For example, the form could have a button that saves the form to a unique name (using the student name) and then clears all the student fields, leaving the common fields unchanged. The user fills and saves/submits for each child. Or it could be more internet connected, where data is loaded dynamically from a server script. There's all kinds of things you can do, but the multipage form is the simplest all around.

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

ppmax
Registered: Jan 21 2009
Posts: 8
Im using app.openDoc() in a 3D PDF using a template created in LiveCycle Designer. This 3D PDF opens other 3D PDF docs based upon which part is selected.

Interestingly, calling openDoc from within an event handler doesnt seem to work--I receive the dreaded "NotAllowedError: Security settings prevent access to this property or method" message. However, calling the exact same PDF from the filesystem from a button click event opens the external PDF without any complaints.

Thom: I've read lots of your posts here and elsewhere and you are obviously an authority on the Acrobat JS API ;) Do you have any ideas why this code generates a "NotAllowedError?"

// create a new handler
var currentSelectedNode;
var seh = new c3d.SelectionEventHandler();
seh.onEvent = function( event )
{
currentSelectedNode = event.selected ? event.node : undefined;

if (currentSelectedNode)
{
currentSelectedNode = nodeName(currentSelectedNode.name); //given "bolt.9013e84' returns "bolt"
var otherDocName = encodeURI("/D/link2.pdf");
if (otherDocName)
{
console.println(otherDocName);
var newDoc = app.openDoc("otherDocName");
}
}
}
runTime.addEventHandler( seh );



thomp
Expert
Registered: Feb 15 2006
Posts: 4411
In JavaScript execution context is everything. Your code is operating within the 3D DOM and the app object is not part of the 3D DOM, it's part of the AcroForm DOM. However, the developers at Adobe were kind enough to provide a link from the 3D world into the AcroForm world. It's called the host object.

So try this:

host.app.openDoc(...);


Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

ppmax
Registered: Jan 21 2009
Posts: 8
Thanks for the reply Thom.

The doc is actually an XFA form, not an AcroForm, and that script is actually executing in the doc DOM. I should have supplied more context (yuk yuk).

When the XFA form event docReady fires, I initialize some vars (globals, actually):
doc = event.target;
a3d = doc.getAnnots3D(0)[0];
c3d = a3d.context3D;
...
I also call an "initialize" function that creates the event handler:
var seh = new c3d.SelectionEventHandler();

The event handler object actually registers any node clicked in the doc--a 3D node in the scene or in the Model Tree, a list item in a listbox, etc--so it's not specific to the 3D context in any way:
currentSelectedNode = event.selected ? event.node : undefined;

"event.node" is any node within the XFA document.

For further clarification, all this code, and lots more, resides in a script object that sits here:
xfa.form.data.Page1.Script1

The curious thing is that this event handler works perfectly when called via the click event from a button that sits here:
xfa.form.data.Page1.openButton

...so this is clearly "working" except for the case where the event.node trigger itself is fired from within the 3D context. I'll definitely try prefixing the openDoc call with the host object...but since the selection handler is an object of the doc object I dont see how this will make a difference.

PS: If you're interested I'll send you an example of one of my latest 3D PDFs.

thx,
PP
try67
Expert
Registered: Oct 30 2008
Posts: 2398
Also, you're using otherDocName as a plain string, instead of a variable name. So this line:
app.openDoc("otherDocName");
Should actually be:
app.openDoc(otherDocName);

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

ppmax
Registered: Jan 21 2009
Posts: 8
try67 wrote:
Also, you're using otherDocName as a plain string, instead of a variable name. So this line:
app.openDoc("otherDocName");
Should actually be:
app.openDoc(otherDocName);
I must have put otherDocName in quotes when typing the code in (I didnt copy/paste from LCD). It's not that way in my code... ;)

thx
PP

thomp
Expert
Registered: Feb 15 2006
Posts: 4411
try67, Good catch!! That would definitely cause Acrobat to throw a security exception. Around Acrobat 8, the JS model started throwing security errors for all kinds of invalid operations, that used to throw more sensible exceptions.

So, even if the code in the PDF is correct, Acrobat will still throw this error if otherDocName contains a bad path.

Also, this code does not execute in the docReady event. It's setup in the docReady event, but the app.openDoc function is called from an event handler in the 3D annot context. Hence the need for the "host" object.

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

ppmax
Registered: Jan 21 2009
Posts: 8
thomp wrote:
Also, this code does not execute in the docReady event. It's setup in the docReady event, but the app.openDoc function is called from an event handler in the 3D annot context. Hence the need for the "host" object.
Good point. openDoc is an app method, not a doc method.

Does the forum support posting files? This is a Drupal site afterall ;)
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Use Acrobat.com to post your file.

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

ppmax
Registered: Jan 21 2009
Posts: 8
Hi Thom--

Sorry for the late reply--I've been meaning to post this for a while. Here is an example of one of the 3D PDF's I've created (with the software my company creates). We have had lots of success recently with large manufacturing companies and this document is actually a generic version of something we built for a US Army Depot.
https://acrobat.com/#d=6WE0GjusGKxTFvAicqGAhg

What do you think?
PP