PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

How to Read/ Set Radio Button on AcroForm
https://forum.pdfsharp.net/viewtopic.php?f=2&t=632
Page 1 of 1

Author:  sorcerer7 [ Fri Feb 06, 2009 10:35 pm ]
Post subject:  How to Read/ Set Radio Button on AcroForm

I have an AcroFrom that has several groups of radio buttons and I am currently unable to set the value using code. One of the groups of radio buttons is named "male or female".

I am using PDFSharp v1.2

Code:
            // Get the root object of all interactive form fields
            PdfAcroForm form = document.AcroForm;

            // Get all form fields of the whole document
            PdfAcroField.PdfAcroFieldCollection fields = form.Fields;

            // Get all form fields of the whole document
            string[] names = fields.Names;
            names = fields.DescendantNames;

            string fqName;
            PdfAcroField field;

            PdfRadioButtonField radField;

            fqName = "male or female";
            field = fields[fqName];
            radField = field as PdfRadioButtonField;



What would I need here to read the value or set the value of the PdfRadioButton radField?

What if anything should I be doing differently?

I am able to set text fields and checkboxes without any issue using:

Code:
            PdfTextField txtField;
            PdfCheckBoxField chkField;

            fqName = "nameoftextfieldtobeset(0)";
            field = fields[fqName];
            txtField = field as PdfTextField;
            txtField.Text = "text value being set";

            fqName = "nameofcheckboxtobeset(0)";
            field = fields[fqName];
            chkField = field as PdfCheckBoxField;
            chkField.Checked = true;



Thanks,
Chris

Author:  williams3 [ Fri Aug 18, 2017 8:15 pm ]
Post subject:  Re: How to Read/ Set Radio Button on AcroForm

I know it has been years and this question never got a response, but I ran into this same issue and discovered a solution. So hopefully this helps anyone else who stumbles across this post.

For this solution, I was using PDFsharp v1.50.4589-beta4.

To figure this out, I used Adobe Acrobat to create a simple radio button group called "Radio1" with four buttons each named "Choice1", "Choice2", "Choice3", and "Choice4". I selected the "Choice1" button so that the radio field would have a value and then used breakpoints in Visual Studio to examine the AcroForm. I could see that the field "Radio1" was set to a value of "/Choice1" but I was unsure of the type that the value had and therefore unable to change it. So I ran the following code:

Code:
PdfDocument _document = PdfReader.Open("test.pdf", PdfDocumentOpenMode.Modify);

PdfAcroForm form = _document.AcroForm;

Console.WriteLine(form.Fields["Radio1"].Value.GetType().ToString());


This gave me an output of "PdfSharp.Pdf.PdfName". So I ran the following code to change the radio button to the third button, "Choice3":

Code:
PdfDocument _document = PdfReader.Open("test.pdf", PdfDocumentOpenMode.Modify);

PdfAcroForm form = _document.AcroForm;

//Select third button
form.Fields["Radio1"].Value = new PdfName("/Choice3");

//Ensure the new values are displayed
if (_document.AcroForm.Elements.ContainsKey("/NeedAppearances") == false)
     _document.AcroForm.Elements.Add("/NeedAppearances", new PdfBoolean(true));
else
     _document.AcroForm.Elements["/NeedAppearances"] = new PdfBoolean(true);

//Save as new document
_document.Save(@"testcopy.pdf");


And when I opened the "testcopy.pdf" in Acrobat, the radio button group did indeed have the third button selected.

If someone has a more intuitive way of accomplishing this, please let me know. For now, this is how I programmatically manipulate a radio button field.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/