我试图添加一个形式到一个pdf使用iText 7。
当我试图设置字段的值时,我一直得到一个错误。我无法从文档 of 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
}发布于 2016-11-22 14:18:02
我猜想您所得到的错误是线程中的PdfException:com.itextpdf.kernel.PdfException: Object必须是间接的,才能使用这个包装吗?
解决方案是在创建注释之后将其标记为间接注释:
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中设置表单字段的值时,它希望注释是间接对象,如果不是,则会引发异常。
https://stackoverflow.com/questions/40712084
复制相似问题