pdf - 复选框是否默认在 PDF 中未选中
问题描述
[编辑感谢 joelgeraci 帮助理解字段字典中的 /V 和 /Opt 键代表什么]
我想使用 Python 提取 PDF 中复选框的值。我有一些这样做的代码,它似乎工作正常并为每个复选框返回字段字典。
我一直在看PDF 标准。
在什么情况下可以说没有选中 PDF 复选框?就我而言,我有复选框,其中字段字典中没有所有 /V、/DV 和 /Opt。这些是否被解释为“未检查”?字段字典中是否有一个键只跟踪框中是否应该有一个勾号?
我很想参考一些规范文档或文档,明确地说,“这里是复选框应该被解释为选中或未选中的情况......”
非常感谢你的帮助!
下面是一些代码,说明了我目前对 PDF 中复选框的实现的理解,您可以用它来发现我思维中的错误:
def parse_checkbox(checkbox_dict: dict) -> tuple:
'''
Take a python dictionary representing the field dictionary
of a PDF checkbox and say what the checkbox represents and
whether it has been ticked.
Parameters:
checkbox_dict: a python dictionary representing the
field dictionary of a PDF checkbox
Returns:
A tuple indicating what the checkbox represents and
whether it was ticked
'''
def field_attribute(x):
return checkbox_dict[x] if x in checkbox_dict else None
opt, title, value, default = [field_attribute(a) for a in ['/Opt', '/T', '/V', '/DV']]
assert opt is not None or title is not None
name = opt if opt is not None else title
if (value == '/Off' or \
value is None and default == '/Off' or \
value is None and default is None):
return (name, 'unchecked')
else:
return (name, 'checked')
解决方案
没那么简单。从PDF规范...
字段字典中的 V 条目(见表 220)包含一个表示复选框外观状态的名称对象,该对象应用于从外观字典中选择适当的外观。
在这种情况下,V 条目与值无关,它用于设置小部件在页面上的外观。
然后...
从 PDF 1.4 开始,复选框和单选按钮的字段字典可能包含可选的 Optentry(参见表 227)。如果存在,Opt 条目应该是一个文本字符串数组,表示字段中每个注释的 导出值。
您可能需要查看值的 Opt 条目,并且该值可能不是“是”,但未选中将始终为“关闭”。
推荐阅读
- python - 通过 matplotlib 图表和图表上的绘图点绘制 python pandas 数据框
- reactjs - 条件封闭元素 React
- java - 如何在多对多关系中保留额外的列(属性)?
- here-api - 如何请求带标题的导航?
- android-studio - 如何在android中创建不同的语音效果?
- python - 如何在 django 上设置 timezone.now
- sql - 我已经在模型中定义了关系,但是如何以 Eloquent 的方式编写复杂的查询
- reactjs - 向 img 标签添加唯一 id 属性
- python - 如何将我们的自定义代码(将 json 转换为 csv)用于 Google 云数据流?
- python - 使用 API 获取和传输数据