java - 在 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);
解决方案
正如我在评论中解释的 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);
}
推荐阅读
- javascript - Web 蓝牙 API 在一个网页中以相同的事件优先级连接多个设备
- scala - 在 Spark scala 中创建从时间戳值开始并以时间戳值递增一小时结束的 df
- c# - Gridview.Sort 什么都不排序?
- android - 如何滚动 Recycler 视图并从每张卡片中收集文本?
- macos - AdaCore gnatstudio 构建但不会运行
- java - 在 Spring MVC 应用程序中使用 Jquery 发送多个参数 Ajax 字符串化请求时,不存在必需的字符串参数“querycontent”
- python - Numpy array_equal 和 float 精确相等检查
- html - 如何在移动浏览器上以全分辨率显示图像?
- xamarin.forms - 使用 xam.plugin.media 选择多个图像
- c++ - C ++密码问题