I am trying to fill the empty fields of pdf file, but I am facing 2 issues with the code, i.e.,
- It is replacing the field values even which are not empty (i.e. it needs to fill ‘NA’ for only empty fields)
- The ‘NA’ for empty fields is working only in the first page for remaining pages it’s not working
I tried the below code –
`import pdfrw
def fill_pdf_with_na(input_path, output_path):
template_pdf = pdfrw.PdfReader(input_path)
for page_num in range(len(template_pdf.pages)):
annotations = template_pdf.pages[page_num]['/Annots']
if annotations is None:
continue
for annotation in annotations:
if annotation['/Subtype'] == '/Widget':
if annotation['/FT'] == '/Tx': # Text field
if '/V' not in annotation:
annotation.update(pdfrw.PdfDict(V="NA"))
elif annotation['/FT'] == '/Btn': # Button (checkbox)
if '/V' not in annotation:
annotation.update(pdfrw.PdfDict(V=pdfrw.PdfName('Yes')))
# Write the modified PDF to the output file
writer = pdfrw.PdfWriter()
writer.write(output_path, template_pdf)`
input_pdf_path = r”C:fillpdfinput.pdf”
output_pdf_path = r”C:fillpdfoutput.pdf”
fill_pdf_with_na(input_pdf_path, output_pdf_path)
print(f”Filled PDF saved to {output_pdf_path}”)
pls help me to solve my 2 issues