We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Split radio group into multiple groups

I am renaming PDF form fields to unique values found in my local database. It basically maps the PDF form field to a field in my table, so I can dynamically load data to it at run-time.


The issue that I’m having is with RadioButtons. Let’s assume that I have a radio button called “Sex” with options “M” and “F”. My requirement is as follows:

1) Break the grouping in the RadioButton.
2) Name the first option “Sex_M”, and the second option “Sex_F”.

As it stands right now, when I rename “Sex” to “Sex_M”, I am really renaming the group name, and both radio buttons on the screen show up as “Sex_M”.

Any help is appreciated!


Hi Richard,


Thanks for contacting support.

We will really appreciate if you can share sample code snippet you are using to rename the options along with the input file. We will investigate it in our environment and update you accordingly. We are sorry for the inconvenience.

Best Regards,
Hi,

Here's the code snipped and file. I've simplified the code a bit for the example. In the attached PDF is a radio option called "Sex". It has two choices "M" or "F". I need to break this up into two radio buttons, one with a choice of "M" called "q123_M" instead of "Sex", and the second should be "q123_F". Basically, I need to treat the radio options as checkboxes, and break the grouping in order to rename them properly.

Document doc = new Document(stream);
// Create instance of FormEditor
FormEditor frmEdit = new FormEditor(doc);
frmEdit.RenameField("Sex", "q123_M"); // for MALE option
frmEdit.RenameField("Sex", "q123_F"); // for FEMALE option
frmEdit.Save();

The reason for this is that I have other code that reads the name "q123_M", and figures out which of the two choices to select programmatically. The naming convention is used to map back to my full dataset.

Hi Richard,

Thanks for sharing the input file and sample code snippet. I am afraid that you are using old Aspose.Pdf.Facades approach is going to be obsolete soon. We are not resolving issue(s) related to the old approach. We strongly recommend using the new Document Object Model (DOM) approach.

By using the DOM approach you can rename the RadioButtonOptionField and also set their values programmatically. You do not need to split the Group just to rename the Options so that your other code can read the specified name and select it. Please check the following code snippet to rename the option fields, save the document, and then set option values respectively. I hope this will help.

// Loaddocument
Document pdfDocument = new Document("name.pdf");
foreach(var field in pdfDocument.Form.Fields)
{
if(field is RadioButtonOptionField)
{
RadioButtonOptionField rbf = (RadioButtonOptionField)field;
if (rbf.OptionName.Equals("Male"))
rbf.Name = "q123_M";
if (rbf.OptionName.Equals("Female"))
rbf.Name = "q123_F";
}
}
// Save Document
pdfDocument.Save("Updated_name.pdf");
// Load Document Again with updated option names
pdfDocument = new Document("Updated_name.pdf");
foreach (var field in pdfDocument.Form.Fields)
{
if (field is RadioButtonOptionField)
{
RadioButtonOptionField rbf = (RadioButtonOptionField)field;
// Set the value of "Male" option

if (rbf.Name == "q123_M")
rbf.ActiveState = "Male";
}
}
// Save document with updated values
pdfDocument.Save("name_malechecked.pdf");

I have also attached the output file(s). If you still face further issues please feel free to contact us.

Best Regards,

Hi,


I appreciate the code, but that’s not what I’m looking for. I am attaching a copy of the PDF with the expected output.

What you’ll see in the attached is that I have two radio buttons, one called “q123_M”, and the other “q123_F”, and each one has an option. The name of the option is not really important. The name of the field is very important, and that’s what I can’t seem to set for both choices.

Thank you for your help.
Alex

Hi Richard,


Thanks for sharing more details. I tried to change the name of RadioButtonField in the input file (name.pdf) and was able to achieve that. Please check the following code snippet to change the name of RadioButtonField. I am also attaching the output generated by the below code.

Document pdfDocument = new Document(“name.pdf”);

foreach(var field in pdfDocument.Form)

{

if(field is RadioButtonField)

{

RadioButtonField rbf = (RadioButtonField)field;

rbf.Name = "Updated_Name";

}

}

pdfDocument.Save("name_out.pdf");


Moreover I am still investigating on modifying radio buttons so that they can work as check boxes. As there is already grouping in the Document it will take some time. I will update you and share my findings about this perspective. Please have patience.


Best Regards,