PDFsharp & MigraDoc Foundation

How do I fill radiobuttons within a form?
Page 1 of 1

Author:  ulrichK [ Tue Apr 16, 2024 9:32 am ]
Post subject:  How do I fill radiobuttons within a form?

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'.

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)
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 };

    return options;

Author:  ulrichK [ Tue Apr 23, 2024 8:07 am ]
Post subject:  Re: How do I fill radiobuttons within a form?

Hey all,

I found the solution to my problem.

The problem was, that I grouped the radiobuttons in a wrong way in LibreOffice Write. I entered a group name and a name for the radio button. But this way of grouping radiobuttons seems to be wrong.

In Libre Office Write, you have to select all the buttons that belong to a group with holding down the SHIFT key, so they get this blueish marking. Then right click on them and enter "Control Properties". Then enter a name in the first line and nothing else.

In your code you then can activate a radiobutton of this group with

PdfRadioButtonField? radioField = fields[i] as PdfRadioButtonField; //gets the group you created in LibreOffice
SetOptionByIndex(radioField, 0); //the second parameter is the index of the button in the group.

Hope this helps anyone who encounters the same issue.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group