方法 getKids() 对于 PDField 类型未定义 [英] The method getKids() is undefined for the type PDField

查看:77
本文介绍了方法 getKids() 对于 PDField 类型未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

https://issues.apache.org/jira/browse/PDFBOX-2148

当有多个具有相同字段名称的副本时,PDField 对象列表中每个孩子的 getFullyQualifiedName 返回父级的名称,后跟 .null.因此,如果父字段被称为 Button2 并且它有 4 个实例,则打印出所有名称的结果将是:

Button2.nullButton2.nullButton2.nullButton2.null

解决方案

根据问题的评论,OP 指的是 PDFBox 2.0.x 版本,尤其是 2.0.6.>

getKids()

<块引用>

方法 getKids() 未定义类型 PDField

在 PDFBox 2.0.6 中,有两个 PDField 的直接子类.前者 (1.8.x) getKids() 方法的不同变体在那里实现:

  • PDNonTerminalField - 在这个类中检索孩子的方法是 getChildren() 并返回一个 List,一个表单列表字段.
  • PDTerminalField - 在这个类中检索孩子的方法是 getWidgets 并返回一个 List,一个小部件注释列表.

父的名称,后跟.null

<块引用>

当有多个具有相同字段名称的副本时,PDField 对象列表中每个孩子的 getFullyQualifiedName 返回父级的名称,后跟 .null

PDFBox 2.0.x 不是这种情况.

在附于 PDFBox 问题的示例文档中,PDFBOX-2148 PDFBox 现在只能正确找到一个名为Button2"的字段.这个字段是一个 PDTerminalField 并且有 4 个小部件注释.后者的类 PDAnnotationWidget 没有 getFullyQualifiedName 方法,因此没有.null".名字.

这样,这个问题就解决了.

重复字段的 FQN

(来自 OP 对 你的问题究竟是什么?"的评论)

<块引用>

如何获取pdfbox中重复字段的完全限定名称

(有效的)PDF 中没有重复的字段,对于给定的名称,最多只有一个字段可以有多个小部件.小部件没有单独的 FQN.

因此,您所说的重复字段"将被删除.在您的示例文档中,实际上是一个 single 字段,其中包含 多个小部件;该字段的名称是Button2";并且可以使用 getFullyQualifiedName() 检索.

哪个页面哪个表单域

(来自 OP 对此答案的评论)

<块引用>

但是如何在 pdfbox 中获取当前页面编号 .. 例如有 3 页,在第 2 页有一个表单字段,那么我如何获取哪个页面的表单字段?

所有PDAnnotation 类,其中包括PDAnnotationWidget,都有一个返回PDPage 实例的getPage() 方法.

但是:根据 ISO 32000-1 中的规定,注释(特别是表单域小部件)不需要具有指向绘制它们的页面的链接(与再现操作相关的屏幕注释除外).

因此,上面提到的方法 getPage() 可能会返回 null(可能更常见).

因此,要确定小部件的相应页面,您必须以另一种方式解决问题:迭代所有页面并在相应的注释数组中查找注释小部件.

对于 PDFBox 1.8.x,您可以在此 stackoverflow 答案中找到示例代码 .根据本答案前面部分提供的信息,将代码移植到 PDFBox 2.0.x 应该很容易.

复选框和单选按钮

(也来自 OP 对此答案的评论)

<块引用>

还有一个问题,如果我同时使用复选框和单选按钮,那么 field.getFieldType() 输出都是 Btn.如何识别?

您可以通过检查通过 fields.getFieldFlags() 检索的字段标志来识别它们:

  • 如果设置了 Pushbutton 标志 (PDButton.FLAG_PUSHBUTTON),则该字段是一个常规的按钮.
  • 否则,如果设置了 Radio 标志 (FLAG_RADIO),则该字段是一个单选按钮.
  • 否则,该字段是一个复选框.

或者,您可以检查 field 对象的类,对于 Btn 可能是 PDPushButtonPDRadioButton,或 PDCheckBox.

注意:如果复选框字段有多个具有不同名称​​on 状态的小部件,此复选框字段及其小部件就像一个单选按钮组 不仅在理论上,我还看到过带有此类复选框字段的 PDF.

要真正确定字段的行为,您还应该比较给定复选框的所有小部件的开启状态名称.

https://issues.apache.org/jira/browse/PDFBOX-2148

When there are multiple copies with the same field name, the getFullyQualifiedName for each kid in the list of PDField objects returns the name of the parent, followed by .null. So if the parent field is called Button2 and it has 4 instances the result of printing out all the names will be:

Button2.null
Button2.null
Button2.null
Button2.null

解决方案

According to the comments to the question, the OP refers to PDFBox 2.0.x versions, in particular 2.0.6.

getKids()

The method getKids() is undefined for the type PDField

In PDFBox 2.0.6 there are two immediate sub-classes of PDField. Different variants of the former (1.8.x) getKids() method are implemented in there:

  • PDNonTerminalField - the method retrieving the kids in this class is getChildren() and returns a List<PDField>, a list of form fields.
  • PDTerminalField - the method retrieving the kids in this class is getWidgets and returns a List<PDAnnotationWidget>, a list of widget annotations.

name of the parent, followed by .null

When there are multiple copies with the same field name, the getFullyQualifiedName for each kid in the list of PDField objects returns the name of the parent, followed by .null

This is not the case in PDFBox 2.0.x.

In the sample document attached to the PDFBox issue PDFBOX-2148 PDFBox now correctly finds only a single field which appropriately is named "Button2". This field is a PDTerminalField and has 4 widget annotations. The class of the latter, PDAnnotationWidget, has no getFullyQualifiedName method, so there are no ".null" names.

Thus, this problem is gone.

FQN of duplicate fields

(from the OP's comment responding to "What exactly is your question?")

how to get Fully Qualified Name of duplicate fields in pdfbox

There are no duplicate fields in (valid) PDFs, for a given name there is at most a single field which may have multiple widgets. Widgets do not have individual FQNs.

Thus, what you call "duplicate fields" in your example document actually is a single field with multiple widgets; the name of that field is "Button2" and can be retrieved using getFullyQualifiedName().

which page which form field

(from the OP's comments to this answer)

but how to get current page no in pdfbox.. for example there are 3 page and in page 2 there is a form field so how can i get which page which form field ?

All PDAnnotation classes, among them PDAnnotationWidget, have a getPage() method returning a PDPage instance.

BUT: As specified in ISO 32000-1, annotations (in particular form field widgets) are not required to have a link to the page on which they are drawn (except for screen annotations associated with rendition actions).

Thus, the above mentioned method getPage() may return null (probably more often than not).

So to determine the respective pages of your widgets, you have to approach the problem the other way around: Iterate over all pages and look for the annotation widgets in the respective annotation array.

For PDFBox 1.8.x you can find example code in this stackoverflow answer. With the information given in the previous parts of this answer it should be easy to port the code to PDFBox 2.0.x.

checkbox and radio button

(also from the OP's comments to this answer)

one more issue if i am using checkbox and radio button both then field.getFieldType() output is Btn for both. how to identify it?

You can identify them by inspecting the field flags which you retrieve via fields.getFieldFlags():

  • If the Pushbutton flag is set (PDButton.FLAG_PUSHBUTTON), the field is a regular push button.
  • Otherwise, if the Radio flag is set (FLAG_RADIO), the field is a radio button.
  • Otherwise, the field is a check box.

Alternatively you can check the class of the field object which for Btn may be PDPushButton, PDRadioButton, or PDCheckBox.

Beware: If a check box field has multiple widgets with differently named on states, this check box field and its widgets act like a radio button group! And not only in theory, I've seen PDFs with such check box fields in the wild.

To really be sure concerning the behavior of the fields, you therefore also should compare the names of the on states of all the widgets of a given check box.

这篇关于方法 getKids() 对于 PDField 类型未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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