Solved

How do I populate a text box with a list box?

Posted on 2008-10-07
10
874 Views
Last Modified: 2011-10-19
Acroforms - Javascript - Adobe Acrobat Pro v8.12

Hello all...

How can I use Acrobat javascript to populate a text box with
the multi selected values from a list box?

EXAMPLE:

---------------------
   MultiSelect
    Listbox1

<Blank>
Apples
[Oranges]  <--Selected
Pears
Cherries
[Grapes]   <--Selected
Figs
[Plums]    <--Selected
---------------------

RESULTS:
---------------------
    Textbox1

Selected: Oranges, Grapes, Plums
---------------------

I placed the following Keyboard Script in
Combo box2, to populate Textbox2 with the
selection from Combo box2:

var targ1=getField("Textbox2");
if(!event.willCommit){
    targ1.value=event.change;
}

Can it be modified to handle the multi selected
items in listbox1 to populate Textbox1?
0
Comment
Question by:dartanion_jm
  • 5
  • 5
10 Comments
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 22668202
Do you have the "JavaScript for Acrobat API Reference" document from Adobe? You need that if you want to do any serious JavaScript development.

Take a look at the property "currentValueIndices" of the Field  class. The following code is based on the example given in the reference document.

I've not actually run this in an Acrobat environment, so it may not work the way I've written it, but it's at least very close. Once you have access tot the reference manual, and look up the currentValueIndices property, you should be able to get things working. If not, let me know, and I'll actually create a PDF file that shows how this can work.



var textFieldNewValue = ""
 
var f = this.getField("ListBox1");
var a = f.currentValueIndices;
 
if (typeof a == "number") // A single selection
{
     textFieldNewValue = f.getItemAt(a, false));
}
else // Multiple selections
{
    console.println("Selection:");
    for (var i = 0; i < a.length; i ++)
    {
        if (i!=0_
        {
            textFieldNewValue += " ";
        }
         textFieldNewValue += f.getItemAt(a[i], false);
    }
}
 
targ1.value = textFieldNewValue;

Open in new window

0
 

Author Comment

by:dartanion_jm
ID: 22675820
Thank you for the prompt response. Will give it a try and get back to you.
0
 

Author Comment

by:dartanion_jm
ID: 22687980
"If not, let me know, and I'll actually create a PDF file that shows how this can work."

I'm going to take you up on your gracious offer to create a PDF file.

When trying the script, I got the following error message:

  Error:  Missing ; before: statement 6 at line 7

                    textFieldNewValue = f.getItemAt(a, false));
  Error---->  }
                   else // Multiple selections
                   {

Couldn't figure it out. (Reference:  Adobe Acrobat SDK v8.0 NOV 2006 - Pg 80 / Example 6.2)

Thanks again for your efforts.

0
On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 22693720
As indicated, I did not actually run this code, so no interpreter had a chance to point out all my typos...

There should only be one closing parenthesis after false:

    textFieldNewValue = f.getItemAt(a, false);
}
else // Multiple selections
{
0
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 22693744
There was of course one more typo in the code. I now actually run it in a PDF file and came up with the following - corrected -s ample code.

The sample assumes that you have two objects, one list box and one text box. The list box is called ListBox1, and the text field has a custom calculation script that is set to the following program.
var textFieldNewValue = ""
 
var f = this.getField("ListBox1");
var a = f.currentValueIndices;
 
if (typeof a == "number") // A single selection
{
     textFieldNewValue = f.getItemAt(a, false);
}
else // Multiple selections
{
    console.println("Selection:");
    for (var i = 0; i < a.length; i ++)
    {
        if (i!=0)
        {
            textFieldNewValue += " ";
        }
         textFieldNewValue += f.getItemAt(a[i], false);
    }
}
 
event.target.value = textFieldNewValue;

Open in new window

0
 

Author Comment

by:dartanion_jm
ID: 22696795
Greetings!

Thank you for your continued efforts.  

I get the following error when trying the last version of the script:

  InvalidSetError: Set not possible, invalid or unknown.
  Field.value:23:Field listbox1:Keystroke

FYI - In my test file the listbox is called "listbox1". I did rename it in the script before running it.

See attached: Listbox_DEV_101208_0517a.pdf

Thanks again.
Listbox-DEV-101208-0517a.pdf
0
 
LVL 44

Accepted Solution

by:
Karl Heinz Kremer earned 500 total points
ID: 22697289
The script has to be associated with the text field. I've modified the PDF file and attached it to this comment. You will see the update in the text field as soon as you commit the values in the list box (e.g. by tabbing out of the list box, or by clicking on any other object on the page).
Listbox-DEV-101208-0517a-modifie.pdf
0
 

Author Closing Comment

by:dartanion_jm
ID: 31504024
The Matrix (1999) - Memorable quote by Morpheus: "You are the One, Neo".  Much Appreciated!   Just what I was looking for. Khkremer.....you are "the One"! Thanks again.
0
 

Author Comment

by:dartanion_jm
ID: 22698084
The Matrix (1999) - Memorable quote by Morpheus: "You are the One, Neo".  Much Appreciated!   Just what I was looking for. Khkremer.....you are "the One"!  Thanks again.
0
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 22699122
Thanks :)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The ability to edit PDF documents can be useful, however it may not be a straight forward process. Many non-technical people don't realise that a PDF document is basically an image rather than a text file, even if it contains nothing but text. If…
One of the questions I get asked again and again is how to validate a field value in an AcroForm with a custom validation script. Adobe provided a lot of infrastructure to do that with just a simple script. Let’s take a look at how to do that wit…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question