I have a fillable pdf form, made with LC Designer. I want to fill it out automatically using iText:
public void fillOut(String input, String output, boolean remove, boolean preserve) throws IOException, DocumentException{
PdfReader reader = new PdfReader(input);
if (remove)
reader.removeUsageRights();
PdfStamper stamper;
if(preserve){
System.out.println("preserve");
stamper = new PdfStamper(reader, new FileOutputStream(input), '\0', true);
}else{
System.out.println("don't preserve");
stamper = new PdfStamper(reader, new FileOutputStream(output));
}
AcroFields form = stamper.getAcroFields();
Map<String,Item> fields = form.getFields();
System.out.println(fields.size());
for (Entry<String, Item> ent: fields.entrySet()) {
System.out.println("[" + ent.getKey() + "] [" + ent.getValue() + "]");
}
form.setField("Text", "test text");
stamper.close();
}
But this code produses NPE:
Exception in thread "main" java.lang.NullPointerException
at com.itextpdf.text.pdf.XfaForm.findFieldName(XfaForm.java:294)
at com.itextpdf.text.pdf.AcroFields.setField(AcroFields.java:1387)
at com.itextpdf.text.pdf.AcroFields.setField(AcroFields.java:1316)
at FillOutForm.fillOut(FillOutForm.java:40)
at AcroTest.main(AcroTest.java:13)
At the same time, when i use the same method, fillOut(String input, String output, boolean remove, boolean preserve), to fill out form, which was previously created with iText - everything is ok.
Here is 7z archive with both pdfs, LCtest.pdf made with LC designer, and generates error, itest.pdf - is made with itext and works fine. So, what is wrong? Is it something wrong in my code, or it is bug?
PS: iText version i'm using is 5.1.3, and 5.3.5 acts the same way.