0

我正在尝试使用iText 7.

尝试设置字段的值时,我不断收到错误消息。我无法从该方法的文档中找到信息addKid()。有谁知道如何解决这个错误?

这是我正在使用的代码示例:

PdfTextFormField confField = PdfFormField.createText(pdf);
confField.setFieldName(fieldName);

PdfWidgetAnnotation confCoverAnnot = new PdfWidgetAnnotation(new Rectangle(x, y, width, height));
PdfWidgetAnnotation confAnnot = new PdfWidgetAnnotation(new Rectangle(x2, y2, width2, height2));

for (int i = 1; i<= numPages; i++) {
    switch(i) {
        case 1:
            pdf.getPage(i).addAnnotation(confCoverAnnot);
            break;
        default:
            pdf.getPage(i).addAnnotation(confAnnot);
            break;
    }
}


/*
    Trying to have two different annotations reference the same field value.

    Upon using the `setValue()` method, I get: object.must.be.indirect.to.work.with.this.wrapper
    Any way to get this to work properly?
*/
form.addField(confField);
confField.addKid(confCoverAnnot);
confField.addKid(confAnnot);
if (value.equals("") != true) {
    confField.setValue(value); //error here
}
4

1 回答 1

2

我想你得到的错误是PdfException:线程“main”com.itextpdf.kernel.PdfException中的异常:对象必须是间接的才能使用这个包装器`?

解决方案是在创建注释后将注释标记为间接:

PdfWidgetAnnotation confCoverAnnot = new PdfWidgetAnnotation(new Rectangle(x, y, width, height));
confCoverAnnot.makeIndirect(pdf);
PdfWidgetAnnotation confAnnot = new PdfWidgetAnnotation(new Rectangle(x, y, width, height));
confAnnot.makeIndirect(pdf);

说明:在 iText7 中设置表单字段的值时,它希望注释是间接对象,如果不是,则会引发异常。由于PdfWidgetAnnotation是独立于创建的PdfDocument,因此需要通过调用显式指定链接makeIndirect()

于 2016-11-22T14:18:02.107 回答