1

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.

Victoria Agafonova
  • 2,048
  • 10
  • 33
  • 50
  • 1
    From the stack trace, since the NPE is thrown by `com.itextpdf.text.pdf.XfaForm.findFieldName`, could it be that there's not field called `"Text"`? – Alexis Pigeon Dec 19 '12 at 09:03
  • 1
    Please also state which version puff itext you are using. – mkl Dec 19 '12 at 09:20
  • mkl, thank you for your reminder, i've added this info. – Victoria Agafonova Dec 19 '12 at 12:01
  • Alexis Pigeion, nope. It fails on that line of code inside the method: acroFieldsSom = new AcroFieldsSearch(datasetsSom.getName2Node().keySet()); And, as launching my app in debug mode shows, datasetsSom is null. Also, datasetsSom in private field with setter and getter, so, it can be initialized somewhere outside its class code. So, that why its not clear to me how to get it, where and why this variable is not initialized (just simply i have no ideas where to put breakpionts and so on). – Victoria Agafonova Dec 19 '12 at 12:39

1 Answers1

2

The NPE isn't happening in your code, it's happening in the library you're using: com.itextpdf.text.pdf

I'd recommend following up with the providers of this library to get support from them - it's highly unlikely to be intentional that they are throwing a NullPointerException from their API.

If that is not possible, perhaps you could get a copy of the source and debug into it to investigate the differences between the case that works and the case that crashes.

Krease
  • 15,805
  • 8
  • 54
  • 86