Since this is high on the Google search results for PDFSharp and radio buttons, figured I'd share what seems to be working for me.
I created extension functions for the PdfRadioButtonField object that:
- Gets all option values and their indexes
- Sets the value for a radio field by index number
- Sets the value for a radio field by value
In the following examples, ErPdfRadioOption is:
Code:
public class ErPdfRadioOption
{
public int Index { get; set; }
public string Value { get; set; }
}
Get all available options for a radio fieldCode:
public static List<ErPdfRadioOption> FindOptions(this 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;
}
Set the option for a radio field by indexYou'd think this is what the SelectedIndex property should be for... but apparently not.
Code:
public static void SetOptionByIndex(this PdfRadioButtonField source,int index) {
if (source == null) return;
List<ErPdfRadioOption> options = source.FindOptions();
ErPdfRadioOption selectedOption = options.Find(x => x.Index == index);
if (selectedOption == null) return; //The specified index does not exist as an option for this radio field
// https://forum.pdfsharp.net/viewtopic.php?f=2&t=3561
PdfArray kids = (PdfArray)source.Elements["/Kids"];
int j = 0;
foreach (var kid in kids) {
var kidValues = ((PdfReference)kid).Value as PdfDictionary;
//PdfRectangle rectangle = kidValues.Elements.GetRectangle(PdfAnnotation.Keys.Rect);
if (j == selectedOption.Index)
kidValues.Elements.SetValue("/AS", new PdfName(selectedOption.Value));
else
kidValues.Elements.SetValue("/AS", new PdfName("/Off"));
j++;
}
}
Note: This depends on the FindOptions() function above.
Set the option for a radio field by valueCode:
public static void SetOptionByValue(this PdfRadioButtonField source, string value) {
if (source == null || string.IsNullOrWhiteSpace(value)) return;
if (!value.StartsWith("/", StringComparison.OrdinalIgnoreCase)) value = "/" + value; //All values start with a '/' character
List<ErPdfRadioOption> options = source.FindOptions();
ErPdfRadioOption selectedOption = options.Find(x => x.Value == value);
if (selectedOption == null) return; //The specified index does not exist as an option for this radio field
source.SetOptionByIndex(selectedOption.Index);
}
Note: This depends on the FindOptions() and SetOptionByIndex() functions above.