Android 中使用 iText 生成的 PDF 中不显示西里尔字母

2024-01-03

我正在尝试在我的 Android 应用程序中生成 PDF。我使用 iText,它生成 PDF,但只显示英文字母。我找到了使用 unicode 的 iText 示例代码。我在一个简单的 comsole java 应用程序中尝试了这个示例代码,它工作得很好。这是代码:

* --> Copyright 2001 by Paulo Soares, Bruno Lowagie <--
public class Chap0903 {
   public static void main(String[] args) {
      System.out.println("Chapter 9 example 3: True Types (embedded)");
      Document document1 = new Document();

      try {
         PdfWriter.getInstance(document1,
           new FileOutputStream("c:\\Chap0903.pdf"));

         BaseFont bfComic = BaseFont.createFont("assets/fonts/comic.ttf",
           BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
         Font font1 = new Font(bfComic, 12);
         String text1 = "This is the quite popular True Type font 'Comic'.";
         String text2 = "Some greek characters: \u0393\u0394\u03b6";
         String text3 = "Some cyrillic characters: \u0418\u044f";

         document1.open();
         document1.add(new Paragraph(text1,font1));
         document1.add(new Paragraph(text2,font1));
         document1.add(new Paragraph(text3,font1)); 
         document1.close();
     }
     catch(DocumentException de) {
        document1.close();
        System.err.println(de.getMessage());
     }
     catch(IOException ioe) {
        document1.close();
        System.err.println(ioe.getMessage());
     }
  }
}

当我为 Android 活动调整此代码时,它停止工作:

public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    String root = Environment.getExternalStorageDirectory().getAbsolutePath();
    Document document1 = new Document();

    try {
        PdfWriter.getInstance(document1,
          new FileOutputStream(root+"/Chap0903.pdf"));

        BaseFont bfComic = BaseFont.createFont("assets/fonts/comic.ttf",
          BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        Font font1 = new Font(bfComic, 12);
        String text1 = "This is the quite popular True Type font 'Comic'.";
        String text2 = "Some greek characters: \u0393\u0394\u03b6";
        String text3 = "Some cyrillic characters: \u0418\u044f";

        document1.open();
        document1.add(new Paragraph(text1,font1));
        document1.add(new Paragraph(text2,font1));
        document1.add(new Paragraph(text3,font1)); 
        document1.close();

        Intent intent = new Intent();
        setResult(RESULT_OK, intent);       
    } 
    catch(DocumentException de) {
        document1.close();
        Intent intent = new Intent();
        setResult(RESULT_CANCELED, intent);        
        System.err.println(de.getMessage());
    }
    catch(IOException ioe) {
        document1.close();
        Intent intent = new Intent();
        setResult(RESULT_CANCELED, intent);
        System.err.println(ioe.getMessage());
        Task.mes(ioe.getMessage());
    } finally {}
}

问题不在于 Comic.ttf 文件的位置,因为如果我将路径更改为错误的路径,则会收到 IOException。问题不在于 PDF 本身的生成,因为如果我使用此代码而不使用font1,它会在 SD 卡上生成一个 PDF 文件,但它不包含 Unicode 字符:

document1.add(new Paragraph(text1));
document1.add(new Paragraph(text2));
document1.add(new Paragraph(text3)); 

可能是什么问题?


I used

BaseFont bfComic = BaseFont.createFont("/system/fonts/Comic.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

它有效。您应该检查 Android 设备上的字体目录来确定。

对于我使用的汉字

BaseFont bfSans = BaseFont.createFont("/system/fonts/DroidSansFallback.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

确保在 catch() 块中提供后备字体,例如:

font = new Font(Font.FontFamily.HELVETICA, 24, Font.NORMAL, BaseColor.BLACK);

请告诉我是否有获取字体文件夹路径的标准方法。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android 中使用 iText 生成的 PDF 中不显示西里尔字母 的相关文章

随机推荐