From 4bdca1628606706f652f02c260c2e7dd53fb2ff7 Mon Sep 17 00:00:00 2001 From: pubpub-zz <4083478+pubpub-zz@users.noreply.github.com> Date: Tue, 2 Apr 2024 18:43:49 +0200 Subject: [PATCH] ENH: Allow to update fields on many pages (#2571) --- pypdf/_writer.py | 14 ++++++++++++-- tests/test_writer.py | 17 +++++++++++------ 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/pypdf/_writer.py b/pypdf/_writer.py index 88e280ee5..06f38b03b 100644 --- a/pypdf/_writer.py +++ b/pypdf/_writer.py @@ -896,7 +896,7 @@ def _update_field_annotation( def update_page_form_field_values( self, - page: PageObject, + page: Union[PageObject, List[PageObject], None], fields: Dict[str, Any], flags: FieldFlag = OPTIONAL_READ_WRITE_FIELD, auto_regenerate: Optional[bool] = True, @@ -908,8 +908,10 @@ def update_page_form_field_values( If the field links to a parent object, add the information to the parent. Args: - page: Page reference from PDF writer where the + page: `PageObject` - references **PDF writer's page** where the annotations and field data will be updated. + `List[Pageobject]` - provides list of page to be processsed. + `None` - all pages. fields: a Python dictionary of field names (/T) and text values (/V). flags: An integer (0 to 7). The first bit sets ReadOnly, the @@ -925,6 +927,14 @@ def update_page_form_field_values( raise PyPdfError("No /Fields dictionary in Pdf in PdfWriter Object") if isinstance(auto_regenerate, bool): self.set_need_appearances_writer(auto_regenerate) + # Iterate through pages, update field values + if page is None: + page = list(self.pages) + if isinstance(page, list): + for p in page: + if PG.ANNOTS in p: # just to prevent warnings + self.update_page_form_field_values(p, fields, flags, None) + return None if PG.ANNOTS not in page: logger_warning("No fields to update on this page", __name__) return diff --git a/tests/test_writer.py b/tests/test_writer.py index baf0134e2..6ff45c3eb 100644 --- a/tests/test_writer.py +++ b/tests/test_writer.py @@ -1504,14 +1504,20 @@ def test_update_form_fields(tmp_path): page_number=0, annotation=Link(target_page_index=1, rect=RectangleObject([0, 0, 100, 100])), ) + writer.insert_blank_page(100, 100, 0) del writer.root_object["/AcroForm"]["/Fields"][1].get_object()["/DA"] del writer.root_object["/AcroForm"]["/Fields"][1].get_object()["/DR"]["/Font"] writer.update_page_form_field_values( - writer.pages[0], + [writer.pages[0], writer.pages[1]], + {"Text1": "my Text1", "Text2": "ligne1\nligne2\nligne3"}, + auto_regenerate=False, + ) + assert b"/Helv " in writer.pages[1]["/Annots"][1]["/AP"]["/N"].get_data() + writer.update_page_form_field_values( + None, {"Text1": "my Text1", "Text2": "ligne1\nligne2\nligne3"}, auto_regenerate=False, ) - assert b"/Helv " in writer.pages[0]["/Annots"][1]["/AP"]["/N"].get_data() Path(write_data_here).unlink() @@ -1568,10 +1574,9 @@ def test_update_form_fields2(): reader.add_form_topname(file) writer = PdfWriter(clone_from=reader) - for page in writer.pages: - writer.update_page_form_field_values( - page, myFiles[file]["usage"]["fields"], auto_regenerate=True - ) + writer.update_page_form_field_values( + None, myFiles[file]["usage"]["fields"], auto_regenerate=True + ) merger.append(writer) assert merger.get_form_text_fields(True) == { "test1.First Name": "Reed",