How do I create a text comb field and limit the number of characters in the field?

Q: I’ve to populate a text field that is a comb of 10 chars (as
defined under Acrobat text field properties>options); this not a combo
field. Each character is placed in its own cell (like, [H][e][l][l]
[o]…), center aligned. Imagine as if you are entering, say, an
account number in a series of boxes. If I try to setValue as a normal
text field, the contents overwrites the cell boundaries. Can I address
each cell separately – I’m guessing there
must be a way as Acrobat allows specifying a text field with such
properties. The alternative is to treat each cell as a text field, but
then I’ll have a lot of text fields to deal with, as there are others
of this type.

A: To achieve the ‘comb text’ effect you need to ‘e_comb’ flag in the
form field. For example:

field.SetFlag(Annot.Flag.e_comb, true);
field.GetSDFObj().PutNumber(“MaxLen”, 13);

If e_comb flag is set, the field is automatically divided into as many
equally spaced positions, or combs, as the value of MaxLen, and the
text is laid out into those combs. The flag is meaningful only if the
MaxLen entry is present in the text field and if the Multiline,
Password, and FileSelect flags are clear.

For comb text fields it is typically a good idea to use a monospace
font (i.e. a font in which all glyphs have the same width). The most
popular monospace font is Courier. The following line can be used to
set the ‘default’ appearance:

field.GetSDFObj().PutString(“DA”, " /Cour 0 Tf 0 g ");