From ffb208478144c2dc8f7ee9a4038f2c1c85ac99df Mon Sep 17 00:00:00 2001 From: Martin Thoma Date: Sat, 23 Apr 2022 10:17:57 +0200 Subject: [PATCH] ENH: Allow setting form field flags (#802) Closes #574 Closes #801 Co-authored-by: Craig Jones --- PyPDF2/pdf.py | 7 ++++++- Tests/test_writer.py | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/PyPDF2/pdf.py b/PyPDF2/pdf.py index 53ea01bd5..38271fb2a 100644 --- a/PyPDF2/pdf.py +++ b/PyPDF2/pdf.py @@ -371,7 +371,7 @@ def appendPagesFromReader(self, reader, after_page_append=None): # Trigger callback, pass writer page as parameter if callable(after_page_append): after_page_append(writer_page) - def updatePageFormFieldValues(self, page, fields): + def updatePageFormFieldValues(self, page, fields, flags=0): ''' Update the form field values for a given page from a fields dictionary. Copy field texts and values from fields to page. @@ -381,6 +381,9 @@ def updatePageFormFieldValues(self, page, fields): and field data will be updated. :param fields: a Python dictionary of field names (/T) and text values (/V) + :param flags: An integer (0 to 7). The first bit sets ReadOnly, the + second bit sets Required, the third bit sets NoExport. See + PDF Reference Table 8.70 for details. ''' # Iterate through pages, update field values for j in range(0, len(page[PG.ANNOTS])): @@ -394,6 +397,8 @@ def updatePageFormFieldValues(self, page, fields): writer_annot.update({ NameObject("/V"): TextStringObject(fields[field]) }) + if flags: + writer_annot.update({NameObject("/Ff"): NumberObject(flags)}) elif writer_parent_annot.get('/T') == field: writer_parent_annot.update({ NameObject("/V"): TextStringObject(fields[field]) diff --git a/Tests/test_writer.py b/Tests/test_writer.py index 22d35a6da..5831fe08a 100644 --- a/Tests/test_writer.py +++ b/Tests/test_writer.py @@ -166,7 +166,7 @@ def test_fill_form(): writer.addPage(page) - writer.updatePageFormFieldValues(writer.getPage(0), {"foo": "some filled in text"}) + writer.updatePageFormFieldValues(writer.getPage(0), {"foo": "some filled in text"}, flags=1) # write "output" to PyPDF2-output.pdf tmp_filename = "dont_commit_filled_pdf.pdf"