我正在尝试添加字段和注释,但是当我尝试设置该值时,我收到此错误object.must.be.indirect.to.work.with.this.wrapper
。无论设置了哪个字段,或者设置了哪个字段,都会发生此错误。有谁知道如何解决这个错误?
以下是导致我的问题的示例代码:
public class HelloWorld {
public static final String DEST = "sampleOutput.pdf";
public static final String SRC = "sample.pdf";
public static void main(String args[]) throws IOException {
new HelloWorld().createPdf(SRC, DEST);
}
public void createPdf(String src, String dest) throws IOException {
//Initialize PDF reader and writer
PdfReader reader = new PdfReader(src);
PdfWriter writer = new PdfWriter(dest);
//Initialize PDF document
PdfDocument pdf = new PdfDocument(reader, writer);
// Initialize document
Document document = new Document(pdf);
HelloWorld.addAcroForm(pdf, document);
//Close document
document.close();
}
public static PdfAcroForm addAcroForm(PdfDocument pdf, Document doc) throws IOException {
int numPages = pdf.getNumberOfPages();
PdfAcroForm form = PdfAcroForm.getAcroForm(pdf, true);
PdfTextFormField confField = PdfFormField.createText(pdf);
confField.setFieldName("confirmation");
PdfSignatureFormField sigField = PdfFormField.createSignature(pdf);
sigField.setFieldName("signature");
PdfWidgetAnnotation firstPageConf = new PdfWidgetAnnotation(new Rectangle(0, 0, 425, 15));
PdfWidgetAnnotation pageConf = new PdfWidgetAnnotation(new Rectangle(0, 0, 425, 15));
PdfWidgetAnnotation signature = new PdfWidgetAnnotation(new Rectangle(0, 100, 425, 15));
//add conf annotation based on first page or not
for (int i = 1; i <= numPages; i++) {
PdfPage page = pdf.getPage(i);
if (i == 1) {
page.addAnnotation(firstPageConf);
} else {
page.addAnnotation(pageConf);
}
}
form.addField(confField);
form.addField(sigField);
confField.addKid(firstPageConf);
confField.addKid(pageConf);
sigField.addKid(signature);
//this one is different because we try to set a value....
PdfTextFormField testField = PdfFormField.createText(pdf);
testField.setFieldName("test");
PdfWidgetAnnotation test = new PdfWidgetAnnotation(new Rectangle(0, 100, 425, 15));
pdf.getPage(1).addAnnotation(test);
testField.addKid(test);
testField.setValue("testValue");//error 'object.must.be.indirect.to.work.with.this.wrapper' occurs here
return form;
}
}
答案 0 :(得分:1)
@ Bruno&#39; s answer几乎做到了。但是在手头的情况下,不仅需要在设置字段值之前使注释test
而且还需要使用表单字段testField
进行间接注释。幸运的是,这可以作为将字段添加到AcroForm的副作用来实现,无论如何都应该这样做。
因此,只需添加行
form.addField(testField);
在testField.setValue
来电之前,参见测试AddFieldWithValue.java,特别是方法testAddLineElliot
和addAcroForm
。
答案 1 :(得分:0)
首先:我们注意到当前iText 7版本中的错误消息存在问题。您看到的错误是错误的键,而不是错误消息的值。幸运的是,错误消息的关键已经为我们提供了足够的信息来了解错误。
在PDF中,您有直接对象和间接对象。直接对象就是未引用的对象。在这种情况下,您有一个作为直接对象的注释字典(test
对象)。
你试图将它作为一个小孩添加到一个字段,错误告诉你:嘿,我无法处理直接对象。我需要间接引用间接对象。您正在添加一个尚未被称为间接对象的对象,因此我无法添加它。
如何解决这个问题?那么,您应该确保test
作为间接对象添加到文档中。一种方法是将它作为注释添加到页面中(无论如何, 应该做什么,否则你的代码没有任何意义)。
所以让我们添加一行:
PdfTextFormField testField = PdfFormField.createText(pdf);
testField.setFieldName("test");
PdfWidgetAnnotation test = new PdfWidgetAnnotation(new Rectangle(0, 0, 425, 15));
pdf.getPage(1).addAnnotation(test);
testField.addKid(test);
testField.setValue("testValue");
添加以下行:
pdf.getPage(1).addAnnotation(test);
我们将注释字典作为间接对象添加到PDF中。创建参考。使用addKid()
方法时会使用该引用。