Skip to content

Commit

Permalink
ENH: Allow setting form field flags (#802)
Browse files Browse the repository at this point in the history
Closes #574
Closes #801

Co-authored-by: Craig Jones <craig@k6nnl.com>
  • Loading branch information
MartinThoma and polyglot-jones committed Apr 23, 2022
1 parent 668869f commit ffb2084
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
7 changes: 6 additions & 1 deletion PyPDF2/pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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])):
Expand All @@ -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])
Expand Down
2 changes: 1 addition & 1 deletion Tests/test_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit ffb2084

Please sign in to comment.