I am trying to find out which format a date of birth should have in a empty field in a PDF using iText.
I can stamp the field with a value but then I need to know how many digits are expected for the date of birth. I figured that if I could get the length of the field, I could know which format to use, because the formats can be:
YYYYMMDDNNNN (14 digits) YYYYMMDD (10 digits) YYMMDD (8 digits)
The field I'm talking about has a fixed number of digits, if I stamp the field with too many numbers, then the numbers that fall outside the field disappear. I can share my PDF if necessary.
You're in luck. I've examined your PDF and the field for the date of birth has a
/MaxLen entry which means that you can find out the maximum length of the field. In the following screenshot, you can see the properties of the field / annotation dictionary for field
Falt__41 (which can be used to add the Sökandens personnummer):
RUPS screen shot
As you can see, this field can contain a maximum of 12 characters. Moreover, the
/Ff (fields flags) value is 29360128 or binary value: 1110000000000000000000000. This means that the following flags are active: do not spell check, do not scroll, and comb. The comb flag makes that whatever you enter, the characters will be evenly distributed over the available width. In this case, every character will take 1/12 of the available width.
Now how do you retrieve the
/MaxLen value? This is some code written from memory:
PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true); PdfFormField fieldName= form.getField("Falt__41"); int maxLen = fieldName.getPdfObject().getAsInt(PdfName.MaxLen);
Or you can receive it by the next way:
int maxLen = ((PdfTextFormField)fieldName).getMaxLen();
Now you can get the
int value of
Important note: not every field has a
Click How to get the number of characters in a field? if you want to see how to answer this question in iText 5.