Itext PDF无法正确显示缅甸Unicode字体

问题描述 投票:4回答:2

Itext 5无法在为Myanmar Unicode fonts生成的pdf文件中正确显示。

Itext版本:5.5.13.1

期望结果:ဂဃနဏဖတ်ခဲ့သည်。]

实际结果

enter image description here

Google Drive Link用于生成PDF。

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

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

    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文件中显示错误。

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

现在,我正在使用肮脏的方式来查看字体可读。我将所有unicode字符串转换为“ Zawgyi字体” (这是另一个缅甸字体和we should never use this。]

,并嵌入到pdf中。这不是一个好的解决方案,我们不能保证所有的unicode都可以正确地转换为Zawgyi-One字体字符串,而且我也不想将unicode文本转换为非标准文本。这就是为什么我不想使用这种方式。

Itext 5在生成的缅甸Unicode字体的pdf文件中无法正确显示。 itext版本:5.5.13.1预期结果:သီဟိုဠ်မှ...

java pdf itext
2个回答
0
投票

(全部披露:我为iText软件工作。)


-2
投票

我也面临同样的问题。但是我在iText上使用了百里香。我使用该语言(不是unicode)的ttf字体包,并使用转换器将unicode转换为我的语言,并将其作为普通字符串附加到PDF中。它就像一个魅力。如果您有可能使用百里香叶,请尝试这种方法。

© www.soinside.com 2019 - 2024. All rights reserved.