使用iText7 .NET Web API加载项目字体报错:Bad subtable format identifier:4
解决iText加载Calibri字体报错
Bad subtable format identifier: 4 报错原因
这个错误源于iText对Calibri字体中部分CMAP子表(格式4)的兼容性问题,这类微软字体的内部结构可能包含旧版本iText无法处理的子表格式。
可行解决方案
1. 升级iText到最新版本
旧版本iText对部分TrueType字体的子表支持不完善,升级到最新的iText 7.x版本,多数情况下能解决这类兼容性问题。
2. 更换字体加载方式
改用FontProgramFactory先加载字体程序,再创建PdfFont,绕开直接调用PdfFontFactory.CreateFont时的子表解析问题:
// 加载字体程序 FontProgram calibriLight = FontProgramFactory.CreateFont("Fonts/CalibriLight.ttf"); FontProgram calibriItalic = FontProgramFactory.CreateFont("Fonts/CalibriItalic.ttf"); FontProgram calibriBold = FontProgramFactory.CreateFont("Fonts/CalibriBold.ttf"); FontProgram calibriBoldItalic = FontProgramFactory.CreateFont("Fonts/CalibriBoldItalic.ttf"); // 创建PdfFont PdfFont cFont = PdfFontFactory.CreateFont(calibriLight, PdfEncodings.IDENTITY_H, PdfFontFactory.EmbeddingStrategy.PREFER_EMBEDDED); PdfFont cFontI = PdfFontFactory.CreateFont(calibriItalic, PdfEncodings.IDENTITY_H, PdfFontFactory.EmbeddingStrategy.PREFER_EMBEDDED); PdfFont cFontB = PdfFontFactory.CreateFont(calibriBold, PdfEncodings.IDENTITY_H, PdfFontFactory.EmbeddingStrategy.PREFER_EMBEDDED); PdfFont cFontBI = PdfFontFactory.CreateFont(calibriBoldItalic, PdfEncodings.IDENTITY_H, PdfFontFactory.EmbeddingStrategy.PREFER_EMBEDDED);
3. 替换为兼容的Unicode字体
如果上述方法无效,可换用开源免费、对iText兼容性更好的全语言字体,比如Noto Sans系列(支持几乎所有Unicode字符),下载对应TTF文件放入Fonts文件夹后,直接用原有代码加载即可:
PdfFont cFont = PdfFontFactory.CreateFont("Fonts/NotoSans-Light.ttf", PdfEncodings.IDENTITY_H, PdfFontFactory.EmbeddingStrategy.PREFER_EMBEDDED);
4. 确保字体文件完整有效
从Windows系统字体目录(C:\Windows\Fonts)重新复制原始Calibri字体:右键点击对应字体选择“复制”,再粘贴到项目的Fonts文件夹,避免复制快捷方式或损坏的字体文件。
内容的提问来源于stack exchange,提问作者Vladimir Platun
相关产品推荐
相关产品推荐

