1

Given an acrokey, is it possible to find the absolute position and dimension of that particular field (getLeft, getTop, getWidth, getHeight) ?

And is the viceversa possible - if I know the position, can I get the acrokey of the field?

Alexis Pigeon
  • 7,423
  • 11
  • 39
  • 44
ahairshi
  • 381
  • 3
  • 18

1 Answers1

3

First part of your question:

Suppose that you have an AcroFields instance (form), either retrieved from a PdfReader (read only) or a PdfStamper instance, then you can get the field position of the first widget that corresponds with a specific field name like this:

Rectangle rectangle = form.getFieldPositions(name).get(0).position;

Note that one field can correspond with multiple widgets. For instance, to get the second widget, you need:

Rectangle rectangle = form.getFieldPositions(name).get(1).position;

Of course: you probably also want to know the page number:

int page = form.getFieldPositions(name).get(0).page;

Second part of your question

Fields correspond with widget annotations. If you know the page number of the widget annotation, you could get the page dictionary and inspect the entries of the /Annots array. You'll have to loop over the different annotations, inspecting each annotation's /Rect entry. Once you find a match, you need to crawl the content for the field that corresponds with the annotation. That's more work than can be provided in a code sample on this site.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • Thank you for your reply. First part - Worked fine Second Part - We have two exacts pdf (one for test, another for production). From the test pdf, I can get the acrofields for the signature. I need only three fields, and there are more than 70 forms. I will manually verify the test pdfs. Using the acrokeys we got from test pdfs, can we get the related acrokeys from the respective prod pdf? – ahairshi Apr 16 '14 at 12:12
  • 2
    *Using the acrokeys we got from test pdfs, can we get the related acrokeys from the respective prod pdf?* - How are they *related*? – mkl Apr 17 '14 at 21:47
  • same content, same field with different acrokeys.. We make our changes in test pdf. Once everything is done, the last test version will be used to create a prod pdf. – ahairshi Apr 30 '14 at 15:43
  • Just to be sure: when you say "acrokey", do you mean "field name"? Your comment doesn't clarify the question. "Once everything is done, the last test version will be used to create a prod PDF" is meaningless to a third party such as me or mkl or any other SO user as long as you don't explain the context of what you're trying to achieve. – Bruno Lowagie Apr 30 '14 at 16:05