iText7 setValue 方法不起作用
iText7 setValue method not working
我正在尝试使用 iText 7
向 pdf 添加表格。
我在尝试设置字段的值时总是收到错误消息。我一直没能从addKid()
方法的documentation中找到信息。有谁知道如何解决这个错误?
这是我正在使用的代码示例:
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
}
我假设您收到的错误是这样的 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()
显式指定 link
我正在尝试使用 iText 7
向 pdf 添加表格。
我在尝试设置字段的值时总是收到错误消息。我一直没能从addKid()
方法的documentation中找到信息。有谁知道如何解决这个错误?
这是我正在使用的代码示例:
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
}
我假设您收到的错误是这样的 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()