How to Check Boxes in a PDF with fdfgen in Python

≡放荡痞女 提交于 2019-12-08 05:49:01

问题


I'm working on a Python 3.4 project that uses fdfgen and pdftk to fill out a pdf. I can fill in text fields pretty easily. The thing I cannot figure out is how to check a box.

Here's the part of my python script:

        fields = generate_vs300_field_list(answers_dict) # Returns list of tuples of fields and text to be added to pdf
        fdf = forge_fdf("", fields, [], [], [])
        logging.debug('Called forge_fdf function. About to open fdf_file.')
        fdf_file = open(os.path.join(destination_dir, 'vs300.fdf'), 'wb')
        logging.debug('Opened fdf_file. About to write to it.')
        fdf_file.write(fdf)
        fdf_file.close()
        logging.debug('Wrote to fdf_file and closed it.')
        cmd = 'pdftk "{0}" fill_form "{1}" output "{2}" dont_ask'.format(
            './styles/vs300.pdf',
            os.path.join(destination_dir, 'vs300.fdf'),
            os.path.join(destination_dir, 'vs300.pdf')
        )
        logging.debug('About to send this command to pdftk: ' + cmd)
        os.system(cmd)
        logging.debug('Command sent. Removing temporary fdf file.')
        os.remove(os.path.join(destination_dir, 'vs300.fdf'))

I can successfully create a list of tuples to be fed to forge_fdf, with the first item in the tuple being the field name, and the second the text for the field I want to add.

What I cannot figure out how to do is to check a box. If I run pdftk dump_data_fields on my pdf, here's the entry for the checkbox I'm trying to check:

FieldType: Button
FieldName: Absolute Divorce
FieldNameAlt: Absolute Divorce
FieldFlags: 0
FieldJustification: Left
FieldStateOption: Off
FieldStateOption: On

I'm not sure how to a) change the fdf file so that it makes the checkbox show up as having been checked, and b), how to actually do that change using fdfgen.


回答1:


Check box fields through fdfgen go through the fdf_data_names variable and not fdf_data_strings. You should also check the PDF itself to see what the checkbox uses for its "on" trigger.

You call to forge_fdf would then look like:

forge_fdf("", fields, data_names)


来源:https://stackoverflow.com/questions/36140392/how-to-check-boxes-in-a-pdf-with-fdfgen-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!