Skip to content

Commit 01ee886

Browse files
committed
264229: Resolved the given feedback.
1 parent 513348f commit 01ee886

File tree

1 file changed

+35
-38
lines changed
  • Forms/Multi‑Page-PDF-Radio-Button-Group-Synchronization/.NET/Multi‑Page-PDF-Radio-Button-Group-Synchronization

1 file changed

+35
-38
lines changed

Forms/Multi‑Page-PDF-Radio-Button-Group-Synchronization/.NET/Multi‑Page-PDF-Radio-Button-Group-Synchronization/Program.cs

Lines changed: 35 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -6,51 +6,48 @@
66
// Create a new PDF document
77
using (PdfDocument document = new PdfDocument())
88
{
9-
// Add pages (adjust count as needed)
10-
PdfPage[] pages = { document.Pages.Add(), document.Pages.Add() };
11-
// Access the document form
9+
// Create two pages
10+
PdfPage page1 = document.Pages.Add();
11+
PdfPage page2 = document.Pages.Add();
12+
// Access the form
1213
PdfForm form = document.Form;
13-
// Disable auto field naming (we will name fields explicitly)
1414
form.FieldAutoNaming = false;
15-
// Common font for labels
15+
// Label font
1616
PdfFont labelFont = new PdfStandardFont(PdfFontFamily.Helvetica, 12f, PdfFontStyle.Regular);
17-
// Shared settings
18-
string groupName = "EmployeesRadioGroup"; // same group name across pages
19-
string[] exportValues = { "rb1", "rb2", "rb3" }; // consistent export values
20-
float[] startYs = { 200f, 300f }; // starting Y per page
21-
float labelOffsetX = 10f; // label X offset from radio button
22-
float labelOffsetY = -2f; // label Y tweak
23-
for (int p = 0; p < pages.Length; p++)
17+
// Group name
18+
string groupName = "EmployeesRadioGroup";
19+
float labelOffsetX = 10f;
20+
float labelOffsetY = -2f;
21+
// Create ONE radio button list field (anchor it on the first page; items can be on any page)
22+
PdfRadioButtonListField radioField = new PdfRadioButtonListField(page1, groupName)
2423
{
25-
PdfPage page = pages[p];
26-
// Create a radio button field for this page (same group name)
27-
PdfRadioButtonListField radioField = new PdfRadioButtonListField(page, groupName)
24+
// Keep standard radio behavior: only one selection in the group
25+
AllowUnisonSelection = false
26+
};
27+
// Define the 4 radio items: 2 on page 1, 2 on page 2
28+
var items = new (PdfPage page, string export, RectangleF bounds, string label)[]
29+
{
30+
(page1, "rb1", new RectangleF(100f, 200f, 20f, 20f), "Radio Button 1"),
31+
(page1, "rb2", new RectangleF(100f, 240f, 20f, 20f), "Radio Button 2"),
32+
(page2, "rb3", new RectangleF(100f, 200f, 20f, 20f), "Radio Button 3"),
33+
(page2, "rb4", new RectangleF(100f, 240f, 20f, 20f), "Radio Button 4"),
34+
};
35+
// Add items to the single radio field and draw their labels
36+
foreach (var (page, export, bounds, label) in items)
37+
{
38+
PdfRadioButtonListItem item = new PdfRadioButtonListItem(page, export)
2839
{
29-
// Keep selection behavior consistent
30-
AllowUnisonSelection = true
40+
Bounds = bounds
3141
};
32-
// Add items and draw labels via loop
33-
for (int i = 0; i < exportValues.Length; i++)
34-
{
35-
// Intialized bounds value
36-
RectangleF bounds = new RectangleF(100f, startYs[p] + (i * 40f), 20f, 20f);
37-
// Create item with consistent export value
38-
PdfRadioButtonListItem item = new PdfRadioButtonListItem(page, exportValues[i])
39-
{
40-
Bounds = bounds
41-
};
42-
radioField.Items.Add(item);
43-
// Draw label near the radio button
44-
page.Graphics.DrawString($"Radio Button {i + 1}", labelFont, PdfBrushes.Black, bounds.Right + labelOffsetX, bounds.Y + labelOffsetY);
45-
}
46-
// Set default selection on the first page
47-
if (p == 0 && exportValues.Length > 0)
48-
{
49-
radioField.SelectedValue = exportValues[0];
50-
}
51-
// Add the field to the form
52-
form.Fields.Add(radioField);
42+
radioField.Items.Add(item);
43+
// Label beside the radio button
44+
page.Graphics.DrawString(label, labelFont, PdfBrushes.Black,
45+
bounds.Right + labelOffsetX, bounds.Y + labelOffsetY);
5346
}
47+
// Set default selection
48+
radioField.SelectedValue = "rb4";
49+
// Add the single field to the form
50+
form.Fields.Add(radioField);
5451
// Save the PDF document
5552
document.Save(Path.GetFullPath(@"Output/Output.pdf"));
5653
}

0 commit comments

Comments
 (0)