首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iText7 setValue方法无效

iText7 setValue方法无效
EN

Stack Overflow用户
提问于 2016-11-21 02:54:23
回答 1查看 609关注 0票数 0

我试图添加一个形式到一个pdf使用iText 7

当我试图设置字段的值时,我一直得到一个错误。我无法从文档 of addKid()方法中找到信息。有人知道如何避免这个错误吗?

下面是我使用的代码示例:

代码语言:javascript
复制
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
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-11-22 14:18:02

我猜想您所得到的错误是线程中的PdfException:com.itextpdf.kernel.PdfException: Object必须是间接的,才能使用这个包装吗?

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

代码语言:javascript
复制
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中设置表单字段的值时,它希望注释是间接对象,如果不是,则会引发异常。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40712084

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档