itext PDF无法正确显示缅甸Unicode字体 [英] Itext PDF do not display correctly Myanmar Unicode Font

查看:597
本文介绍了itext PDF无法正确显示缅甸Unicode字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于缅甸Unicode字体,生成的pdf文件中的itext 5无法正确显示.

Itext 5 do not display correctly at generated pdf file for Myanmar Unicode fonts.

itext版本:5.5.13.1

Itext version : 5.5.13.1

预期结果::

实际结果:

Google云端硬盘链接,用于生成的PDF.

Google Drive Link for generated PDF.

我的测试字符串类似于英语中的快速的棕色狐狸跳过懒惰的狗".它包含大多数缅甸字母.

My test string is similar with "The quick brown fox jump over the lazy dog" in English. It contains most of Myanmar alphabets.

我以前用于pdf以上产品的Java程序

Java program that I used to product above pdf

    String fileName = "sample.pdf";
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        Document doc = new Document();
        PdfWriter writer = PdfWriter.getInstance(doc, baos);
        writer.setCloseStream(false);

        BaseFont unicode = BaseFont.createFont("/fonts/NotoSansMyanmar-Regular.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        Font myanmarUniCodeFont = new Font(unicode, 11, Font.NORMAL, BaseColor.BLACK);
        Rectangle pageSize = new Rectangle(PageSize.A4);
        doc.setPageSize(pageSize);
        doc.open();
        String textStr = "သီဟိုဠ်မှ ဉာဏ်ကြီးရှင်သည်အာယုဝဎ္ဍနဆေးညွှန်းစာကို ဇလွန်ဈေးဘေးဗာဒံပင်ထက် အဓိဋ္ဌာန်လျက် ဂဃနဏဖတ်ခဲ့သည်။";
        doc.add(new Paragraph(textStr, myanmarUniCodeFont));
        doc.close();
    }
    catch (Exception e) {
        e.printStackTrace();
    }

    response.setCharacterEncoding(StandardCharsets.UTF_8.name());
    response.setHeader("Cache-Control", "no-cache,no-store,max-age=0");
    response.setHeader("Pragma", "No-cache");
    response.setHeader("Content-Disposition", "inline; filename=" + fileName);
    response.setContentType("application/pdf");
    response.setContentLength(baos.size());
    OutputStream os = response.getOutputStream();
    baos.writeTo(os);
    os.flush();
    os.close();
    baos.close();

输出的文本是正确的(您可以将其复制并粘贴到记事本等文本编辑器中,然后查看结果),但在pdf文件中显示错误.

Ouput texts are correct (you can copy and paste into a text editors like Notepad++ and see the result) but wrong display at pdf file.

如何使用itext-pdf-5正确显示缅甸Unicode字体?

What should I do to display correctly Myanmar Unicode Font by using itext-pdf-5 ?

现在,我正在使用肮脏的方式来查看字体的可读性.我将所有unicode字符串都转换为"Zawgyi字体" (这是另一种缅甸字体,并且

Now I'm using dirty way to see the fonts readable. I converted all unicode strings to "Zawgyi Font" (This is another Myanmar font and we should never use this.) and embeded into pdf. This is not good solution and we can't promise all unicodes are correctly converted to Zawgyi-One font string and I don't want to convert unicode texts to non-standard texts. That's why I don't want to use this way.

使用Itext编辑了有关ZawGyi字体的

某些文本也无法使用itext正确显示.例如:သိန်နီ ဂွ

Some texts also do not render correctly with itext . Eg : သိန္နီ ၊ ဂွ

推荐答案

(完整披露:我为iText软件工作.)

(Full disclosure: I work for iText Software.)

iText 5不支持缅甸书写系统的基于Unicode的正确处理.尽管iText 5具有针对阿拉伯语的特定实现,但是其字体基础结构的固有局限性阻止了对其他各种书写系统所需的字体功能的支持.

iText 5 does not support proper Unicode based processing of the Myanmar writing system. Although iText 5 has a specific implementation for Arabic, the inherent limitations of its font infrastructure prevent support for font features that are needed for various other writing systems.

iText 7在此方面进行了改进,提供了新的字体实现和一个可选模块(pdfCalligraph,不是开放源代码),以支持不同的书写系统.但是,缅甸(尚)不受支持.

iText 7 improves on this with a new font implementation and an optional module (pdfCalligraph, not open source) to support different writing systems. However, Myanmar is not (yet) supported.

相应的iText 7代码如下:

The corresponding iText 7 code looks like this:

PdfWriter writer = new PdfWriter(baos);
PdfDocument pdfdoc = new PdfDocument(writer);
Document doc = new Document(pdfdoc);

PdfFont f = PdfFontFactory.createFont("/fonts/NotoSansMyanmar-Regular.ttf",
    PdfEncodings.IDENTITY_H, true);

String textStr =
    "သီဟိုဠ်မှ ဉာဏ်ကြီးရှင်သည်အာယုဝဎ္ဍနဆေးညွှန်းစာကို ဇလွန်ဈေးဘေးဗာဒံပင်ထက် အဓိဋ္ဌာန်လျက် ဂဃနဏဖတ်ခဲ့သည်။";
// Explicit writing system
//doc.add(new Paragraph(textStr).setFont(f).setFontScript(Character.UnicodeScript.MYANMAR));
// Rely on autodetection
doc.add(new Paragraph(textStr).setFont(f));
doc.close();

无论是否使用pdfCalligraph,渲染仍然是错误的:

Regardless whether pdfCalligraph is used or not, the rendering is still wrong:

如果您可以选择使用商业许可,请提交此功能请求.仍在积极添加其他书写系统.如果不是这样,恐怕iText无法做到这一点,您将不得不寻找另一种解决方案.

If a commercial license is an option for you, please submit this feature request. Additional writing systems are still actively added. If not, I'm afraid this will not be possible with iText and you'll have to find another solution.

这篇关于itext PDF无法正确显示缅甸Unicode字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆