Adding radio buttons throws error

I’ve been able to add different form fields to a PDF (Aspose.PDF 19.5), except for RadioButtonFields. I copied different examples from the aspose git repo, but 1 throws an exception and I don’t see any radio buttons in my second sample:

Sample 1:

RadioButtonField field = new RadioButtonField(page);
field.Rect = new Aspose.Pdf.Rectangle(40, 650, 100, 720);
field.PartialName = “MyRadio”;

RadioButtonOptionField opt1 = new RadioButtonOptionField();
opt1.Rect = new Aspose.Pdf.Rectangle(40, 650, 60, 670);
opt1.OptionName = “Option1”;
opt1.Border = new Border(opt1); <--------- Throws ‘Holder area not set’ exception
opt1.Border.Width = 1;
opt1.Characteristics.Border = System.Drawing.Color.Black;

Sample 2:

RadioButtonField field = new RadioButtonField(page);
field.Rect = (a large rect);
field.PartialName = “MyRadio”;
RadioButtonOptionField opt = new RadioButtonOptionField();
opt.Rect = new Rectangle(a rect within field’s rect);
opt.MappingName = “enumValue”;
opt.OptionName = “enumLabel”;
field.Add(opt);
(plus 2 more option fields…)

In Sample 2, I don’t see any radio buttons. I also can’t set its border without getting the same exception as in Sample 1.

@dilbertp

Thank you for contacting support.

You may use below code snippet to add radiobuttons in your PDF documents.

Document doc = new Document();
Page page = doc.Pages.Add();

RadioButtonField rb = new RadioButtonField(doc);
rb.PartialName = "YesNoOption";
doc.Form.Add(rb, 1);

RadioButtonOptionField rbo1 = new RadioButtonOptionField();
RadioButtonOptionField rbo2 = new RadioButtonOptionField();

rbo1.OptionName = "YES";
rbo2.OptionName = "NO";
rbo1.Width = 12;
rbo1.Height = 12;
rbo2.Width = 12;
rbo2.Height = 12;

rbo1.IsInLineParagraph = true;
rbo2.IsInLineParagraph = true;

rb.Add(rbo1);
rb.Add(rbo2);

rbo1.Caption = new TextFragment("YES");
rbo2.Caption = new TextFragment("NO");
rbo1.Border = new Border(rbo1);
rbo1.Border.Width = 1;
rbo1.Border.Style = BorderStyle.Solid;
rbo1.Characteristics.Border = System.Drawing.Color.Black;
rbo1.DefaultAppearance.TextColor = System.Drawing.Color.Black;

rbo2.Border = new Border(rbo2);
rbo2.Border.Width = 1;
rbo2.Border.Style = BorderStyle.Solid;
rbo2.Characteristics.Border = System.Drawing.Color.Black;
rbo2.DefaultAppearance.TextColor = System.Drawing.Color.Black;

page.Paragraphs.Add(rbo1);
page.Paragraphs.Add(rbo2);

// Save the PDF file
doc.Save(dataDir + "Radio_19.6.pdf");

We hope this will be helpful. Please feel free to contact us if you need any further assistance.

Thank you - that worked. However, I’m trying to position the radio button group to a text fragment’s position and can’t quite figure out how to do that with the sample you provided. Setting the options’ rect like before makes them disappear. Is there a way to do that?

Thanks!

@dilbertp

Thank you for your kind feedback.

To add the radio button at specific coordinates on a page, you may use a Table wrapper and set the Top and Leftproperty for setting position of RadioButtonField. We have devised below code snippet for your kind reference.

Document doc = new Document();
Page page = doc.Pages.Add();
Aspose.Pdf.Table table = new Aspose.Pdf.Table();

//Set position here
table.Left = 200;
table.Top = 300;

table.ColumnWidths = "120";
page.Paragraphs.Add(table);
Aspose.Pdf.Row r1 = table.Rows.Add();
Aspose.Pdf.Row r2 = table.Rows.Add();
Aspose.Pdf.Cell c1 = r1.Cells.Add();
Aspose.Pdf.Cell c2 = r2.Cells.Add();

RadioButtonField rf = new RadioButtonField(page);
rf.PartialName = "radio";
doc.Form.Add(rf, 1);

RadioButtonOptionField opt1 = new RadioButtonOptionField();
RadioButtonOptionField opt2 = new RadioButtonOptionField();

opt1.OptionName = "Yes";
opt2.OptionName = "No";

opt1.Width = 15;
opt1.Height = 15;
opt2.Width = 15;
opt2.Height = 15;

rf.Add(opt1);
rf.Add(opt2);

opt1.Border = new Border(opt1);
opt1.Border.Width = 1;
opt1.Border.Style = BorderStyle.Solid;
opt1.Characteristics.Border = System.Drawing.Color.Black;
opt1.DefaultAppearance.TextColor = System.Drawing.Color.Red;
opt1.Caption = new TextFragment("Yes");
opt2.Border = new Border(opt1);
opt2.Border.Width = 1;
opt2.Border.Style = BorderStyle.Solid;
opt2.Characteristics.Border = System.Drawing.Color.Black;
opt2.DefaultAppearance.TextColor = System.Drawing.Color.Red;
opt2.Caption = new TextFragment("No");
c1.Paragraphs.Add(opt1);
c2.Paragraphs.Add(opt2);
doc.Save(dataDir + "TableRadio_19.6.pdf");

We hope this will be helpful. Please feel free to contact us if you need any further assistance.