Create Grouped Checkbox using Aspose.PDF for .NET - multiple checkboxFields within a single group

I am attempting to create a group of checkboxFields, where they are all on the same row and only one can be selected at a time. Currently we use the below approach to create the checkboxFields, however this results in each checkboxField being on its own row and group. How can I achieve multiple checkboxFields within a single group?

public void CreateCheckbox(SpecRow sr)
 
{
        Aspose.Pdf.Page pdfPage = Doc.Pages[currentPage];

        double bottomY = pdfPage.Rect.LLY;
        double offset = 25;

        if (lastY - offset - sr.Height < bottomY)
        {
            Doc.Pages.Add();
            currentPage++;
            pdfPage = Doc.Pages[currentPage];
            lastY = pdfPage.Rect.URY;
        }

        Aspose.Pdf.Rectangle fldRect = new Aspose.Pdf.Rectangle(
            pdfPage.Rect.LLX,               // fieldLLX
            lastY - offset - sr.Height,     // fieldLLY                                                                 // Height
            pdfPage.Rect.LLX + sr.Width,    // fieldURX,                                                                // Width
            lastY - offset                  // fieldURY
            );
        CheckboxField checkBox = new CheckboxField(pdfPage, fldRect);
        checkBox.PartialName = sr.Name;                                                                                 // Name
        checkBox.DefaultAppearance.FontName = sr.Font;                                                                  // Font
        checkBox.DefaultAppearance.FontSize = sr.FontSize != null ? (double)sr.FontSize :
            checkBox.DefaultAppearance.FontSize;                                                                        // Font Size
        checkBox.DefaultAppearance.TextColor = System.Drawing.Color.FromName(sr.TextColor);                             // Text Color
        checkBox.AlternateName = sr.Tooltip;                                                                            // Tooltip
        if (!string.IsNullOrEmpty(sr.BorderColor))
        {
            checkBox.Border = new Aspose.Pdf.Annotations.Border(checkBox);
            checkBox.Border.Style =
                (Aspose.Pdf.Annotations.BorderStyle)Enum.Parse(typeof(Aspose.Pdf.Annotations.BorderStyle), sr.BorderStyle); // Border Style
            checkBox.Border.Width = (int)sr.BorderThickness;                                                            // Border Thickness
            checkBox.Characteristics.Border = System.Drawing.Color.FromName(sr.BorderColor);                            // Border Color
        }
        if (!string.IsNullOrEmpty(sr.FillColor))
        {
            checkBox.Characteristics.Background = System.Drawing.Color.FromName(sr.FillColor);                            // Fill Color
        }
        if (!string.IsNullOrEmpty(sr.ExportValue))
        {
            checkBox.Exportable = true;
            checkBox.Value = sr.ExportValue;
            checkBox.ExportValue = sr.ExportValue;                                  // Export Value
        }
        checkBox.Characteristics.Rotate = (Aspose.Pdf.Rotation)Enum.Parse(typeof(Aspose.Pdf.Rotation), sr.Orientation); // Orientation
        checkBox.Flags = !string.IsNullOrEmpty(sr.FormField) ?
            (Aspose.Pdf.Annotations.AnnotationFlags)Enum.Parse(typeof(Aspose.Pdf.Annotations.AnnotationFlags), sr.FormField) :
            checkBox.Flags;                                                                                             // Form Field
        checkBox.TextHorizontalAlignment = !string.IsNullOrEmpty(sr.HorizontalAlignment) ?
            (Aspose.Pdf.HorizontalAlignment)Enum.Parse(typeof(Aspose.Pdf.HorizontalAlignment), sr.HorizontalAlignment) :
            checkBox.TextHorizontalAlignment;                                                                               // Horiz Align
        checkBox.ReadOnly = sr.ReadOnly;                                                                                // Read Only
        checkBox.Required = sr.Required;                                                                                // Required
        checkBox.Style = (Aspose.Pdf.Forms.BoxStyle)Enum.Parse(typeof(Aspose.Pdf.Forms.BoxStyle), sr.CheckboxStyle);    // Button Style
        checkBox.Checked = (bool)sr.CheckedByDefault;                                                                   // Checked By Default
        // checkBox.IsSynchronized = sr.CheckedInUnison;                                                  // IsSynchronized is read only

        Doc.Form.Add(checkBox);
        lastY = fldRect.LLY;
    }

@gewangdfsco

In order to add grouped check boxes, you need to use RadioButtonField instead of CheckboxField. You can change BoxStyle of the radio buttons to make them look like check box. Please check following article in API documentation and in case you still face any issue, please feel free to let us know.

@asad.ali

Thank you for the response. The reason we are not using RadioButtonField instead is because we need the ability to have none checked in a group, as well as one checked in a group. When using RadioButtonFields there has to be a default selection, if I’m not mistaken. Is there a way to set up a RadioButtonField such that it allows no selection, or would I have to create another option in the field called “None” to handle such a case?

@gewangdfsco

You can add grouped radio buttons without making a default selection and in the output PDF, none of the options would be selected. For example, please check attached output PDF and below code snippet which was used to generate it:

Document pdfDocument = new Document();
Page pdfPage = pdfDocument.Pages.Add();

Table table = new Table();
table.Margin = new MarginInfo { Left = 20, Right = 20, Top = 10, Bottom = 0 };
//pdfPage.Paragraphs.Add(table);
table.ColumnWidths = "50 50";

RadioButtonField rf = new RadioButtonField(pdfPage);
rf.PartialName = "GroupedRadio";

RadioButtonOptionField option = new RadioButtonOptionField();
            
option.OptionName = "Yes";
option.Width = 15;
option.Height = 15;
option.Caption = new TextFragment("Yes");
rf.MappingName = "1";
rf.Add(option);
option.Border = new Border(option);
option.Border.Dash = new Dash(1, 3);
option.Border.Effect = BorderEffect.Cloudy;
option.Border.EffectIntensity = 1;
option.Border.Style = BorderStyle.Dashed;

RadioButtonOptionField option2 = new RadioButtonOptionField();

option2.OptionName = "No";
option2.Width = 15;
option2.Height = 15;
option2.Caption = new TextFragment("No");
rf.MappingName = "2";
            
rf.Add(option2);

//rf.Selected = 2;

rf.Add(option);
rf.Add(option2);

pdfDocument.Form.Add(rf, pdfPage.Number);

pdfPage.Paragraphs.Add(option);
pdfPage.Paragraphs.Add(option2);

Row row = table.Rows.Add();
Cell cellYesField = row.Cells.Add();
cellYesField.Paragraphs.Add(option);
Cell cellNoField = row.Cells.Add();
cellNoField.Paragraphs.Add(option2);
pdfDocument.Save(dataDir + @"RadioButton_out.pdf");

RadioButton_out.pdf (42.2 KB)

@asad.ali Thank you for the example! Are RadioButtons able to go from having an option selected to having none selected? I guess I wasn’t completely clear with my second question. We want fields where there is no default selection, and we can check/uncheck an option at will. From my understanding of the above code, if I selected an option I then would not be able to then go back to no selection?

@gewangdfsco

Yes, your understandings are correct. You cannot uncheck a radio button if it is already checked. In order to deal with such case, you can add a third option with the caption of “None” if it suits you. Please let us know in case you need further assistance.