iText7 setValue方法不起作用 [英] iText7 setValue method not working

查看:68
本文介绍了iText7 setValue方法不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用iText 7向pdf添加表单.

I am trying to add a form to a pdf using iText 7.

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

I keep getting an error when trying to set the value of the field. I haven't been able to find information from the documentation of the addKid() method. Does anyone know how to get around this error?

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

Here's a sample of the code I'm using:

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中的异常:对象必须是间接工作的用这个包装器??

I presume the error you are getting is this PdfException: Exception in thread "main" com.itextpdf.kernel.PdfException: Object must be indirect to work with this wrapper`?

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

The solution is to mark you annotations as indirect after creating them:

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()

Explanation: When setting the values of form fields in iText7, it expects the annotations to be indirect objects and will raise an exception when they aren't. Since PdfWidgetAnnotationis created independently from the PdfDocument, the link needs to be specified explicitly by calling makeIndirect()

这篇关于iText7 setValue方法不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆