7

あるエリアに行き詰まりました。PDAcroForm1 つの pdf 内のフィールドの位置を特定する必要があります。フィールドの x 値と y 値を処理する必要があります。

これを行う方法はありますか?情報は COS オブジェクトに存在しますか?

4

4 に答える 4

13

今日も同じ問題がありました。私の場合、次のコードが機能します。

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);
}
于 2013-02-15T14:29:43.190 に答える
6

受け入れられた答えはもう機能しません。私はアプローチを試み、NullPointerExceptionいくつかの要素を受け取りました。PDFBOX 2.x では、COS オブジェクト ツリーを直接照会せずに四角形を取得できます。

フィールド位置に関する情報は に格納されPDAnnotationWidgetます。フィールドに関連付けられたウィジェットがさらに存在する場合があります。最初のものを取得する (これらが 1 かどうかをチェックせずに)。

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

すべての長方形を取得するには (もっとある場合):

List<PDRectangle> rectangles = field.getWidgets().stream().map(PDAnnotation::getRectangle).collect(Collectors.toList());
于 2017-07-08T09:14:40.603 に答える
2

このような詳細を取得できます

   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());
于 2013-02-18T05:40:27.960 に答える