Frequently Asked Question:
Create Radiobuttons
Question
How can I create a group of radio buttons and I should be able to select only one radiobuttons at a time. I tried using the function NewFormField, but I was unsuccessful in creating one.
Please help
Answer
Quick PDF Library supports the creation and manipulation of radio button form fields, but it's a little trickier than working with text form fields. I have included some sample code below which demonstrates how to create radio buttons:
// Draw co-ordinates from top left corner of page
QP.SetOrigin(1);
// Draw some filler text
QP.DrawText(20, 20, 'Radio Button Test:');
// Add the first radio button form field
iDf1 := QP.NewFormField('Nationality', 4);
iDf101 := QP.AddFormFieldSub(iDf1, 'Australian');
QP.SetFormFieldCheckStyle(iDf101, 2, 1);
QP.SetFormFieldBounds(iDf101, 20, 60, 23, 23);
QP.SetFormFieldBorderColor(iDf101, 0, 0, 0);
// Add the second radio button form field
iDf102 := QP.AddFormFieldSub(iDf1, 'Swedish');
QP.SetFormFieldCheckStyle(iDf102, 2, 1);
QP.SetFormFieldBounds(iDf102, 20, 120, 23, 23);
QP.SetFormFieldBorderColor(iDf102, 0, 0, 0);
// Optional: Specify which radio button in
// this group is selected
QP.SetFormFieldValue(iDf1, 'Australian');
// Call the SetNeedAppearances function.
// Required to display form fields in
// PDF viewer.
QP.SetNeedAppearances(1);
// Save the new PDF form to disk
QP.SaveToFile('radio-buttons.pdf');
Some details:
- The NewFormField function lets you create new radio buttons. But if you want to create a radio button group then you need to add sub-radio buttons, using the AddFormFieldSub function, to the original radio button that you create using the NewFormField function. In most cases you can think of the NewFormField function as creating the parent radio button in your group. All sub-radio buttons will be derived from it.
- Using the SetFormFieldCheckStyle function is optional. It lets you specify which check style you want the radio button to use. Tick, circle, cross, etc. By default for a radio button a circle will be used.
- The SetFormFieldBounds function lets you specify the location of the radio button and the height and width of the radio button.
- The SetFormFieldBorderColor lets you specify the border color. It's necessary to specify a border color because if you don't, no border color will be used and you won't be able to see the radio button.
- You may want to pre-select one of the radio buttons, you can do this using the SetFormFieldValue function and the index number returned by the original call to the NewFormField function.
- Make sure you call the SetNeedAppearances function. This is required to tell the PDF viewer to display the form fields when the PDF is rendered.