How can I find the maximum character limit for a text field?

Tags: formsmeasure texttext fieldiText 7

I've researched this problem and I tried the following code snippets. Unfortunately, I couldn't find the maximum character limit for a text field in an AcroForm form:

Snippet 1

List<AcroFields.FieldPosition> positions = fields.getFieldPositions(signame);
Rectangle rect = positions.get(0).position; // In points:
float left   = rect.getLeft();
float bTop   = rect.getTop();
float width  = rect.getWidth();
float height = rect.getHeight();
Snippet 2

PdfDictionary mergedFieldDictionary = myAcroFields.getFieldItem( key ).getMerged( 0 );
PdfNumber maxLengthNumber = mergedFieldDictionary.getAsNumber( PdfName.MAXLEN );
if (maxLenghtNumber != null) {
  MaxFieldLength = maxLengthNumber.intValue();
Can any one please help me out?

Posted on StackOverflow on Aug 5, 2015 by Manikanta andydev

Code snippet 2 is correct, but it will only work if a maximum length was defined for the form field. Usually, no maximum length will be defined in which case maxLenghtNumber will be null.

If no maximum length was defined, there is no limit, so you will have to measure if the text fits. Based on your code snippet 1, you already know the available width: width. How many characters can fit this width depends on the font size and the characters. The width of the characters depends on the font.

If the font is a monospaced font, it will be very easy to know how many characters fit within the width. In this case, it's sufficient to get the width of a single glyph (e.g. w) and divide the total width by the glyph width: width / w.

Usually, fonts will have proportional widths, making your question unanswerable. For instance: the width of "IIIIIIIIII" is smaller than the width of "WWWWWWWWWW" In both cases, we have 10 characters, but an I doesn't take as much space as a W.

I suggest that you measure your String value (as explained here: and compare it with width. If your text is in a variable text and you have size and a font, this is how you get the width of the text:

double w = font.getContentWidth(new PdfString(text)) * font.getFontMatrix()[0]* size

If that value is greater than width, you need to downsize font size to make the text fit.

Important: in some forms, the font size is defined as 0. This doesn't mean that a font size of 0 should be used. It means that the viewer should choose a font size in such a way that it fits into the text field.

For example: if my text value is of length 100 characters, and if the limit is 60, then I need to add 60 characters in the first page and the remaining 40 character text in next page of pdf form. How can I determine the limit to achieve this?

Please compare:




In both cases, we have exactly 100 characters. However, the first sequence takes a lot less space than the second one.

If your form doesn't define any /MaxLength, then you have to determine if the characters of your String match the available width. This can NOT be expressed as a number of characters, because the number of characters that fit depends on the characters you want to add!

100 I characters might fit, whereas 100 W characters may not. This is elementary logic. You can only calculate how many characters fit in case the form tells you that the content is added using a monospace font.

Click this link if you want to see how to answer this question in iText 5.