You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Kotlin中iText 7生成PDF无法显示西里尔字符问题

Android + iText 7.2.5生成PDF俄语文本显示异常问题

问题概述

生成PDF文档时,俄语文本无法正常显示(显示为乱码),仅英文文本显示正常。

使用环境

  • Android JDK + Kotlin
  • iText 7.2.5

相关代码与资源

字体资源定义

const val fontName = "roboto.ttf"

字体资源放置在项目的assets目录下。

创建文本表单字段代码

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

输出效果

俄语文本无显示,仅英文内容正常。

尝试过的解决方案及结果

尝试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)
}

结果:无崩溃,但无俄语字符显示

尝试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)
  }

结果:无崩溃,但无俄语字符显示

尝试3

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

结果:无崩溃,但无俄语字符显示

尝试4

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

结果:无崩溃,但无俄语字符显示

尝试5
(与尝试4代码相同)
结果:无崩溃,但无俄语字符显示

尝试6

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

结果:无崩溃,但无俄语字符显示

尝试7

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

结果:抛出异常 java.io.IOException: /system/fonts/Comic.ttf not found as file or resource

尝试8

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

结果:无崩溃,但无俄语字符显示

额外尝试:使用UTF8、UNICODBIG、UNICODEBIG_UNMARKED编码时,抛出异常:java.lang.ArrayIndexOutOfBoundsException: length=128; index=128


内容的提问来源于stack exchange,提问作者Ekatarina El

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 17:42:22