使用pdfbox获取表单字段值 [英] Using pdfbox to get form field values

查看:615
本文介绍了使用pdfbox获取表单字段值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我第一次使用pdfbox。现在我正在网站上阅读 Pdf



<总结我有这样的pdf:





只是我的文件有很多很多不同的组件(textField,RadionButton,CheckBox)。对于这个pdf,我必须阅读这些值:Mauro,Rossi,MyCompany。现在我编写了以下代码:

  PDDocument pdDoc = PDDocument.loadNonSeq(myFile,null); 
PDDocumentCatalog pdCatalog = pdDoc.getDocumentCatalog();
PDAcroForm pdAcroForm = pdCatalog.getAcroForm();

for(PDField pdField:pdAcroForm.getFields()){
System.out.println(pdField.getValue())
}

这是读取表单组件内部值的正确方法吗?
有关此问题的任何建议吗?
我在哪里可以学习pdfbox上的其他内容?

解决方案

您所拥有的代码应该有效。如果您实际上想要对值进行某些操作,则可能需要使用其他一些方法。例如,您可以使用 pdAcroForm.getField(< fieldName>)获取特定字段:

  PDField firstNameField = pdAcroForm.getField(firstName); 
PDField lastNameField = pdAcroForm.getField(lastName);

请注意 PDField 只是一个基类。您可以将事物转换为子类以从中获取更多有趣的信息。例如:

  PDCheckbox fullTimeSalary =(PDCheckbox)pdAcroForm.getField(fullTimeSalary); 
if(fullTimeSalary.isChecked()){
log.debug(该人获得全职工资);
} else {
log.debug(该人没有全薪工资);
}

如您所知,您可以在apache pdfbox网站上找到更多信息。


I'm using pdfbox for the first time. Now I'm reading something on the website Pdf

Summarizing I have a pdf like this:

only that my file has many and many different component(textField,RadionButton,CheckBox). For this pdf I have to read these values : Mauro,Rossi,MyCompany. For now I wrote the following code:

PDDocument pdDoc = PDDocument.loadNonSeq( myFile, null );
PDDocumentCatalog pdCatalog = pdDoc.getDocumentCatalog();
PDAcroForm pdAcroForm = pdCatalog.getAcroForm();

for(PDField pdField : pdAcroForm.getFields()){
    System.out.println(pdField.getValue())
}

Is this a correct way to read the value inside the form component? Any suggestion about this? Where can I learn other things on pdfbox?

解决方案

The code you have should work. If you are actually looking to do something with the values, you'll likely need to use some other methods. For example, you can get specific fields using pdAcroForm.getField(<fieldName>):

PDField firstNameField = pdAcroForm.getField("firstName");
PDField lastNameField = pdAcroForm.getField("lastName");

Note that PDField is just a base class. You can cast things to sub classes to get more interesting information from them. For example:

PDCheckbox fullTimeSalary = (PDCheckbox) pdAcroForm.getField("fullTimeSalary");
if(fullTimeSalary.isChecked()) {
    log.debug("The person earns a full-time salary");
} else {
    log.debug("The person does not earn a full-time salary");
}

As you suggest, you'll find more information at the apache pdfbox website.

这篇关于使用pdfbox获取表单字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆