iText 7.0.0
有没有办法在 iText7 中构建字段层次结构而不直接操作 Fields 字典?尽管 PdfFormField 具有 setParent / addKid 方法,但我还没有找到可以产生的 AcroForm.addField 和 setParent/addKid 的正确组合/序列(希望这种语法有意义):
/Fields [(
/T root 1 0 ("empty" field)
/Kids [(
/T child 2 0 ("empty" field)
/Parent 1 0
/Kids [(
/T text1 (text widget)
/FT Txt
/Type /Annot
/Subtype /Widget
/Parent 2 0
)]
)]
)]
即一个名为的字段root.child.text1
我最接近的(这不适用于某些indirectRef场景)是
PdfFormField root = PdfFormField.createEmptyField(doc);
root.setFieldName("root");
form.addField(root, null);
PdfFormField child = PdfFormField.createEmptyField(doc);
child.setFieldName("child");
form.addField(child, null);
root.addKid(child);
PdfTextFormField text1 = PdfFormField.createText(doc, new Rectangle(100, 700, 200, 20), "text1", "");
// any rendered field needs to be added to the form BEFORE parent
// is set, otherwise an exception is thrown in processKids()
form.addField(text1);
child.addKid(text1);
// cleanup the Fields dict
PdfArray rootFields = form.getPdfObject().getAsArray(PdfName.Fields);
rootFields.remove(text1.getPdfObject());
rootFields.remove(child.getPdfObject());