简体   繁体   English

使用pdfbox调用acroform.getFields()时获取null

[英]getting null when call acroform.getFields() using pdfbox

I tried to get All the fields available in pdf form but I'm encountering a NullPointerException when calling acroform.getFields() using PDFBox. 我试图以pdf格式获取所有字段,但是在使用PDFBox调用acroform.getFields()时遇到NullPointerException

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
}

这是因为你的pdf如果不包含任何acroform

form is not null , but that doesn't mean it is not empty . form不为null ,但这并不意味着它不是空的

Check this instead: 检查一下:

if (form.getDocument()!=null)

or 要么

if (form.getFields()!=null)

If those are null, then the error comes from elsewhere. 如果它们为null,那么错误来自其他地方。 Probably from the document loading code ; 可能来自文档加载代码; )

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. 我有同样的错误,结果我只是假设从这个特定的屏幕我们的集合中的所有PDF都有字段。 It turned out that was not the case and that we had clients with certain pdfs that had no fields at all. 事实证明情况并非如此,我们的客户拥有某些根本没有字段的pdf。 So just add a null check to make sure AcroForm is not null and you should be good to go. 所以只需添加一个空检查以确保AcroForm不为空,你应该好好去。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM