How to Add Radio Button Items to Pages Using PDF in .NET Core?
The Syncfusion Essential® PDF is a comprehensive, high-performance .NET PDF library that enables developers to create, read, and modify PDF documents programmatically without relying on Adobe components. It provides advanced features such as merging, splitting, stamping, form handling, compression, and securing PDF files. This library enables you to add radio button items to multiple pages with group synchronization in a PDF document.
Steps to add grouped radio button fields to multiple pages in a PDF programmatically
- Create a new project: Start a new console application in .NET core.
- Install required packages: Add the Syncfusion.Pdf.Net.Core NuGet package to your project.
- Include namespaces: In your
Program.csfile, include the essential namespaces.
using Syncfusion.Drawing;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf.Interactive;
- Use the following code example to add synchronized radio button items across multiple PDF pages.
// Create a new PDF document
using (PdfDocument document = new PdfDocument())
{
// Create two pages
PdfPage page1 = document.Pages.Add();
PdfPage page2 = document.Pages.Add();
// Access the form
PdfForm form = document.Form;
form.FieldAutoNaming = false;
// Label font
PdfFont labelFont = new PdfStandardFont(PdfFontFamily.Helvetica, 12f, PdfFontStyle.Regular);
// Group name
string groupName = "EmployeesRadioGroup";
float labelOffsetX = 10f;
float labelOffsetY = -2f;
// Create ONE radio button list field (anchor it on the first page; items can be on any page)
PdfRadioButtonListField radioField = new PdfRadioButtonListField(page1, groupName)
{
// Keep standard radio behavior: only one selection in the group
AllowUnisonSelection = false
};
// Define the 4 radio items: 2 on page 1, 2 on page 2
var items = new (PdfPage page, string export, RectangleF bounds, string label)[]
{
(page1, "rb1", new RectangleF(100f, 200f, 20f, 20f), "Radio Button 1"),
(page1, "rb2", new RectangleF(100f, 240f, 20f, 20f), "Radio Button 2"),
(page2, "rb3", new RectangleF(100f, 200f, 20f, 20f), "Radio Button 3"),
(page2, "rb4", new RectangleF(100f, 240f, 20f, 20f), "Radio Button 4"),
};
// Add items to the single radio field and draw their labels
foreach (var (page, export, bounds, label) in items)
{
PdfRadioButtonListItem item = new PdfRadioButtonListItem(page, export)
{
Bounds = bounds
};
radioField.Items.Add(item);
// Label beside the radio button
page.Graphics.DrawString(label, labelFont, PdfBrushes.Black,
bounds.Right + labelOffsetX, bounds.Y + labelOffsetY);
}
// Set default selection
radioField.SelectedValue = "rb4";
// Add the single field to the form
form.Fields.Add(radioField);
// Save the PDF document
document.Save("Output.pdf");
}
A complete working sample is available for download from GitHub.
By executing the program, you will generate the following PDF document.
Take a moment to peruse the documentation to learn how to add form fields in a PDF document.
Conclusion
I hope you enjoyed learning how to synchronize radio button items across multiple pages in a PDF document.
You can refer to our ASP.NET Core PDF feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our ASP.NET Core PDF example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!