Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Allow setting field flags for forms #802

Merged
merged 1 commit into from
Apr 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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