Code snippet 2 is correct (after a small fix I applied), 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.
Here's an extra code snippet:
TextField tx = new TextField(writer, null, null);
fields.decodeGenericDictionary(mergedFieldDictionary, tx);
float size = tx.getFontSize();
BaseFont basefont = tx.getFont();
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: How to calculate the string width in iText?) and compare it with width
. With my extra code snippet, you have a size
and a basefont
. If your text is in a variable text
, this is how you get the width of the text:
float w = basefont.getWidthPoint(text, 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.
Update:
In a comment you say:
For, example my text value is of length 100 characters, and if for suppose limit is 60. Then based on 60, i need to add 60 characters in first page and remaining 40 character text in next page of pdf form. * Here i want know how can we determine the limit.
Please compare:
IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
with:
WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW
In both cases, we have exactly 100 characters. However, the first sequence fits a line in a StackOverflow page, whereas the second sequence doesn't.
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. It surprises me that you disagree with me.
You can only calculate how many characters fit in case the form tells you that the content is added using a monospace font.