From 605366392f529c31f8641f313714aaba1df7718b Mon Sep 17 00:00:00 2001 From: pubpub-zz <4083478+pubpub-zz@users.noreply.github.com> Date: Tue, 2 Jul 2024 22:49:49 +0200 Subject: [PATCH] FIX: Cope with Matrix entry in field annotations closes #2731 --- pypdf/_writer.py | 4 ++++ tests/test_writer.py | 14 ++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/pypdf/_writer.py b/pypdf/_writer.py index 524828c05..00b9d498c 100644 --- a/pypdf/_writer.py +++ b/pypdf/_writer.py @@ -906,6 +906,10 @@ def _update_field_annotation( "/Length": 0, } ) + if AA.AP in anno: + for k, v in cast(DictionaryObject, anno[AA.AP]).get("/N", {}).items(): + if k not in {"/BBox", "/Length", "/Subtype", "/Type", "/Filter"}: + dct[k] = v # Update Resources with font information if necessary if font_res is not None: diff --git a/tests/test_writer.py b/tests/test_writer.py index 5aff3ce07..9dfeffdd8 100644 --- a/tests/test_writer.py +++ b/tests/test_writer.py @@ -2286,3 +2286,17 @@ def test_field_box_upside_down(): box = writer.pages[0]["/Annots"][13].get_object()["/AP"]["/N"]["/BBox"] assert box[2] > 0 assert box[3] > 0 + + +@pytest.mark.enable_socket() +def test_matrix_entry_in_field_annots(): + """Cf #2731""" + url = "https://github.com/user-attachments/files/16036514/template.pdf" + name = "iss2731.pdf" + writer = PdfWriter(BytesIO(get_data_from_url(url, name=name))) + writer.update_page_form_field_values( + writer.pages[0], + {"Stellenbezeichnung_1": "some filled in text"}, + auto_regenerate=False, + ) + assert "/Matrix" in writer.pages[0]["/Annots"][5].get_object()["/AP"]["/N"]