问题描述
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);
}