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

发布于 2024-11-27 06:53:10 字数 3136 浏览 1 评论 0原文

我正在尝试在我的 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 am tring to generate a PDF in my android application. I use iText and it generates PDF but only letters in english are shown. I found example code for iText working with unicode. I tried this example code in a simple comsole java application and it worked fine. This is the code:

* --> 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());
     }
  }
}

When I adapted this code for an android activity, it stopped to work:

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 {}
}

The problem is not with the location of the file comic.ttf because if I change the path to a wrong one I get an IOException. The problem is not with the generation of the PDF itself because if I use this code without font1, it generates a PDF file on the SD card but it is without the Unicode characters:

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

What can be the problem ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不回头走下去 2024-12-04 06:53:10

我用过

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);

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

I used

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

and it works. You should check the fonts directory on your Android device to make sure.

For Chinese characters I used

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

Make sure you provide a fallback font in the catch() block, for example:

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

Please let me know if there is a standard way to get the path of the fonts folder.

_蜘蛛 2024-12-04 06:53:10

它还取决于您使用的字体,如果您使用的字体不支持西里尔字母,则不会出现西里尔字母。
例如:如果您使用字体 google google_regular.ttf 那么西里尔字母将不会出现在 pdf 文件中

It also depends on the font you use, if the font you use does not support Cyrillic letters, Cyrillic letters will not appear.
For example: If you use the font google google_regular.ttf then the Cyrillic letters will not appear in the pdf file

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文