3

I tried to get All the fields available in pdf form but I'm encountering a NullPointerException when calling acroform.getFields() using PDFBox.

Sample:

pdDoc = PDDocument.load(fileName);
PDAcroForm form = pdDoc.getDocumentCatalog().getAcroForm();
if(form!=null)
{
    List<PDField> field = form.getFields(); //here I am getting null pointer exception
}
MWiesner
  • 8,868
  • 11
  • 36
  • 70
sudhan sudh
  • 31
  • 1
  • 2
  • Are you sure you get NPE in that line? `pdDoc.getDocumentCatalog().getAcroForm();` seems much more likely to throw it. – Vladimir May 07 '13 at 13:44
  • pdDoc.getDocumentCatalog().getAcroForm() works great for me. Thanks! – Jianwen W. Aug 02 '13 at 08:18
  • Was this ever solved? Please answer yourself, or click the green checkmark on one of the answers or delete the question. For me, neither the question is clear nor is any answer useful. Could it be that the NPE was in PDFBox itself (at that time)? – Tilman Hausherr Jan 31 '17 at 12:26

2 Answers2

2

this is because your pdf if not contain any acroform

sdsds
  • 21
  • 2
0

I had this same error, and it turned out I was merely assuming all PDFs in our collection from this particular screen would have fields. It turned out that was not the case and that we had clients with certain pdfs that had no fields at all. So just add a null check to make sure AcroForm is not null and you should be good to go.

Uncle Iroh
  • 5,748
  • 6
  • 48
  • 61