首页 > 解决方案 > 在 itext 7 中无法识别字体类型

问题描述

URL font_path = Thread.currentThread().getContextClassLoader().getResource("font/font1.ttf");
byte[] b = PdfEncodings.convertToBytes(String.valueOf(font_path), PdfEncodings.WINANSI);
PdfFont font = PdfFontFactory.createFont(b, PdfEncodings.WINANSI, true);

标签: javaitext7

解决方案


正如我在评论中解释的 mkl ,您在代码中存在与 iText 无关的逻辑错误 - 您应该首先正确获取资源的字节。

这是有关如何执行此操作的示例:

URL font_path = Thread.currentThread().getContextClassLoader().getResource("font/font1.ttf");
try (InputStream is = font_path.openStream()) {
    byte[] bytes = StreamUtil.inputStreamToArray(is);
    PdfFont font = PdfFontFactory.createFont(bytes, PdfEncodings.WINANSI, EmbeddingStrategy.PREFER_EMBEDDED);
}

推荐阅读