From c0c5f936d14b2b5734afd4532b9dcbcc15a8e964 Mon Sep 17 00:00:00 2001 From: Steven Date: Sat, 16 Apr 2022 19:35:15 +0200 Subject: [PATCH] BUG: Set Linked Form Value (#414) 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 --- PyPDF2/pdf.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/PyPDF2/pdf.py b/PyPDF2/pdf.py index dcbb28447..29bedc28d 100644 --- a/PyPDF2/pdf.py +++ b/PyPDF2/pdf.py @@ -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. @@ -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): '''