-1

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?

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165

1 Answers1

0

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.

Community
  • 1
  • 1
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • Actually ,my requirement is to know the maximum character limit before populating text in formFiled. Here we need to consider basic font that comes with PDF form and I don't want change font size as well. **need to calculate maximum characters that will fit in the form field.** – Manikanta andydev Aug 06 '15 at 08:52
  • 1
    Please use your common sense and ask yourself if your question makes sense. How can you calculate the maximum characters that will fit in the form field if the characters don't have a predictable width? Please confirm that you understand that your question doesn't make sense. – Bruno Lowagie Aug 06 '15 at 09:28
  • * 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. – Manikanta andydev Aug 06 '15 at 09:43
  • I'll update my question in the hope that you can see the error in your thoughts. – Bruno Lowagie Aug 06 '15 at 09:53
  • The update is done. I hope that you now agree with me that your question is wrong. If you don't agree, you shouldn't expect an answer. – Bruno Lowagie Aug 06 '15 at 10:00