如何在 Python 中使用 fdfgen 检查 PDF 中的框 [英] How to Check Boxes in a PDF with fdfgen in Python

查看:56
本文介绍了如何在 Python 中使用 fdfgen 检查 PDF 中的框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个使用 fdfgen 和 pdftk 来填写 pdf 的 Python 3.4 项目.我可以很容易地填写文本字段.我无法弄清楚的是如何选中一个框.

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.

这是我的python脚本的一部分:

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'))

我可以成功地创建一个要提供给 forge_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.

我无法弄清楚如何做的是选中一个框.如果我在我的 pdf 上运行 pdftk dump_data_fields,这是我要检查的复选框的条目:

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

我不知道如何 a) 更改 fdf 文件,以便使复选框显示为已选中,以及 b) 如何使用 fdfgen 进行实际更改.

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.

推荐答案

通过 fdfgen 的复选框字段通过 fdf_data_names 变量而不是 fdf_data_strings.您还应该检查 PDF 本身以查看复选框用于其开启"触发器的内容.

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.

您对 forge_fdf 的调用将如下所示:

You call to forge_fdf would then look like:

forge_fdf("", fields, data_names)

这篇关于如何在 Python 中使用 fdfgen 检查 PDF 中的框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆