Search code examples
javapdfpdf-generationitextitextpdf

How can I use tab characters in multiline AcroFields with iText?


The project I'm working on has a PDF file that is used as a template to generate another final PDF.

The template has a couple of form fields. To render the final document I use iText to open the template, find all the form fields and fill in the required data.

There is a requirement now that requires me to render a table-like layout inside 1 multiline acrofield.

The text I need to fill in the field is something like:

Monday\tFrom 10:00\tUntil 12:00
Tuesday\tFrom 20:00\tUntil 22:00

I have used \n in the fields before for newlines, that seems to work. However, tabs don't get displayed. Is there any way I can use tabs in a multiline field?

I've tried using html entities / the unicode representation but that does not seem to work either.


Solution

  • A tab command in this context is a lateral movement to an anchor point of some kind (e.g. a jump to a previously-defined point, such as the next field, next column, etc.), and within a single field, you really don't have any target to jump to.

    If you're able to redefine the space for your text to be three columns with no gutter, no vertical rules or any other visual cues, then you're free to actually do your table-like layout as a table, your Tab characters can function as intended, and you'll have the backup insurance of being able to wrap long entries properly within your column, as opposed to possibly having them blunder into the next column and overset content there.

    One other suggestion that could work for you if you're bound into the current configuration would be to use a monospace font so that all your characters will be a fixed width (e.g. Courier), figure out what your maximum width of each simulated column will be in those characters, count the number of characters you're placing in the current column, and then issue the right number of spaces to get you over to the next column from that location.

    So for example, if your simulated columns within that field will each be 15 characters wide, then "Monday" will consume 6 characters, after which you issue 9 spaces to tab over to your next column, place "From 10:00" there, issue 5 more spaces, drop "Until 12:00" there, and you're done with that row. The next one will align neatly under it, and so on.