繁体   English   中英

如何使用pdfbox获取字段的位置?

[英]How to get the position of a field using pdfbox?

我被困在一个领域。 我需要在一个 pdf 中确定PDAcroForm字段的位置。 我需要对字段的 x 和 y 值进行一些处理。

知道如何做到这一点吗? 信息是否存在于 COS 对象中?

我今天遇到了同样的问题。 以下代码适用于我的情况:

private PDRectangle getFieldArea(PDField field) {
  COSDictionary fieldDict = field.getDictionary();
  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();

  return new PDRectangle(new BoundingBox(left, bottom, right, top));
}

编辑:karthicks 代码更短。 所以我现在使用这个代码:

private PDRectangle getFieldArea(PDField field) {
  COSDictionary fieldDict = field.getDictionary();
  COSArray fieldAreaArray = (COSArray) fieldDict.getDictionaryObject(COSName.RECT);
  PDRectangle result = new PDRectangle(fieldAreaArray);
  return result;
}

如果要测试返回的矩形是否正确,可以使用此代码:

private void printRect(final PDPageContentStream contentStream, final PDRectangle rect) throws IOException {
  contentStream.setStrokingColor(Color.YELLOW);
  contentStream.drawLine(rect.getLowerLeftX(), rect.getLowerLeftY(), rect.getLowerLeftX(), rect.getUpperRightY()); // left
  contentStream.drawLine(rect.getLowerLeftX(), rect.getUpperRightY(), rect.getUpperRightX(), rect.getUpperRightY()); // top
  contentStream.drawLine(rect.getUpperRightX(), rect.getLowerLeftY(), rect.getUpperRightX(), rect.getUpperRightY()); // right
  contentStream.drawLine(rect.getLowerLeftX(), rect.getLowerLeftY(), rect.getUpperRightX(), rect.getLowerLeftY()); // bottom
  contentStream.setStrokingColor(Color.BLACK);
}

接受的答案不再有效。 我已经尝试过这种方法并收到了某些元素的NullPointerException 在 PDFBOX 2.x 中,无需直接查询 COS 对象树即可获取矩形。

有关字段位置的信息存储在PDAnnotationWidget 可以有更多与该字段关联的小部件。 获得第一个(不检查这些是否是一个)。

PDRectangle rectangle = field.getWidgets().get(0).getRectangle();

获取所有矩形(如果可能有更多):

List<PDRectangle> rectangles = field.getWidgets().stream().map(PDAnnotation::getRectangle).collect(Collectors.toList());

我能够得到这样的细节

   COSDictionary trailer = document.getDocument().getTrailer();
   COSDictionary root = (COSDictionary) trailer.getDictionaryObject(COSName.ROOT);
   COSDictionary acroForm = (COSDictionary) root.getDictionaryObject(COSName.getPDFName("AcroForm"));
   if (null != acroForm) {
    COSArray fields1 = (COSArray) acroForm.getDictionaryObject(COSName.getPDFName("Fields"));
    for (int l = 0; l < fields1.size(); l++) {
     COSDictionary field = (COSDictionary) fields1.getObject(l);
     COSArray rectArray= (COSArray)field.getDictionaryObject("Rect");
     PDRectangle mediaBox = new PDRectangle( rectArray ); 
System.out.println("mediaBox: " + mediaBox.getLowerLeftX()  +"||" +mediaBox.getLowerLeftY());
System.out.println("mediaBox: " + mediaBox.getUpperRightX()  +"||" + mediaBox.getUpperRightY());

使用以下代码获取最新的 PdfBox 依赖版本

private PDRectangle getFieldArea(PDField field) {
    COSDictionary fieldDict = field.getCOSObject();
    COSArray fieldAreaArray = (COSArray) fieldDict.getDictionaryObject(COSName.RECT);
    PDRectangle rectangle = new PDRectangle(fieldAreaArray);
    System.out.println(rectangle);
    return rectangle;
}

暂无
暂无

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

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