PyPDF2 icon indicating copy to clipboard operation
PyPDF2 copied to clipboard

Filling PDF Fields doe not work if fields include RadioButtons

Open rupeshbhurke opened this issue 2 years ago • 0 comments

Hi! I am using following code to fill a Fillable PDF:

from PyPDF2 import PdfReader, PdfWriter
from PyPDF2.generic import BooleanObject, NameObject

inp = r"C:\Temp\fill_form\Input.pdf"
out = r"C:\Temp\fill_form\Output.pdf"

reader = PdfReader(inp, strict=False)

if "/AcroForm" in reader.trailer["/Root"]:
    reader.trailer["/Root"]["/AcroForm"].update(
        {NameObject("/NeedAppearances"): BooleanObject(True)}
    )

writer = PdfWriter()
writer.append(reader)
field_dictionary = {
    'Line1_MiddleName[0]': 'MiddleName',
    'Line1_GivenName[0]': 'FirstName',
    'Line1_FamilyName[0]': 'LastName',
    'Line3_Unit[0]': '/ Off ',
    'Line3_Unit[1]': '/ APT ',
    'Line3_Unit[2]': '/ Off ',
    'Line9_EmailAddress[0]': '[email protected]',
    'Line3_TaxNumber[0]': '12345',
    'TextField1[0]': "12-3456789",
}
writer.update_page_form_field_values(writer.pages[0], field_dictionary)
with open(out, "wb") as fp:
    writer.write(fp)

Please note that first 3 fields are text fields, next 3 fields are RadioButtons and rest are test fields. If I remove RadioButtons it works well and all the values can be seen in PDF file using Adobe Acrobat Reader. But the moment RadioButtons are added all the values after the RadioButtons dont show unless they are clicked upon.

Kindly let me know what is wrong here.

Thank you.

Input.pdf

rupeshbhurke avatar Sep 27 '23 05:09 rupeshbhurke