Problem with the display of Cyrillic characters in a pdf with lib itext

318 Views Asked by At

In the PDF document, you need to display the text in Russian. Many ways were used, but the result is the same - instead of letters "krazyabry". The text in English is displayed as intended.

Used:

  • Android JDK + Kotlin

  • iText 7.2.5

Name of font assets is const

const val fontName = "roboto.ttf"

Screenshot of assets folder

enter image description here

The font is added at creation

return@map PdfTextFormField
  .createText(outputDoc, it.wrapper, it.type.fieldName, it.v, font, 10f)
  .setJustification(1)

Output

enter image description here

Try #1

protected val font: PdfFont by lazy {
  val asset = context.assets.open(fontName).readBytes()
  return@lazy PdfFontFactory.createFont(asset, PdfEncodings.IDENTITY_H, PdfFontFactory.EmbeddingStrategy.FORCE_EMBEDDED)
}

Result: no crash, no russian symbols

Try #2

protected val font: PdfFont by lazy {
    val asset = context.assets.open(fontName).readBytes()
    return@lazy PdfFontFactory.createFont(asset, PdfEncodings.CP1250, PdfFontFactory.EmbeddingStrategy.FORCE_EMBEDDED)
  }

Result: no crash, no russian symbols

Try #3

protected val font: PdfFont by lazy {
    val asset = context.assets.open(fontName).readBytes()
    return@lazy PdfFontFactory.createFont(asset, "Cp1251", PdfFontFactory.EmbeddingStrategy.FORCE_EMBEDDED)
  }

Result: no crash, no russian symbols

Try #4

protected val font: PdfFont by lazy {
    return@lazy PdfFontFactory.createFont("/assets/arial.ttf", PdfEncodings.IDENTITY_H, PdfFontFactory.EmbeddingStrategy.FORCE_EMBEDDED)
}

Result: no crash, no russian symbols

Try #5

protected val font: PdfFont by lazy {
    return@lazy PdfFontFactory.createFont("/assets/arial.ttf", PdfEncodings.IDENTITY_H, PdfFontFactory.EmbeddingStrategy.FORCE_EMBEDDED)
}

Result: no crash, no russian symbols

Try #6

protected val font: PdfFont by lazy {
    return@lazy PdfFontFactory.createFont("/assets/arial.ttf", "Cp1251", PdfFontFactory.EmbeddingStrategy.FORCE_EMBEDDED)
}

Result: no crash, no russian symbols

Try #7

protected val font: PdfFont by lazy {
        return@lazy PdfFontFactory.createFont( "/system/fonts/Comic.ttf", PdfEncodings.UNICODE_BIG_UNMARKED, PdfFontFactory.EmbeddingStrategy.FORCE_EMBEDDED)
}

Result: java.io.IOException: /system/fonts/Comic.ttf not found as file or resource

Try #8

protected val font: PdfFont by lazy {
        return@lazy PdfFontFactory.createFont( "/system/fonts/Roboto-Regular.ttf", PdfEncodings.IDENTITY_H, PdfFontFactory.EmbeddingStrategy.FORCE_EMBEDDED)
}

Result: no crash, no russian symbols

Try with encodoing: UTF8, UNICODBIG (error), UNICODEBIG_UNMARKED (error)
error: java.lang.ArrayIndexOutOfBoundsException: length=128; index=128

1

There are 1 best solutions below

1
Alexey Subach On

Have you tried this? You need a font that has the relevant glyphs and arialuni.ttf is one of them:

protected val font: PdfFont by lazy {
    return@lazy PdfFontFactory.createFont("/assets/arialuni.ttf", PdfEncodings.IDENTITY_H, PdfFontFactory.EmbeddingStrategy.FORCE_EMBEDDED)
}

Just make sure to find the right font file (.ttf file)