How to I set values on a combo-box or multi-selection list?

Q:

I am very impressed with your SDK-s ability to read and update forms.
I have searched your site to find out how to populate a combo-box but
have not seen any examples for this. How to I set values on a combo-
box or a multi-selection list?
----

A:
Combo boxes and list boxes are represented in PDF using the 'choice'
field type (in PDFNet it is pdftron.PDF.Field.Type.e_choice). A choice
field contains several text items, one or more of which may be
selected as the field value. The items may be presented to the user in
either of two forms:

- A scrollable list box
- A combo box consisting of a drop-down list optionally accompanied by
an editable text box in which the user can type a value other than the
predefined choices (In PDFNet they are differentiated using
pdftron.PDF.Field.Flag.e_combo flag; If this flag is set, the field is
a combo box otherwise the field is a list box).

To set the value of any form field, you can use Field.SetValue(value)
method.

In case of choice fields, the value identifies the item or items
currently selected in the choice field. If the field does not allow
multiple selection (that is if the
pdftron.PDF.Field.Flag.e_multiselect is not set) or if multiple
selection is supported but only one item is currently selected, the
value should be a text string (i.e. one of the values specified in
field dictionary's 'Opt' array). For example:

C#: field.SetValue(pdftron.SDF.Obj.CreateString("Item1"));
VB.Net: field.SetValue(pdftron.SDF.Obj.CreateString("Item1"))
C++: field.SetValue(new pdftron::SDF::Str("Item1"));

If multiple items are selected, the value should be an array of such
strings. For example:

C#:
Obj selection = pdftron.SDF.Obj.CreateArray();
selection.PushBack(pdftron.SDF.Obj.CreateString("Item1"));
selection.PushBack(pdftron.SDF.Obj.CreateString("Item2"));
selection.PushBack(pdftron.SDF.Obj.CreateString("Item4"));
field.SetValue(selection);

C++:
Obj* selection = new pdftron::SDF::Array();
selection.PushBack(new Str("Item1"));
selection.PushBack(new ("Item2"));
selection.PushBack(new ("Item4"));
field.SetValue(selection);

If no items should be selected the value should be set to Null. For
example:

C#: field.SetValue(pdftron.SDF.Obj.CreateNull());
VB.Net: field.SetValue(pdftron.SDF.Obj.CreateNull());
C++: field.SetValue(new pdftron::SDF::Null());

The default value for 'choice' field types is null, indicating that no
item is currently selected.

The following the full sample code referenced in the previous article:

using System;
using pdftron;
using pdftron.Common;
using pdftron.Filters;
using pdftron.SDF;
using pdftron.PDF;

namespace FormsTestCS
{
  class Class1
  {
    static void Main(string[] args)
    {
      PDFNet.Initialize();
      PDFNet.SetResourcesPath("../../../../../resources");

      try
      {
        PDFDoc doc = new PDFDoc("1.pdf");
        doc.InitSecurityHandler();

        FieldIterator itr, end = doc.FieldEnd();

        // Search for a specific field
        itr = doc.FieldFind("MyCombo");
        if (itr != end)
        {
          Field f = itr.Current();
          Console.WriteLine("Field search for {0} was successful.",
f.GetName());
          if (f.GetType() == Field.Type.e_choice)
          {
            f.SetFlag(Field.Flag.e_multiselect, true);
            Obj selection = pdftron.SDF.Obj.CreateArray();
            selection.PushBack(pdftron.SDF.Obj.CreateString("Item1"));
            selection.PushBack(pdftron.SDF.Obj.CreateString("Item4"));
            f.SetValue(selection);
          }
        }
        else
        {
          Console.WriteLine("Field search failed.");
        }

        // Regenerate field appearances.
        doc.RefreshFieldAppearances();
        doc.Save("2.pdf", 0);
        doc.Close();
        Console.WriteLine("Done.");
      }
      catch (PDFNetException e)
      {
        Console.WriteLine(e.Message);
      }

      PDFNet.Terminate();
    }
  }
}

The code in the previous posts is no longer valid. The following is the updated code to set a choice field with multiple values

`
Field f = doc.GetField(“MyCombo”);
if (f != null)
{
if (f.GetType() == Field.Type.e_choice)
{
f.SetFlag(Field.Flag.e_multiselect, true);

Obj selection = doc.CreateIndirectArray();
selection.PushBackString(“Item1”);
selection.PushBackString(“Item4”);
f.SetValue(selection);
}
}
`

1 Like