首页 > 解决方案 > 复选框是否默认在 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')

标签: pdfcheckboxdefault

解决方案


没那么简单。从PDF规范...

字段字典中的 V 条目(见表 220)包含一个表示复选框外观状态的名称对象,该对象应用于从外观字典中选择适当的外观。

在这种情况下,V 条目与值无关,它用于设置小部件在页面上的外观。

然后...

从 PDF 1.4 开始,复选框和单选按钮的字段字典可能包含可选的 Optentry(参见表 227)。如果存在,Opt 条目应该是一个文本字符串数组,表示字段中每个注释的 导出值

您可能需要查看值的 Opt 条目,并且该值可能不是“是”,但未选中将始终为“关闭”。


推荐阅读