如何使用 PDFbox 获取单选按钮的位置
How to get position of Radiobutton using PDFbox
您好,我正在使用此代码获取 PDF 中组件的位置。但是,如果字段是 Radiobutton 类型,则 fieldAreaArray = null
COSDictionary fieldDict = field.getCOSObject();
COSArray fieldAreaArray = (COSArray) fieldDict.getDictionaryObject(COSName.RECT);
float left = (float) ((COSFloat) fieldAreaArray.get(0)).doubleValue();
float bottom = (float) ((COSFloat) fieldAreaArray.get(1)).doubleValue();
float right = (float) ((COSFloat) fieldAreaArray.get(2)).doubleValue();
float top = (float) ((COSFloat) fieldAreaArray.get(3)).doubleValue();
一个字段可以有多个widgets(一个单选按钮肯定会有,看问题下面mkl的评论)然后widgets列表会在/Kids数组中找到,所以让你的生活变得简单并使用这些方法由 PDFBox 提供,而不是直接访问字典。附加代码使用 PDFBOX-142 issue. The field "amt", although it looks like a checkmark, is radio button field with 9 widgets - run PDFDebugger 中的文件并转到 "View" 菜单,更改为 "show internal structure" 然后查看 Root/AcroForm/Fields:
要以编程方式获取矩形,请使用此代码:
PDDocument document = PDDocument.load(new URL("https://issues.apache.org/jira/secure/attachment/12742551/Testformular1.pdf").openStream());
PDField field = document.getDocumentCatalog().getAcroForm().getField("amt");
System.out.println(field.getClass().getSimpleName());
for (PDAnnotationWidget widget : field.getWidgets())
{
System.out.println(widget.getRectangle());
}
document.close();
这是输出:
PDRadioButton
[177.944,580.3095,190.1991,592.0744]
[178.8963,350.25168,191.45924,361.8096]
[535.79285,467.17078,548.0479,478.93567]
[417.16348,467.17078,429.41858,478.93567]
[298.04395,466.19037,310.29904,477.95526]
[177.944,466.19037,190.1991,477.95526]
[534.81244,581.2899,547.0675,593.0548]
[417.16348,581.2899,429.41858,593.0548]
[298.04395,581.2899,310.29904,593.0548]
您可以通过使用 PDFDebugger 打开文件来验证这些位置(如果已经在 "show internal structure" 模式下打开,然后在 "View" 菜单中选择 "show pages")并将鼠标移到渲染图像,坐标将显示在主框架的左下角。
您好,我正在使用此代码获取 PDF 中组件的位置。但是,如果字段是 Radiobutton 类型,则 fieldAreaArray = null
COSDictionary fieldDict = field.getCOSObject();
COSArray fieldAreaArray = (COSArray) fieldDict.getDictionaryObject(COSName.RECT);
float left = (float) ((COSFloat) fieldAreaArray.get(0)).doubleValue();
float bottom = (float) ((COSFloat) fieldAreaArray.get(1)).doubleValue();
float right = (float) ((COSFloat) fieldAreaArray.get(2)).doubleValue();
float top = (float) ((COSFloat) fieldAreaArray.get(3)).doubleValue();
一个字段可以有多个widgets(一个单选按钮肯定会有,看问题下面mkl的评论)然后widgets列表会在/Kids数组中找到,所以让你的生活变得简单并使用这些方法由 PDFBox 提供,而不是直接访问字典。附加代码使用 PDFBOX-142 issue. The field "amt", although it looks like a checkmark, is radio button field with 9 widgets - run PDFDebugger 中的文件并转到 "View" 菜单,更改为 "show internal structure" 然后查看 Root/AcroForm/Fields:
要以编程方式获取矩形,请使用此代码:
PDDocument document = PDDocument.load(new URL("https://issues.apache.org/jira/secure/attachment/12742551/Testformular1.pdf").openStream());
PDField field = document.getDocumentCatalog().getAcroForm().getField("amt");
System.out.println(field.getClass().getSimpleName());
for (PDAnnotationWidget widget : field.getWidgets())
{
System.out.println(widget.getRectangle());
}
document.close();
这是输出:
PDRadioButton
[177.944,580.3095,190.1991,592.0744]
[178.8963,350.25168,191.45924,361.8096]
[535.79285,467.17078,548.0479,478.93567]
[417.16348,467.17078,429.41858,478.93567]
[298.04395,466.19037,310.29904,477.95526]
[177.944,466.19037,190.1991,477.95526]
[534.81244,581.2899,547.0675,593.0548]
[417.16348,581.2899,429.41858,593.0548]
[298.04395,581.2899,310.29904,593.0548]
您可以通过使用 PDFDebugger 打开文件来验证这些位置(如果已经在 "show internal structure" 模式下打开,然后在 "View" 菜单中选择 "show pages")并将鼠标移到渲染图像,坐标将显示在主框架的左下角。