Skip to content
Advertisement

The non-latin text is disappearing from the pdf text field

I create a PDF document with text fillable fields using itext 7.1.9 library. The PdfTextFormField contains a multilanguage text. When the PDF document has created, I open it in Adobe Acrobat Reader and the non-latin symbols are disappearing from the text field and I see only latin symbols, but if I click on the field the whole text will be visible including non-latin symbols. [! The PDF text field after openning document]1. [! The PDF text field after clicking to the field]2. For creating a PDF document I’m using the code like the following:

JavaScript

I have tried to solve this issue and I even found the article on itext blog, but it didn’t help me. I know about using ff.setNeedAppearence(true) method, but I can’t use it because it breaks another part of my app. And I couldn’t set PdfEncoding.IDENTITY_H because it embeds only a subset of symbols which was included programmatically to the field and a user can’t fill out this field.
Can anyone help me? What do I do wrong?

Advertisement

Answer

To make sure the full font is embedded, and not just a subset, use font.setSubset(false);.

Generally speaking, you should as much as possible try to use a font which contains all of the glyphs from your value. Otherwise the consumers of your PDFs may have issues.

As a workaround you can create your own appearance using layout module by utilizing FontSet functionality which selects appropriate fonts automatically. In my example I only add one font to the FontSet but you may add multiple fonts there. It is strongly recommended, though, to limit the number of fonts to one, and to the smallest possible number if one is not possible.

So here we basically create a PdfFormXObject which serves as our appearance object:

JavaScript

And then we have to set it to the field:

JavaScript

Full code of your createPdf now looks as follows:

JavaScript

Visual result on opening the PDF:

result

UPD The code above works fine in Adobe Acrobat, Foxit, Chrome PDF viewer but when you open it up in Adobe Reader you see empty form field.

To make it work in Acrobat, you have to make sure to have your XObject bbox start at origin:

JavaScript

And also wrap the appearance into /Tx BMC / EMC block which marks the part that need to be replaced when appearance is regenerated.

Fixed part of code produces correct result in Adobe Acrobat Reader as well:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement