0

を使用してPDFにフォームを追加しようとしてい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:スレッド「メイン」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 に答える