Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pypdf/_doc_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@ def _build_field(
fileobj.write("\n")
retval[key] = Field(field)
obj = retval[key].indirect_reference.get_object() # to get the full object
if obj.get(FA.FT, "") == "/Ch":
if obj.get(FA.FT, "") == "/Ch" and obj.get(NameObject(FA.Opt)):
retval[key][NameObject("/_States_")] = obj[NameObject(FA.Opt)]
if obj.get(FA.FT, "") == "/Btn" and "/AP" in obj:
# Checkbox
Expand Down
17 changes: 16 additions & 1 deletion tests/test_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ def test_get_page_of_encrypted_file(pdffile, password, should_fail):
"crazyones.pdf",
{},
None,
),
)
],
)
def test_get_form(src, expected, expected_get_fields, txt_file_path):
Expand Down Expand Up @@ -448,6 +448,21 @@ def test_get_form(src, expected, expected_get_fields, txt_file_path):
]


@pytest.mark.enable_socket
def test_reading_choice_field_without_opt_key():
"""Tests reading a choice field in a PDF without an /Opt key."""
url = "https://github.com/user-attachments/files/23853677/Musterservicevertrag-HNRAGB_Okt2022-Blanko.pdf"
reader = PdfReader(BytesIO(get_data_from_url(url, name="Musterservicevertrag-HNRAGB_Okt2022-Blanko.pdf")))
fields = reader.get_fields()

tn_anrede = fields.get("TN_Anrede")
assert tn_anrede is not None

# Ensure that parsing of a choice field without /Opt key worked
tn_anrede_opt = tn_anrede.get("/Opt")
assert tn_anrede_opt is None


@pytest.mark.parametrize(
("src", "page_number"),
[
Expand Down
Loading