Skip to content

Radio buttons do not retain values when filling template (when viewed in Adobe Acrobat) #3549

@melvsparks

Description

@melvsparks

I am trying to programmatically fill in a template.pdf that needs to have viewing support with Adobe Acrobat. The files look just fine in other PDF viewers.

I'm trying to do the following

  1. Set the "Policy number" text field - This works perfectly
  2. Retain the enabled "New Enrollment" radio button - (see attempts 1-3 in the comments below) The generated PDF always has a disabled Radio button
  3. Update the "Gender" radio button to Female - This value also doesn't show as enabled in the generated pdf

I looked at the underlying code for the PDFs and they appear to be all set correct (/V, /DV, /AS, etc).

Environment

Which environment were you using when you encountered the problem?
Opened the final pdf in windows and MacOS adobe acrobat

$ python -m platform
Windows-11-10.0.26100-SP0

$ python -c "import pypdf;print(pypdf._debug_versions)"
pypdf==6.4.0, crypt_provider=('local_crypt_fallback', '0.0.0'), PIL=11.3.0

$ python repro_issue.py
Created: repro_adobe_issue.pdf

Code + PDF

Template: TemplateReproBug.pdf
Output: repro_adobe_issue.pdf

This is a minimal, complete example that shows the issue:

# Filename: repro_issue.py
from pypdf import PdfReader, PdfWriter
from pypdf.generic import BooleanObject, NameObject

inp = "src/scripts/carriers/UHCAZ/TemplateReproBug.pdf"
out = "repro_adobe_issue.pdf"

reader = PdfReader(inp, strict=False)

if "/AcroForm" in reader.trailer["/Root"]:
    reader.trailer["/Root"]["/AcroForm"].update(
        {NameObject("/NeedAppearances"): BooleanObject(True)}
    )

writer = PdfWriter()
writer.append(reader)

field_dictionary = {
    "Policy Number": "ABC123456",  # Set Policy Number field
    "Gender 01": "/0",  # Gender - select first option (kid 0)
    # What doesn't work when trying to retain the New Enrollment radio button thats selected in the template 
    # 1. Leaving out 'Radio Button 3' entirely
    # 2. Setting 'Radio Button 3' to '/0' programatically: ```'Radio Button 3': '/0'```
    # 3. See below for manual setting of /V, /DV, and /AS
}


writer.update_page_form_field_values(writer.pages[0], field_dictionary)

# 3. Ensure proper /V, /DV, and /AS for radio button Adobe compatibility
fields = writer.pages[0].get("/Annots")
if fields:
    for field_ref in fields:
        field = field_ref.get_object()
        field_name = field.get("/T")

        if field_name == "Radio Button 3":
            # Set parent /V and /DV
            field.update(
                {
                    NameObject("/V"): NameObject("/0"),
                    NameObject("/DV"): NameObject("/0"),
                }
            )
            # Set child /AS for selected kid
            kids = field.get("/Kids")
            if kids:
                for kid_ref in kids:
                    kid = kid_ref.get_object()
                    kid.update({NameObject("/AS"): NameObject("/0")})
                    break  # Only set first kid to selected

        elif field_name == "Gender 01":
            # Set parent /V and /DV for gender (use /0 for first option)
            field.update(
                {
                    NameObject("/V"): NameObject("/0"),
                    NameObject("/DV"): NameObject("/0"),
                }
            )
            # Set child /AS for selected kid (kid 0 gets /0, others get /Off)
            kids = field.get("/Kids")
            if kids:
                for i, kid_ref in enumerate(kids):
                    kid = kid_ref.get_object()
                    if i == 0:  # First kid gets selected
                        kid.update({NameObject("/AS"): NameObject("/0")})
                    else:  # Other kids get Off
                        kid.update({NameObject("/AS"): NameObject("/Off")})

with open(out, "wb") as fp:
    writer.write(fp)

print(f"Created: {out}")

TemplateReproBug.pdf

Screenshots

TemplateReproBug.pdf

Image

repro_adobe_issue.pdf

The relevant fields for this post that are being updated are highlighted in red

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    is-bugFrom a users perspective, this is a bug - a violation of the expected behavior with a compliant PDFneeds-discussionThe PR/issue needs more discussion before we can continueworkflow-formsFrom a users perspective, forms is the affected feature/workflow

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions