iText Output Missing Unicode Characters

I am using iText to generate PDFs out of java from java.awt.print.Printables. It all works fine, just until one point:

The printables often contain various unicode characters (as they just do, I guess :-) ), for which I am using a composite Font with one extra fallback font in lib/fonts/fallback. In Java it all works fine with this solution.

When exporting it to PDF, I am already using my own FontMapper-Implementation:

public class PdfFontMapper implements FontMapper {

public BaseFont awtToPdf(Font font) {

    try {
         if(!font.getFamily().equalsIgnoreCase("Trebuchet MS")) {
                return getBaseFontFromFile(SunFontManager.getInstance().getPlatformFontPath( true ), "ARIALUNI.ttf");
         }
         else {
                return getBaseFontFromFile(SunFontManager.getInstance().getPlatformFontPath( true ), "Trebuchet MS.ttf");
         }

    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

public Font pdfToAwt(BaseFont baseFont, int size) {

    throw new UnsupportedOperationException();
}

private BaseFont getBaseFontFromFile(String directory, String filename)
        throws Exception {

    InputStream is = null;
    try {

        final ClassLoader cl = Thread.currentThread()
                .getContextClassLoader();
        is = cl.getResourceAsStream(filename);
        // is = IMappingApplication.class.getResourceAsStream(directory +
        // filename);

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[] buf = new byte[1024];
        while (true) {
            int size = is.read(buf);
            if (size < 0) {
                break;
            }
            bos.write(buf, 0, size);
        }
        buf = bos.toByteArray();
        BaseFont bf = BaseFont.createFont(filename, BaseFont.IDENTITY_H,
                BaseFont.NOT_EMBEDDED, BaseFont.NOT_CACHED, buf, null);
        return bf;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    } finally {
        if (is != null) {
            is.close();
        }
    }
}
}

, special thanks to GJ Schouten.

But unfortunately, characters not included in my base font, "Trebuchet MS", don't get exported - they just go missing. Interestingly, if I only add one Font to the Mapper, "Arial Unicode", it works pretty fine. Well, I could of course just use one font then. But I hope there is a better solution.

And by the way: The glyphs (Emojis) from my fallback-library always get rendered well. But I didn't get a solution to run in which I could only use a physical font + fallback-fonts in fonts/fallback.

Does anyone have an idea how to solve this? Btw.: I already tried embedding the fonts with the PDF, but that didn't work (and I would think it's not necessary, since I am using pretty common fonts).

Greetings, Andy

链接地址: http://www.djcxy.com/p/65458.html

上一篇: 获取范围的开始和结束偏移量相对于其父容器

下一篇: iText输出缺少Unicode字符