getting null when call acroform.getFields() using pdfbox

懵懂的女人 提交于 2019-12-10 20:21:58

问题


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
}

回答1:


this is because your pdf if not contain any acroform




回答2:


form is not null, but that doesn't mean it is not empty.

Check this instead:

if (form.getDocument()!=null)

or

if (form.getFields()!=null)

If those are null, then the error comes from elsewhere. Probably from the document loading code ; )




回答3:


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.



来源:https://stackoverflow.com/questions/16420509/getting-null-when-call-acroform-getfields-using-pdfbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!