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

Pdf Form labels/values #165

Open
typhoon71 opened this issue Apr 14, 2019 · 1 comment
Open

Pdf Form labels/values #165

typhoon71 opened this issue Apr 14, 2019 · 1 comment

Comments

@typhoon71
Copy link

I'm trying to read the labels and values from some pdf fillable form. I used:

x =pdfrw.PdfReader(path)

This gave me a dict with a ['/Root']['/AcroForm']['/Fields'] structure inside, but I can't find the form values I need.

[ pdfminer gives a similar structure, but has a resolver that takes care of getting the labels/values out of that dict, but I couldn't find any for pdfrw ]

Using PyPDF2 I could do:

x = PyPDF2.PdfFileReader(path)
d = x.getFields() 

and I would get fields/values of the form.

Is this possible with pdfrw? I couldn't find anything in the examples so I'm asking it here.
If it's possible it would be nice to have an example for this too. (please, thanks)

@gbroiles
Copy link

Try this:

import pdfrw

ANNOT_KEY = "/Annots"
ANNOT_FIELD_KEY = "/T"
ANNOT_VAL_KEY = "/V"
SUBTYPE_KEY = "/Subtype"
WIDGET_SUBTYPE_KEY = "/Widget"

PDF_NAME = "test.pdf"

template_pdf = pdfrw.PdfReader(PDF_NAME)
for page in range(0, len(template_pdf.pages)):
    annotations = template_pdf.pages[page][ANNOT_KEY]
    for annotation in annotations:
        if annotation[SUBTYPE_KEY] == WIDGET_SUBTYPE_KEY:
            if annotation[ANNOT_FIELD_KEY]:
                name = annotation[ANNOT_FIELD_KEY]
                print("{} ".format(name), end="")
                if annotation[ANNOT_VAL_KEY]:
                    value = annotation[ANNOT_VAL_KEY]
                    print("= {}".format(value))
                else:
                    print()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants