Search code examples
formspdffieldpdflib

Get form field position with pdflib and pCos


For one project, I have to get the form fields of the pdf in the right order, I use pdflib but the fields are not extracted in the same position than they appear in the PDF. I rode that we can extract the form field position with pCos but I don't find the solution.

Thanks!


Solution

  • you can retrieve the rectangle of a form field with pCos by using the "/Rect" dictionary of the fields. A very simple (java based sample) looks like in this way:

            String rect_path = "fields[" + f + "]/Rect";
            if (p.pcos_get_string(doc, "type:" + rect_path).equals("array")
                && (int) p.pcos_get_number(doc, "length:" + rect_path) == 4) {
              System.out.print("[");
              for (int i = 0; i < 4; i += 1) {
                if (i > 0) {
                    System.out.print(" ");
                }
                System.out.print(p.pcos_get_number(doc, rect_path + "[" + i + "]"));
              }
              System.out.print("], "); 
            }
    

    The "Rect" dictionary contains four values, with the lower left and upper right coordinates of the form field. So you can loop over of the dictionary and retrieve the values.

    you might find further samples in the pCOS cookbook