Stefan Cameron on Forms
Building intelligent forms using Adobe LiveCycle Designer

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
Both comments and pings are currently closed.

51 Responses to “Form Variable Tip”

  1. Zack on July 20th, 2006

    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. Stefan Cameron on July 20th, 2006

    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 on July 21st, 2006

    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.

  4. Stefan Cameron on July 23rd, 2006

    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 on July 24th, 2006

    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

  6. smvo on July 24th, 2006

    Hello,

    Why not open the pdf form using a xdp file?

  7. Stefan Cameron on July 24th, 2006

    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 on July 24th, 2006

    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. Stefan Cameron on July 29th, 2006

    smvo,

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

  10. Alan Garcia on August 15th, 2006

    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. Stefan Cameron on August 15th, 2006

    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 on September 18th, 2006

    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. Stefan Cameron on September 21st, 2006

    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 on September 28th, 2006

    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. Stefan Cameron on October 3rd, 2006

    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 on October 16th, 2006

    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.

  17. Stefan Cameron on October 17th, 2006

    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 on October 27th, 2006

    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. Stefan Cameron on October 31st, 2006

    Sidney,

    There are two ways you can go about it:

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

    2. 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 on December 8th, 2006

    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. Stefan Cameron on December 11th, 2006

    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 on January 6th, 2007

    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. Stefan Cameron on January 11th, 2007

    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.

  24. Bill Geary on May 12th, 2007

    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

  25. Stefan Cameron on May 14th, 2007

    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.

  26. Bill Geary on May 16th, 2007

    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.

  27. Stefan Cameron on May 17th, 2007

    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.

  28. Danniel Agbada on April 10th, 2008

    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!

  29. Stefan Cameron on April 19th, 2008

    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.

  30. Evgeny on April 23rd, 2008

    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.

  31. Stefan Cameron on May 1st, 2008

    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.

  32. Evgeny on May 5th, 2008

    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.

  33. Stefan Cameron on May 5th, 2008

    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.

  34. Evgeny on May 8th, 2008

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

  35. Stefan Cameron on May 14th, 2008

    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.

  36. Evgeny on May 19th, 2008

    I see!! Thanks for your help!!!

  37. Dan Murphy on October 23rd, 2008

    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

  38. Stefan Cameron on October 30th, 2008

    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.

  39. Chris on December 30th, 2008

    Stefan,

    Great Blog. I used this thread to answer most of my questions. However I still have one remaining.

    I am in a similar boat, as most of the people in this thread. I want a user to open a PDF document, and have the fields pre-populated. I think my difference is that I want one form, that will pull in

    I have decided to go with the database connection approach, since I do not want users to have to search for a .xml file using the dialog box (although that approach did work well).

    My question is when I setup my OLEDB connection, and type my SQL Query, I want to also pass in a parameter into the Query through VBA code. Is this possible?

    For example: I would want my SQL Query to be:
    Select MovieTitle from Movies
    Where MovieID = %ID%;

    I want to use Microsoft Excel to pass in the ID value (%ID%), into the SQL Query. This way one user could enter 7 in their Excel Macro, and get one result, and another user could enter 3 in their Excel Macro, and get another result. Is this possible?

    Thanks.

  40. Stefan Cameron on January 5th, 2009

    Chris,

    I’m glad you’ve found my blog to be useful!

    It sounds like your problem is that you want some kind of a dynamic query to be executed and you want some external application to affect that the query executed by the data connection in the PDF will be.

    The easiest way to get the ID into the PDF, assuming you’ll always be using Acrobat to show the form and that you’ll always be showing the form in a browser, is by passing the ID in the URL Request when your macro calls the PDF form on the web server. This is not a secure way, by any means, of passing data into the PDF, but if that’s OK for you, it might be just what you need.

    Otherwise, I would say you would need to do this either via a web service somehow or by using LiveCycle Forms to which you would give a dynamically-generated XML file (by your macro) to merge into the PDF prior to it being served to the browser.

  41. Tim Jeske on January 6th, 2009

    I’m a novice at scripting in PDF … and I’m tearing my hairs out one by one as I’ve spent a couple of hours today reading stuff way over my head in search of an answer. Perhaps you can/will help.

    I’m working on developing a PDF form in which I have several places where I need to calculate registration fees on the basis of the number of people being registered for an event. Thus, I have a field for the user to fill in the number of registrants at $75.00 each; a field for indicating the number of registrants at $90.00 each; and the number of registrants at the rate of $120.00 each. I want to calculate the total amount for each of those lines individually and get a grand total at the bottom. Simple calcs won’t work … I’ve figured that out … I think I need a “script” to do this.

    How do I calculate 7 (entered in field by user) x 75.00 and come up with a total?
    How do I calculate 3 (entered in field by user) x 90.00 and come up with a total etc.

    Thanks!

    T

  42. Stefan Cameron on January 6th, 2009

    Tim Jeske,

    I’ll try to explain this as simply as I can. Typically, in a form like yours, there’s a quantity field, a cost field and a total field. Let’s say there are 3 rows and also a grand total field that sums all of the total fields (note that all fields here are numeric fields):

    quantity1   cost1   total1
    quantity2   cost2   total2
    quantity3   cost3   total3
                        ------
                        grandTotal

    Usually, the cost fields will be read-only (“Object palette > Value tab > Type property” set to “Read-Only”) since the price is fixed and the total and grandTotal fields will be read-only since they are calculated fields (“Object palette > Value tab > Type property” set to “Calculated – Read-Only”). You then need some simple FormCalc scripts to make this all come together.

    For the total1 field, you would set its Calculate script (using the Script Editor palette) to the following FormCalc expression (make sure the script language is set to “FormCalc”):

    quantity1 * cost1

    and similarly for total2 and total3.

    Finally, for the grantTotal field, you would set its Calculate script to the following FormCalc expression:

    SUM(total1, total2, total3)

    With these simple scripts set, whenever the value of a dependent field changes (i.e. total1 depends on quantity1 and grantTotal depends on total1-3), the values of the calculated fields will change accordingly.

  43. Tim Jeske on January 7th, 2009

    Thank you Stefan, I am beginning to see some light but am still unclear on a couple things.

    My form currently contains this line (for example)

    ____ adults at $75 = __________

    What I want to happen is for the user to enter a number (n) in the first blank (field name “reg1”) and have the second blank (field name “reg1total”) display the product of n*75.

    A calculation expression of: reg1 * 75 in field “reg1total” does not work. If I understand your instructions correctly, it would seem as though I need to create a third field for this line (let’s call it “regrate1” which contains the number 75 and then create a script for “reg1total” which reads: reg1 * regrate1 Is that correct? If it is, I know how to create the field and make it “read only” but I’m uncertain how to pre-populate it with the number 75.

    Thanks for your patience!

    T

  44. Tim Jeske on January 7th, 2009

    BTW … I don’t know whether it makes any difference or not and I probably should have mentioned this earlier but I’m doing this in Acrobat Pro 6.0. Is that too ancient for this kind of effort?

  45. Stefan Cameron on January 7th, 2009

    Tim Jeske,

    It’s true you did say, “scripting in PDF.” I answered the question as though you were creating a PDF form using Adobe’s LiveCycle Designer. Sorry about that. I guess you’re trying to create an AcroForm Form (just a form in PDF, no XFA involved).

    In that case, I’m afraid I’m not in a very good position to help you out. I would suggest you ask your AcroForm-related questions on the Acrobat Users forum.

  46. Tim Jeske on January 7th, 2009

    Thanks Very Much! I got my answer straightaway!

    T

  47. Chris on January 9th, 2009

    Thanks for your ideas Stefan. I may end up having to move to Adobe LiveCycle Forms. You do a great job on this site.

  48. Ron on April 8th, 2009

    Hi,
    This blog is really very much interesting, i can’t resist to stop asking my question to everyone.

    Am having an import button, with the code call to xfa.host.import(); function, But after the import operation, all my variables declared globally are lost. Is there any way i can sustain them to perform operations, on the imported XML.
    Where should declare the variables, to be totally global?

    Thanks,
    Ron.

  49. Stefan Cameron on April 14th, 2009

    Ron,

    Unfortunately, when you merge data into the form, unless you’re not replacing the form’s current data set (which you need to do in order to get bindings to act on the new data you’re importing), the form’s state gets reset. Furthermore, the call to import data doesn’t occur immediately which means that even if you created local variables to hold the global variable values in order to re-set the global variables after importing the new data, you would no longer have access to those local variables after the import occurs.

    The only way to “persist” your global variable values is to store them in the data, merge the combined data into the form, and retrieve them from there afterward. The problem with xfa.host.importData() is that it doesn’t give you an opportunity to edit the XML data prior to merge it into the form.

    You could perhaps use Acrobat’s Doc.createDataObject() API prior to calling xfa.host.importData() in order to temporarily store the values of your global variables and then, if your global variables are reset the next time you get their values, you would know to reload them from the special Data Object you stored them in… Of course, this would only work if your form is used in Reader or Acrobat (not if it’s rendered as HTML or as a Form Guide).

  50. Hai on August 16th, 2010

    Hi I’m trying to do something similiar with form variables but as we mentioned in the post, for variables get reset after import. It was mentioned to use Doc.createDataObject.

    I’m actually trying to save a complex object instead of a string. Is there a way I can do this?
    I particular, I’m trying to keep track of an array of bookmark objects.

    thanks
    Hai

  51. Stefan Cameron on August 24th, 2010

    @Hai,

    You could always serialize your complex object to a string and rebuild it after data import.