Building a Radio Group From Nothing — the One Argument That Decides Whether add_widget Survives
PyMuPDF's add_widget can build a full radio group by itself — with one condition: w.field_value = False must be set before the call. This is the first time in the series the variable is isolated inside one environment: same process, same page construction, three kids each way. Without that assignment: 3/3 ValueError: bad xref. With it: 3/3 widgets added and saved. Two more verbatim observations round out the page: the auto-generated on-state for every created kid is 'Yes', so a three-option group is not automatically A/B/C; and after picking one kid, PyMuPDF re-read shows [('planA','Yes'),('planA','Off'),('planA','Off')] while pypdf's cross-read of the same file gives {'planA': 'Off'} — the kid-level visibility gap from Day 15, unchanged in version 1.28.2.
How the isolation happened
Installed with pip install pymupdf pypdf (no pins). Versions printed by the runs themselves: pypdf 6.20.0 | PyMuPDF 1.28.2. The first attempt of this session added three radio widgets the plain way and logged bad xref three times — yet the saved file still contained three RadioButton widgets with states: None. Because Day 16's single-widget success had set field_value = False before add_widget and today's plain attempt did not, we ran the two forms back to back in this same environment with nothing else changed. That contrast is listed verbatim below, and where we write "decides", the write-up follows our own rule from Day 16: the only variable that moved is the assignment; everything else is the same process.
The two runs, side by side
| Setting | Plain add (no field_value before add) | With w.field_value = False before add |
|---|---|---|
| Kid add results | kid0 FAIL: ValueError: bad xref / kid1 FAIL / kid2 FAIL — 3/3 | kid0 OK / kid1 OK / kid2 OK — 3/3 |
| Saved-file widgets | 3 × ('plan', 'RadioButton') present despite the exceptions | 3 × ('planA', 'RadioButton') |
| Per-kid on-state | states: None | states: {'normal': ['Off', 'Yes'], 'down': None} ×3 |
After the created group was filled: selected 'Yes' on planA; re-read gave [('planA','Yes'),('planA','Off'),('planA','Off')], and pypdf reading the same file printed {'planA': 'Off'}. Nothing was attributed here — it is the same parent-/Off-with-kid-state shape Day 15 and Day 16 already recorded, now seen on a group PyMuPDF built itself.
The exact calls, with their outputs inline
import pypdf, pymupdf
print("RUN VERSIONS pypdf:", pypdf.__version__, "| pymupdf:", pymupdf.__version__)
# ran: RUN VERSIONS pypdf: 6.20.0 | pymupdf: 1.28.2
doc = pymupdf.open()
page = doc.new_page(width=612, height=400)
page.insert_text((72,100), "Radio Create Variant A", fontsize=16)
resA = []
for i, y in enumerate([130,170,210]):
w = pymupdf.Widget()
w.rect = pymupdf.Rect(72,y,92,y+20)
w.field_type = pymupdf.PDF_WIDGET_TYPE_RADIOBUTTON
w.field_name = "planA"
w.field_value = False # the line that flips 3/3 crash to 3/3 OK in this environment
page.add_widget(w)
resA.append(f"kid{i} OK")
doc.save("radio17_A.pdf")
# filling the created group
doc = pymupdf.open("radio17_A.pdf")
for wd in doc[0].widgets():
st = (wd.button_states() or {}).get("normal") or []
non_off = [s for s in st if s != "Off"]
if non_off:
wd.field_value = non_off[0] # non_off == ['Yes']
wd.update()
break
doc.save("radio17_A_filled.pdf")
print([(w.field_name, w.field_value) for w in pymupdf.open("radio17_A_filled.pdf")[0].widgets()])
# ran: [('planA', 'Yes'), ('planA', 'Off'), ('planA', 'Off')]
print(PdfReader("radio17_A_filled.pdf").get_fields())
# ran (pypdf): {'planA': 'Off'}
What this adds to the series' running record
Day 15 logged widget creation as impossible (3/3 bad xref, environment since wiped), Day 16 found one creation succeed after a reinstall, and today pinpoints the code-level difference that separates the two outcomes — verified inside a single process. The cross-library visibility gap, on the other hand, has now been observed on three different fixtures (Day 15 hand-built, Day 16 single widget, Day 17 self-created group) across version movement, and we record it as a repeated observation without a stated mechanism.
Left unrun, with the reason attached
- Custom per-kid export values (A/B/C) on built widgets — not run; the created group auto-named every on-state
'Yes', and we did not explore widget-attribute rewriting to produce distinct export names. - Appearance-stream validity of created radio widgets — not run; rendering the built file visually requires tooling outside this run, so the
'Yes'state was verified by value reads only. - Grouping semantics check across pages (kids on different pages under one parent) — not run; today's group lives on one page.
- pdf.js / browser-side read of the filled created group — not run; verification again stopped at pypdf + PyMuPDF.
- XFA forms — carried unchanged from Day 14: no offline XFA fixture.
Quoted lines come from this session's saved logs (day17_create_log.txt, day17_variant_log.txt, day17_fill_log.txt); versions were printed inside each log before any work began.