Java iText7,无法设置formField值而不获取错误
我试图添加一个字段和注释,但是当我试图设置值时,我得到了这个错误Java iText7,无法设置formField值而不获取错误,java,pdf,itext7,Java,Pdf,Itext7,我试图添加一个字段和注释,但是当我试图设置值时,我得到了这个错误object.must.be.indirect.to.work.with.this.wrapper。不管设置了哪个字段,或者设置在哪里,都会发生此错误。有人知道如何避开这个错误吗 下面是导致我的问题的示例代码: public class HelloWorld { public static final String DEST = "sampleOutput.pdf"; public static final Str
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;
}
}
公共类HelloWorld{
公共静态最终字符串DEST=“sampleOutput.pdf”;
公共静态最终字符串SRC=“sample.pdf”;
公共静态void main(字符串args[])引发IOException{
新建HelloWorld().createPdf(SRC,DEST);
}
public void createPdf(String src,String dest)引发IOException{
//初始化PDF读写器
PdfReader读取器=新PdfReader(src);
PdfWriter writer=新的PdfWriter(dest);
//初始化PDF文档
PdfDocument pdf=新PdfDocument(读写器);
//初始化文档
文件=新文件(pdf);
HelloWorld.addAcroForm(pdf文件);
//关闭文档
document.close();
}
公共静态PdfAcroForm addAcroForm(PdfDocument pdf,Document doc)引发IOException{
int numPages=pdf.getNumberOfPages();
PdfAcroForm form=PdfAcroForm.getAcroForm(pdf,true);
PdfTextFormField confField=PdfFormField.createText(pdf);
confField.setFieldName(“确认”);
PdfSignatureFormField sigField=PdfFormField.createSignature(pdf);
sigField.setFieldName(“签名”);
PdfWidgetAnnotation firstPageConf=新的PdfWidgetAnnotation(新矩形(0,0425,15));
PdfWidgetAnnotation pageConf=新的PdfWidgetAnnotation(新矩形(0,0425,15));
PdfWidgetAnnotation签名=新的PdfWidgetAnnotation(新矩形(0100425,15));
//是否根据第一页添加conf注释
对于(int i=1;i首先,我们注意到当前iText 7版本中的错误消息存在问题。您看到的错误是错误的键,而不是错误消息的值。幸运的是,错误消息的键已经为我们提供了足够的信息,让我们知道什么是错误的
在PDF中,您有直接对象和间接对象。直接对象本身就是未引用的对象。在这种情况下,您有一个注释字典,它是直接对象(测试对象)
您小时候试图将其添加到字段中,错误告诉您:嘿,我无法处理直接对象。我需要对间接对象的间接引用。您正在添加一个尚未被称为间接对象的对象,因此我无法添加它
如何解决这个问题?您应该确保将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()时将使用该引用
方法。@Bruno几乎做到了。但是在本例中,不仅注释测试,而且表单字段测试字段在设置字段值之前都需要进行间接设置。幸运的是,这可以作为将字段添加到AcroForm的副作用来实现,而您无论如何都应该这样做
因此,只需添加行即可
form.addField(testField);
就在调用testField.setValue
之前,请参阅测试,特别是方法testAddLineElliot
和addAcroForm
我添加了pdf.getPage(1).addAnnotation(测试);行,但我仍然得到相同的错误。这很奇怪。我无法重现此错误。我肯定在其他地方做了一些愚蠢的事情。如果您可以看一看,我添加了更多的上下文。知道我做错了什么吗?当我在上面的代码中为任何PdfFormField设置值时会发生此错误有趣的是,当我有两个单独的孩子在字段: