PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Fri Jun 22, 2018 6:46 pm

All times are UTC

Forum rules

Please read this before posting on this forum: Forum Rules

Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: Fri Feb 06, 2009 10:35 pm 

Joined: Fri Feb 06, 2009 10:17 pm
Posts: 6
Location: Plano Texas
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

            // 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:

            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;


Reply with quote  
PostPosted: Fri Aug 18, 2017 8:15 pm 

Joined: Fri Aug 18, 2017 7:47 pm
Posts: 1
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:

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

PdfAcroForm form = _document.AcroForm;


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":

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));
     _document.AcroForm.Elements["/NeedAppearances"] = new PdfBoolean(true);

//Save as new document

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.

Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC

Who is online

Users browsing this forum: Google [Bot] and 6 guests

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group