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

bc. MyVar = “test”

to assign the value “test” to it and you can write

bc. 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:

bc. 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:

bc.
test

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:

bc. 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

bc. MyField

as opposed to having to write

bc. 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).

23 Responses to Form Variable Tip

  1. Zack says:

    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

  2. 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.

  3. Zack says:

    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 functionxfa.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,-ZackP.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.

  4. 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.

  5. Zack says:

    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.htmlTheir 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

  6. smvo says:

    Hello,Why not open the pdf form using a xdp file?

  7. 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.

  8. Zack says:

    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

  9. smvo,I’m not quite sure I understand what you’re asking. Could you elaborate a little more?

  10. Alan Garcia says:

    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!

  11. 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:xfa.host.importData();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.

  12. JK says:

    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!

  13. 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).

  14. Virginia says:

    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

  15. 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.

  16. Bruce says:

    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”elseifdrop-down1 == 2 thendrop-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)”) thenVialSize1.addItem(“20”)VialSize1.addItem(“30”)VialSize1.addItem(“40”)endifThanks for any help you can provide… oh ya, my code is FormCalc, but it doesn’t have to be.

  17. 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).

  18. Sidney says:

    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.

  19. Sidney,There are two ways you can go about it:URL in XML Data: You can simply store the URL to the image in your XML Data. In order to get the image to show-up when the form is loaded, you just need to assign the value of a URL to the HREF attribute of an image field object. You can do this by using the following JavaScript script:this.value.resolveNode(“#image”).href = {insert data node reference here}Image Encoded in XML Data: You can encode the actual image data right into the XML Data file. UTF-8-encoded image data can be loaded directly from an XML Data file and assigned to an image field’s value (that’s “rawValue” in script).

  20. Jan says:

    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.

  21. 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…

  22. karl winchester says:

    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

  23. 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.