PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Mar 28, 2024 3:04 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 
Offline

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

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


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

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:

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.


Top
 Profile  
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: No registered users and 112 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