Sergey Zakharchenko
Sergey Zakharchenko

Reputation: 104

Having problems setting a value to a choice field

I am trying to use iText 7 to populate pdf forms programmatically. I use C# libraries. everything goes well so but choice fields. When a value is set for the field it becomes kinder highlighted in the resulting pdf. looks like this:

enter image description here

this does not happen to other types of fields. I am using PdfFormField.SetValue(string value) function. I tried to cast type to PdfChoiceFormField with the same result.

Any help?

PS here is the form.

PPS One more observation: I understand that iText is not changing background randomly (and not supposed to :) but here are values of the instance before and right after field.SetValue("California").

values differences

Upvotes: 1

Views: 746

Answers (1)

OpenNingia
OpenNingia

Reputation: 76

To set the value of a choice field you need to use the SetListSelectedmethod like this:

string value = "hello world!";

if (ff is PdfChoiceFormField ch)
{
    ch.SetListSelected(new[] { value });
}
else
{
    ff.SetValue(value);
}

Of course value should be one of the value allowed by the Choice field.

Upvotes: 1

Related Questions