Hello again,
I am struggeling with filling a form via PDFsharp, especially I am not able to figure out how to use the radiobuttons.
In LibreOffice I created a form and gave names to all the form items. I can read out the names of textboxes and checkboxes and can fill them or check them.
But when it comes to radiobuttons I am confused. In LibreOffice I create a group of radiobuttons and export the form as pdf. Lets say the group is called 'radiogroup' and the buttons are called 'opt_yes' and 'opt_no'. When I read out all the field names with the code below, then there is no group name shown, just the first option in the group 'opt_yes' twice. I get one entry with "opt_yes 0 /1' and one entry with 'opt_yes 1 /2'.
Code:
...
PdfRadioButtonField? radioField = fields[i] as PdfRadioButtonField;
List<ErPdfRadioOption> options = FindOptions(radioField);
foreach (var option in options)
{
Console.WriteLine(radioField.Name.ToString() + " " + option.Index + " " + option.Value);
}
...
How do I get the group name as I need this for my autofill program. And how do I set one of the options in the group? Maybe there is a Sample for that, but I wasn't able to find.
Again, thanks in advance!
Here the code for the function/class I use (found them here on the forum)
Code:
public class ErPdfRadioOption
{
public int Index { get; set; }
public string Value { get; set; }
}
public static List<ErPdfRadioOption> FindOptions(PdfRadioButtonField source)
{
if (source == null) return null;
List<ErPdfRadioOption> options = new List<ErPdfRadioOption>();
PdfArray kids = source.Elements.GetArray("/Kids");
int i = 0;
foreach (var kid in kids)
{
PdfReference kidRef = (PdfReference)kid;
PdfDictionary dict = (PdfDictionary)kidRef.Value;
PdfDictionary dict2 = dict.Elements.GetDictionary("/AP");
PdfDictionary dict3 = dict2.Elements.GetDictionary("/N");
if (dict3.Elements.Keys.Count != 2)
throw new Exception("Option dictionary should only have two values");
foreach (var key in dict3.Elements.Keys)
if (key != "/Off")
{ // "Off" is a reserved value that all non-selected radio options have
ErPdfRadioOption option = new ErPdfRadioOption() { Index = i, Value = key };
options.Add(option);
}
i++;
}
return options;
}