Form Variable Tip
Form Variables can be useful because you can store string values into them that are persisted across object events as opposed to variables created locally (within scripted events) which are destroyed at the end of every event.
You can create Form Variables by going to the Variables tab within the Form Properties dialog, accessed via the main File menu.
The catch is in the way you get and set their values: If you’re writing a FormCalc script, then you can manipulate them just like any other variable. For example, let’s say you define one named “MyVar”. In your script, you can then write
MyVar = "test"
to assign the value “test” to it and you can write
xfa.host.messageBox(MyVar)
to display its value in a message box.
If you were then to change the script’s language to JavaScript and attempted to display the variable’s value in a message box as above, you would get the following error:
GeneralError: Operation failed.
XFAObject.messageBox:1:XFA:form1[0].#subform[0].Button1[0]:click
Argument mismatch in property or function argument
This is because FormCalc helps you by doing things “under the hood” based on the context in order to get and set the value of a Form Variable whereas JavaScript doesn’t.
If you look at the XFA definition of the variable, it looks like this:
<variables>
<text name="MyVar">test</text>
</variables>
This means that the value of the “MyVar” Form Variable is actually contained within the MyVar element’s content which must be accessed via the value property. Therefore, in order to access a Form Variable’s value in JavaScript, you need to do this:
MyVar.value = "test"; // set the value
xfa.host.messageBox(MyVar.value); // get the value
Just like being able to simply write a field’s name like this in a FormCalc script in order to access its value
MyField
as opposed to having to write
MyField.rawValue
in JavaScript, you need to specify the property you’re accessing on the object in JavaScript when accessing the value of a Form Variable (which is always the value property).
Posted by Stefan Cameron on July 16th, 2006
Filed under Scripting
You can skip to the end and leave a response. Pinging is currently not allowed.


Hi Stefan. Your blog seems to be by far the most current and up to date page I’ve found providing help with Designer. I was hoping you might be able to help me out with a problem involving prepopulating a form from an XML file. The problem is fairly specific, but I suspect it may have a simple solution. Could I potentially send you an email with a longer problem description? Thanks,
-Zack
Zack,
I’d be delighted to help! Is the description very long? It might help others if you posted it on my blog as a comment.
Let me know. If you’d still rather do email, we can do that too.
Hello again Stefan,
I’m working on a project that requires the use of prepopulated forms to be displayed for user interaction.
First of all, we’re working with Acrobat version 7.0.8 and a 60 day trial version of LiveCycle Designer 7.1.
What I’ve done is created a form in Designer, made a new data connection to an XML schema file and created text fields on it via the dragging and dropping from the data tab. Thus all the text fields on the form are bound to the schema. I then tried specifying a sample XML file to prepopulate by going to File –> Form Properties –> Defaults and specifying the sample file path. This seems to only work in the actual LiveCycle Designer PDF preview tab, and not when opening the form in Acrobat.
We then saw that through Acrobat we could go to File –> Form Data –> Import Data to a form, and that this did exactly what we wanted. However, we needed the form to be automatically prepopulated; user interaction for this was unacceptable. By searching on the Designer forums on the Adobe site we found a workaround. What we’re doing is actually calling the AFExecuteThisScript function from the Acrobat SDK in our program, and passing it the javascript function
xfa.host.importData (”C:\\sampleData.xml”)
This worked for us, except that when opening the form the user is asked to verify the signer via a dialog. From what I’ve read, the only way to make this automated (and thus removing the dialog that pops up) is to certify the form with a valid Adobe Partner signature. Do you know if that’s right?
Regardless, I feel that we’re beating our heads against the wall and having to do much more work than need be done for prepopulation. We haven’t found any examples of simple prepopulation after extensive searching. However, all documentation regarding it brushes it off as something that’s very simple to do, often simply saying “link the form to a sample data file.”
If you could give us some help or point us in the right direction I’d be greatly appreciative. Thanks,
-Zack
P.S. Even when going through the File –> Form Data –> Import Data method in Acrobat, we’ve never got images to load in an image field. I read something about Acrobat 7.0.5 breaking this, do you know if that’s true? We absolutely need to prepopulate with images as well as text, so hopefully this isn’t a problem.
Zack,
Thanks for describing your problem in more detail.
First of all, “specifying a sample XML file to pre-populate by going to File –> Form Properties –> Defaults and specifying the sample file path”, will, as you’ve discovered, only work when previewing the form in Designer. That’s because this is a special feature which performs a merge of your form with that sample data when the temporary copy of your form is created for previewing. It’s purely designed to save you the extra step of specifying a data file in Acrobat once your form is loaded.
Unfortunately, I don’t think you’re going to get very far with automatically loading data from an XML Data file on form load. This is because you’re attempting to retrieve the data from a file and doing so automatically would mean that Acrobat would have to grant your form special security rights. You’ve also discovered this, however, because, as you’ve mentioned, the user is then forced to certify the form (which doesn’t compliment your workflow very nicely).
The only way I know to get around these issues is to use a Data Connection to something other than a file on a disk. That means using a Data Connection to an ODBC data source or a Web Service (WSDL). Using those types of connections, you should be able to get your data to load automatically on form load simply because the connection is considered “safe” by Acrobat. (Note that if you’re using an ODBC data source, the user will have to setup it up on their machine first so depending on your requirements, WSDL may be the — only? — way to go.)
Now about problems with pre-populating a form with image data: That’s a good question. I’m going to have to consult some of my contacts here at Adobe about that one. I’ll get back to you ASAP.
Stefan,
Thanks for the help. Hopefully we don’t have to resort to using an ODBC or WSDL data source, but we’ll see. Currently we’re investigating something called GeoTrust.
http://www.adobe.com/security/partners_cds.html
Their product “allows authors to create Adobe® Portable Document Format (PDF) files that automatically certify* to the recipient that the author’s identity has been verified by a trusted organization, and that the document has not been altered in any way.” Hopefully that might work.
As for images, we’ve found a solution. We simply had to include the raw image data in the sample file that we imported rather than a reference to a file on the hard drive. I think there’s supposed to be a way to get it to work with a reference, but this solution works fine.
Once again, thanks a ton, and I’ll get back to you if the GeoTrust certificate works for our automatic certification.
-Zack
Hello,
Why not open the pdf form using a xdp file?
Zack,
You’re very welcome! I’m glad you found a solution to your problems with loading images from XML Data files.
Please do let me know how you make-out with the GeoTrust certification.
Stefan,
So after talking with someone from GeoTrust it sounds like you were right. I think we’re either going to have to allow the validation dialog to pop up or try and populate from a database or web service. Oh well, I’m sure we’ll work something out. Thanks again, and I’ll be sure to check back if we come across anything else we totally can’t figure out.
-Zack
smvo,
I’m not quite sure I understand what you’re asking. Could you elaborate a little more?
The problem mentioned in the comments is the exact same problem I’ve been trying to solve for a long time. I kinda gave up. I’ve gotten really frustrated that this product can’t automatically load a file. Even more frustrating is the documentation which as Zack noted brushes it off as something that’s very simple to do, often simply saying “link the form to a sample data file.” AHHH!
Alan,
Thanks for your input. I’ll mention this problem to our documentation team.
Note, however, that the reason why you can’t automatically load an external file when opening a form in Acrobat is for security reasons. If Acrobat let you do that, it would open-up a huge security whole that would undoubtedly be exploited by viruses.
That said, it is possible, but only by certifying your form and having the user accept to grant it special security rights that would allow it to automatically load a file on open.
Note, however, that it might be useful to know that you can create your own data import/load button by using a regular button and specifying this script (FormCalc or JavaScript) on its Click event:
This will cause the “Import Data” dialog to open (same as having the user go to “File | Form Data | Import” in Acrobat). I know this is still a manual process but at least it’s a little more convinient for the user who doesn’t know how to import data into Acrobat using the menus.
Does SDK provide any method to automatically fill a PDF form with the XML data? We want to automate the form filling process like a batch job and I could not find any easier method but to iterate through each field and fill in.
Thanks!
JK,
I believe what you’re wanting to do is addressed with data connections and data binding. In Designer, you can use the Data View palette to create a data connection based on a schema which describes an XML Data file (or you can use an existing XML Data file to infer a schema). Then you can drag the objects in the resulting data connection tree into your form (which automatically creates fields bound to their respective data nodes). When data is imported into the form at run time (using the XML Data file), the fields that are bound to the data nodes in the data connection are automatically populated with the values from the XML Data file. See my article on Connecting a Form to a Database. The procedure for creating and using a data connection based on a schema is similar (except for the Data Connection Controls that are used to iterate through records).
I have some similar issues from this string. I am a novice with Designer et al but have a project, ergo must learn.
I want to know if I can specify the file name and forego the dialog box with my button for importing the data.
xfa.host.importData();
What is the proper syntax - my file is on the desktop currently.
thanks,
Virginia
Virginia,
Technically, the xfa.host.importData function does support an optional parameter which is the path to the XML Data file to import whereby specifying this parameter would by-pass the Import Data Dialog.
The problem with this is that it’s a security issue (where a PDF could be importing and then submitting sensitive data without the user knowing it). Therefore, in order to use the xfa.host.importData function with a path, your form must be certified (signed with a digital signature), trusted by the end-user who opened the form and the end-user must have sufficient permissions on their system to access the data at the specified path.
In short, it’s less painful to stick with the dialog unless you really, really don’t want it to show-up.
Interestingly enough, however, you can import data automatically via data connections to databases and web services without needing a user’s permission. I suppose that’s because web services are “public” and for database access, the user must have already created a System DSN which points to it.
Hey… I am trying to change the values of a drop-down list based on the results of a previous drop-down list. i.e. if drop-down1 == 1 then drop-downA.additem “A”
drop-downB.additem “B”
elseif
drop-down1 == 2 then
drop-downA.additem “AA”
drop-downA.additem “BB”
etc…
In doing so, I keep getting an error “Invalid property get operation; value doesn’t have a default property.”
The shortened code is this:
if (SiteInformation.grpContactInformation.VialManufactuer.value == “Kerr Friendly & Safe (Kerr/FS)”) then
VialSize1.addItem(”20″)
VialSize1.addItem(”30″)
VialSize1.addItem(”40″)
endif
Thanks for any help you can provide… oh ya, my code is FormCalc, but it doesn’t have to be.
Bruce,
I think this might be as simple as changing VialManufactuer.value for VialManufactuer.rawValue, assuming “VialManufactuer” is a field.
The error message is indicating that the “value” element doesn’t have a default property. This is because “value” is an element on the field object (”VialManufactuer” is a field, I’m assuming) instead of a property and when you don’t specify a property name, the script interpreter attempts to retrieve an element’s contents (value) using a default property however “value” doesn’t have one.
The proper way to access a field’s value is by using the “rawValue” property on the field object itself (”VialManufactuer” in this case).
I’m also wondering how to get an image field to load from XML data. I have tried importing raw data from .jpg and .bmp files with no success. Zach, I was wondering what format your “raw” data images was in when you do the import. Barring that, it would be much nicer to be able to import from url.
Sidney,
There are two ways you can go about it:
this.value.resolveNode(”#image”).href = {insert data node reference here}
Hi Stefan,
do you think it is possible to fill data from an odbc data connection inside a form variable? After writing the data in a form variable I`m independent from the datasource and can publish the PDF in a Reader environment.
Jan,
Unfortunately, Form Variables aren’t like fields whose values get saved when you save the PDF in Acrobat Pro (or Reader with a Reader-Enabled form). Essentially, they’re exactly like a JavaScript variable you would declare in a script object: It isn’t persisted on save.
What you would have to do is use a hidden field as the variable so that when the form is saved, its value is saved and reloaded as you would expect.
From there, if you could figure-out a way to serialize the data from the data connection into a string (as the value of a hidden text field, for example) in such a way that you could read it back into some organized structure that you could use within the form, you’re golden! The only thing is that you’ll have to be able to save the form in order to save the Form Variable’s value.
For example, I’m thinking that if you used script that iterates through all records in a database and you accumulated the various values into multi-dimensional JavaScript Array objects, you could then use Array.toString() to serialize the array into a string of comma-separated values and store that into a hidden text field.
When the form is reloaded, you could then check the hidden text field for a value. If it has one, then there’s no need to load the data connection since it contains the data. You could then use the JavaScript String.split method which returns an array of strings based on a separator (which would probably be a comma).
Just an idea…
i have created an interactive form using adobe designer 7.0.
in it is the data is stored ina field named “name”.
is it possible that the xml file created when submitted by email can be named with the variable name?
ex. John Doe.xml
Karl,
Unfortunately, it’s not possible to specify a name for the XML data file that gets submitted from a form.
The name of the file will always be the name of the form with a “.xml” extension.
Stefan:
This has been a very informative blog. I’ve been struggling with the same issues for a few days now. In my scenario, I have created an XML output from our SQL data and place it in a particular subdirectory that’s created on the fly. I also put a copy of the PDF form in the same directory. I have created a button on the form and am using the xfa.host.importData(”") function so the end-user doesn’t have to go through the command menus in Acrobat. The default path is “My Documents” You note the following:
“Technically, the xfa.host.importData function does support an optional parameter which is the path to the XML Data file to import whereby specifying this parameter would by-pass the Import Data Dialog. The problem with this is that it’s a security issue (where a PDF could be importing and then submitting sensitive data without the user knowing it). Therefore, in order to use the xfa.host.importData function with a path, your form must be certified (signed with a digital signature), trusted by the end-user who opened the form and the end-user must have sufficient permissions on their system to access the data at the specified path.”
I would like to experiment with this, since I am using this functionality within my company. Can you tell me how to structure the optional parameter if I want to have the dialog box open in the same directory where the PDF form is located? I think having the end-user (my employees) acknowledge that they trust the data source is less of a hassle than having to drill to the particular subdirectory where the XML data is stored. Alternatively, if it isn’t possible to default to the home directory of the PDF form, then to a particular subdirectory where the end-user would only have to drill one additional level down.
Thanks,
Bill
Bill,
Unfortunately, the xfa.host.importData method doesn’t have the means to open to a particular folder that you specify. It either opens to a folder determined by the host (probably the last place you looked in to open a PDF in Acrobat) or it doesn’t show the dialog at all because you specified a path to an XML data file.
Stefan:
Thanks for the response. I don’t see any pattern. It seems Acrobat 8 Professional wants to open to “My Documents” as a default. I can’t see any way of modifying it to default elsewhere. Do you know of a command-line option that forces Acrobat to default to a particular subdirectory upon launching? Then I when I run the xfa.host.importData command, it should open to the default directory that Acrobat is set to.
Bill Geary,
You’re welcome. Unfortunately, “xfa.host.importData” was designed to do quite what you’re looking for. It’s really just made for importing XML data files but doesn’t have any concept of preset folders for browsing. I don’t know of any command-line option to specify a default folder either.
I’m sorry I couldn’t be of more help for you on this topic.
Hi Stefan and Zack,
I’ve been trying to work on the imagefield object by automatically loading the image based on the XML file. I can do this in the preview pane of LC however when generating the PDF file with xdp in web and merging it with the XML data, it seems that I cant load the image. What do you meant when you said the I just need to load the raw image file as well? Also, if I use scripting to load the image, the script does not run on Dynamic forms. It only runs on static forms? Am I correct about this?
Thanks!
Danniel Agbada,
I’m not quite sure how you’ve setup your form to get the data when it’s displayed in a web browser however I can say that importing XML data isn’t something that you can do by default in Reader. It’s not the same when previewing the form in Designer using a “preview data file” set in the “File menu > Form Properties > Preview” tab. It could be this difference that’s preventing you from seeing your image data.
In the end, Zack determined that he had to embed the base64-encoded image bits (raw image data) into his data file rather than only include a reference (path) to the image. How are you including the image in your XML data file?
As for scripting, it should work in Dynamic PDF forms as well as in Static PDF forms but without knowing exactly what your script is, I can’t really tell. Acrobat might be generating an error which you might be able to see in the JavaScript Console.
Hello. I have added to the PDF document XFA form with ImageField. Image is loaded from some URL. When user opens PDF document with Acrobat it shows prompt (allow/block URL). May be is it some way to avoid that prompt so user haven’t to change his security settings? Is it possible to certify document so included URL will be signed as trusted content?
P.S. Sorry for my VERY BAD english.
Evgeny,
Unfortunately, I’m not sure if there’s way to suppress that security dialog. Furthermore, starting in Acrobat 8.1, referencing images from URLs is no longer allowed. You can still use a URL as the source for the image field when you’re designing your form but the image bytes will be automatically embedded into your form when it’s saved as a PDF (or rendered as a PDF on the server). So even if you could get around this dialog, it wouldn’t do you much good going forward.
Thanks a lot! I’ll try to load image through web service (import image data in xml format). May be it will solve my problem with prompt.
Evgeny,
Using a web service will get around the issue of not being able to reference images from URLs however Acrobat/Reader will still show a dialog asking the user to allow the connection to the web service. At least they’ll only see it once every time the form is opened unless they opt to “always allow” the connection.
I’ve asked same question at acrobatusers forum. Someone there had told me that in certified documents that prompt may be will not appear if I shall use web service to import image. But I don’t understand what kind of certification I need (adobe or it trusted partners certification or I can certify it myself).
Evgeny,
Certifying a PDF means signing it with a digital signature. Unfortunately, digital signatures are quire costly. An alternative is to “self-certify” your PDF using Acrobat 8 or later. This certifies the PDF but doesn’t use a signature created by a third-party service. That means the signature may not be trusted by everyone however it will let you certify the PDF nonetheless and doesn’t cost anything.
To create a self-certified PDF, open the PDF in Acrobat 8 or later and choose “Advanced > Sign & Certify > Certify with/without Visible Signature”. A dialog box suggesting that you get a digital ID from an Adobe Partner will appear. Click “OK”. You will then be instructed to draw a box where the signature should appear. After you’ve drawn the box, you’ll get another dialog box with 4 options, one of which is “Create a self-signed digital ID for use with Acrobat”. Choose that option and following the remaining steps.
I see!! Thanks for your help!!!
Stefan -
I am a total newbie to Acrobat and LiveCycle, but like previous posters, we have a need, ergo I must learn…
I have gone through the “Purchase Order” tutorial in LiveCycle, and that all makes sense and works fine (as long as I do my print preview from within LiveCycle!)
We are creating an extranet website using VB .NET (Visual Studio 2008). A partial workflow of this system is that after a customer logs in, he sees a page with links for various forms. When he clicks one of the links, I want our .NET app to pull up the PDF form, merge it with the appropriate data (from SQL Server) based on his login credentials, and then present the user with the pre-populated form. Note that the importing of the data will be on our end; by the time the customer sees the form, it should already be pre-populated — he should not have to press any button or menu item to popuate it. We must assume that the customer has Adobe Reader (not Acrobat). After the user gets the form, he will fill in additional fields, and send the form back to us.
Questions:
Is this possible?
What type of data connection (web service, XML, OLEDB) would be best suited for my purposes? Does security/certification play a role in making this decision? (We have not purchased any digital signatures)
I’m guessing I’ll need to send the “xfa.host.importData” command (or something similar) to the PDF file from within my .NET code. Am I on the right track? If so, how do I create a reference to the PDF file object from within my .NET code? Are there any References I would have to add to my VS project?
Will Acrobat and/or LiveCycle need to be installed on our server? (So far, it is only on my desktop).
Hope this all makes sense!
TIA
Dan
Dan Murphy,
What you’re wanting to do is what LiveCycle Forms is designed to do: serve-out XDP forms as PDFs with merged data (as well as a whole slew of other things like serve the form as an RIA Form Guide and handle server-side form scripts).
This is a little beyond my expertise but I believe you will need LiveCycle running on your server to achieve this unless you could somehow figure-out a way to automate Acrobat and use it to load the PDF, execute the import data command, save the PDF with the data and then serve it out to the client but if this is a commercial website, nothing will beat the performance of LiveCycle for doing this.
Say you did this with LiveCycle. If you’re using LiveCycle ES or ES Update 1, there are now remoting end-points exposed that let you initiate a process remotely: invoke() for short-lived processes and invoke_Async() for long-lived processes. To use these methods, you’ll need to pass credentials of a LiveCycle user which can always be the same user or it can be what the user logged into your site as, provided there’s a similar user configured in LiveCycle. Then you could invoke a process by passing it some data and the process could merge that data with a PDF and return the PDF to the client.
If you want to pursue this, I would recommend you check-out the Adobe LiveCycle Blog and/or post a question one of the Adobe LiveCycle ES Forums. Address your question to Lee Sutton at Adobe — he’s usually answering forum questions and posting on the LC blog and knows more about this stuff than I do.