iText pdf not displaying Chinese characters when using NOTO fonts or Source Hans

前端 未结 1 1863
南方客
南方客 2020-12-03 19:35

I am trying to use NOTO fonts (https://www.google.com/get/noto/) to display Chinese characters. Here is my sample code,a modified sample code from iText.

pub         


        
相关标签:
1条回答
  • 2020-12-03 20:07

    Clearly you are using the wrong font. I have downloaded the fonts from the link you posted. You are using NotoSerif-Bold.ttf, a font that does not support Chinese. However, the ZIP file also contains fonts with CJK in the font name. As described on the site you refer to, CJK stands for Chinese, Japanese and Korean. Use one of those CJK fonts and you'll be able to product Chinese text in your PDF.

    Take a look at the NotoExample in which I use one of the fonts from the ZIP file you refer to. It creates a PDF that looks like this:

    enter image description here

    This is the code I used:

    public static final String FONT = "resources/fonts/NotoSansCJKsc-Regular.otf";
    public static final String TEXT = "These are the protagonists in 'Hero', a movie by Zhang Yimou:\n"
        + "\u7121\u540d (Nameless), \u6b98\u528d (Broken Sword), "
        + "\u98db\u96ea (Flying Snow), \u5982\u6708 (Moon), "
        + "\u79e6\u738b (the King), and \u9577\u7a7a (Sky).";
    public static final String CHINESE = "\u5341\u950a\u57cb\u4f0f";
    public static final String JAPANESE = "\u8ab0\u3082\u77e5\u3089\u306a\u3044";
    public static final String KOREAN = "\ube48\uc9d1";
    
    public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(DEST));
        document.open();
        Font font = FontFactory.getFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        Paragraph p = new Paragraph(TEXT, font);
        document.add(p);
        document.add(new Paragraph(CHINESE, font));
        document.add(new Paragraph(JAPANESE, font));
        document.add(new Paragraph(KOREAN, font));
        document.close();
    }
    

    You claim that Adobe Reader XI doesn't show the Chinese glyphs, but instead shows a "Cannot extract the embedded Font" message. I can not reproduce this [*]. I have even used Preflight in Adobe Acrobat as indicated here, but no errors were found:

    enter image description here

    [*] Update: this problem can be reproduced if you use iText 4.2.x, a version that was released by somebody unknown to iText Group NV. Please use iText versions higher than 5 only.

    0 讨论(0)
提交回复
热议问题