Skip to content

Commit

Permalink
BUG: Set Linked Form Value (#414)
Browse files Browse the repository at this point in the history
If a form text field is linked to multiple locations in a PDF, PdfFileWriter.updatePageFormFieldValues failed to set the value.

If writer_annot contains a link to a Parent object, the value in that should object should be set.

Closes #398
  • Loading branch information
malvidin authored Apr 16, 2022
1 parent 9acebab commit c0c5f93
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions PyPDF2/pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ def updatePageFormFieldValues(self, page, fields):
'''
Update the form field values for a given page from a fields dictionary.
Copy field texts and values from fields to page.
If the field links to a parent object, add the information to the parent.
:param page: Page reference from PDF writer where the annotations
and field data will be updated.
Expand All @@ -372,11 +373,19 @@ def updatePageFormFieldValues(self, page, fields):
# Iterate through pages, update field values
for j in range(0, len(page[PG.ANNOTS])):
writer_annot = page[PG.ANNOTS][j].getObject()
# retrieve parent field values, if present
writer_parent_annot = {} # fallback if it's not there
if PG.PARENT in writer_annot:
writer_parent_annot = writer_annot[PG.PARENT]
for field in fields:
if writer_annot.get('/T') == field:
writer_annot.update({
NameObject("/V"): TextStringObject(fields[field])
})
elif writer_parent_annot.get('/T') == field:
writer_parent_annot.update({
NameObject("/V"): TextStringObject(fields[field])
})

def cloneReaderDocumentRoot(self, reader):
'''
Expand Down

0 comments on commit c0c5f93

Please sign in to comment.