Skip to content

Instantly share code, notes, and snippets.

@isaacgr
Created July 19, 2020 14:31
Show Gist options
  • Save isaacgr/1f6c4023a805b2d1b43b1eaa2f3895fe to your computer and use it in GitHub Desktop.
Save isaacgr/1f6c4023a805b2d1b43b1eaa2f3895fe to your computer and use it in GitHub Desktop.
A script to fillout/annotate a pdf form
from pdfrw import PdfReader, PdfWriter, PdfDict, PdfObject
ANNOT_KEY = '/Annots'
ANNOT_FIELD_KEY = '/T'
ANNOT_VAL_KEY = '/V'
ANNOT_RECT_KEY = '/Rect'
SUBTYPE_KEY = '/Subtype'
WIDGET_SUBTYPE_KEY = '/Widget'
def main():
pdf = PdfReader('qpdf.pdf')
pdf.Root.AcroForm.update(PdfDict(NeedAppearances=PdfObject('true')))
pages = pdf.pages
keys = []
for page in range(len(pages)):
annotations = pdf.pages[page]['/Annots']
for annotation in annotations:
if annotation[SUBTYPE_KEY] == WIDGET_SUBTYPE_KEY:
if annotation[ANNOT_FIELD_KEY]:
key = annotation[ANNOT_FIELD_KEY][1:-1]
keys.append(key)
#annotation.update(PdfDict(AP='{}'.format('69'), V='{}'.format('69')))
PdfWriter().write('test.pdf', pdf)
with open('fields.txt', 'w') as f:
for key in keys:
f.write(key)
f.write('\n')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment