Skip to content

Commit

Permalink
ENH: Allow to update fields on many pages (#2571)
Browse files Browse the repository at this point in the history
  • Loading branch information
pubpub-zz authored Apr 2, 2024
1 parent 4d3d797 commit 4bdca16
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
14 changes: 12 additions & 2 deletions pypdf/_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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
Expand Down
17 changes: 11 additions & 6 deletions tests/test_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit 4bdca16

Please sign in to comment.