正确jar包引入:(这里的版本号无所谓,下同)

<dependency><groupId>com.itextpdf</groupId><artifactId>itext-asian</artifactId><version>5.2.0</version>
</dependency>
<dependency><groupId>com.itextpdf</groupId><artifactId>itextpdf</artifactId><version>5.4.3</version>
</dependency>

不要引入的jar

<dependency><groupId>com.lowagie</groupId><artifactId>itext</artifactId><version>2.1.7</version>
</dependency>

关键点在于:使用com.lowagie.text.pdf.BaseFont还是com.itextpdf.text.pdf.BaseFont

BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);

com.lowagie.text.pdf.BaseFont

这里我们先看一下com.lowagie.text.pdf.BaseFont的源码,为何会报错:Font ‘STSong-Light’ with ‘UniGB-UCS2-H’

public static BaseFont createFont(String var0, String var1, boolean var2) throws DocumentException, IOException {return createFont(var0, var1, var2, true, (byte[])null, (byte[])null, false);
}public static BaseFont createFont(String var0, String var1, boolean var2, boolean var3) throws DocumentException, IOException {return createFont(var0, var1, var2, true, (byte[])null, (byte[])null, var3);
}public static BaseFont createFont(String var0, String var1, boolean var2, boolean var3, byte[] var4, byte[] var5) throws DocumentException, IOException {return createFont(var0, var1, var2, var3, var4, var5, false);
}public static BaseFont createFont(String var0, String var1, boolean var2, boolean var3, byte[] var4, byte[] var5, boolean var6) throws DocumentException, IOException {return createFont(var0, var1, var2, var3, var4, var5, false, false);
}public static BaseFont createFont(String var0, String var1, boolean var2, boolean var3, byte[] var4, byte[] var5, boolean var6, boolean var7) throws DocumentException, IOException {String var8 = getBaseName(var0);var1 = normalizeEncoding(var1);//var9=false,详解如下boolean var9 = BuiltinFonts14.containsKey(var0);//看这里//var10=false,详解如下boolean var10 = var9 ? false : CJKFont.isCJKFont(var8, var1);//看这里if (!var9 && !var10) {if (var1.equals("Identity-H") || var1.equals("Identity-V")) {var2 = true;}} else {var2 = false;}....//if (!var9 && !var0.toLowerCase().endsWith(".afm") && !var0.toLowerCase().endsWith(".pfm")) {if (!var8.toLowerCase().endsWith(".ttf") && !var8.toLowerCase().endsWith(".otf") && var8.toLowerCase().indexOf(".ttc,") <= 0) {if (!var10) {if (var6) {return null;}//进入到这里直接跑异常throw new DocumentException("Font '" + var0 + "' with '" + var1 + "' is not recognized.");}....
}

这里我们注意下BuiltinFonts14.containsKey(var0);

protected static final HashMap BuiltinFonts14 = new HashMap();static {BuiltinFonts14.put("Courier", PdfName.COURIER);BuiltinFonts14.put("Courier-Bold", PdfName.COURIER_BOLD);BuiltinFonts14.put("Courier-BoldOblique", PdfName.COURIER_BOLDOBLIQUE);BuiltinFonts14.put("Courier-Oblique", PdfName.COURIER_OBLIQUE);BuiltinFonts14.put("Helvetica", PdfName.HELVETICA);BuiltinFonts14.put("Helvetica-Bold", PdfName.HELVETICA_BOLD);BuiltinFonts14.put("Helvetica-BoldOblique", PdfName.HELVETICA_BOLDOBLIQUE);BuiltinFonts14.put("Helvetica-Oblique", PdfName.HELVETICA_OBLIQUE);BuiltinFonts14.put("Symbol", PdfName.SYMBOL);BuiltinFonts14.put("Times-Roman", PdfName.TIMES_ROMAN);BuiltinFonts14.put("Times-Bold", PdfName.TIMES_BOLD);BuiltinFonts14.put("Times-BoldItalic", PdfName.TIMES_BOLDITALIC);BuiltinFonts14.put("Times-Italic", PdfName.TIMES_ITALIC);BuiltinFonts14.put("ZapfDingbats", PdfName.ZAPFDINGBATS);
}

所以BuiltinFonts14.containsKey(“STSong-Light”)返回false
注意CJKFont.isCJKFont(var8, var1)这个方法;我们进CJKFont看一下

public static boolean isCJKFont(String var0, String var1) {loadProperties();//看这里String var2 = cjkFonts.getProperty(var0);return var2 != null && (var1.equals("Identity-H") || var1.equals("Identity-V") || var2.indexOf("_" + var1 + "_") >= 0);
}
private static void loadProperties() {if (!propertiesLoaded) {Hashtable var0 = allFonts;synchronized(allFonts) {if (!propertiesLoaded) {try {InputStream var1 = getResourceStream("com/lowagie/text/pdf/fonts/cjkfonts.properties");cjkFonts.load(var1);var1.close();var1 = getResourceStream("com/lowagie/text/pdf/fonts/cjkencodings.properties");cjkEncodings.load(var1);var1.close();} catch (Exception var3) {cjkFonts = new Properties();cjkEncodings = new Properties();}propertiesLoaded = true;}}}
}

CJKFont会先加载com/lowagie/text/pdf/fonts下的并不存在的cjkfonts.properties和cjkencodings.properties文件,然后再获取var0(即"STSong-Light")的值为null,所以isCJKFont方法返回false
再回上面查看代码可知,直接抛出异常

com.itextpdf.text.pdf.BaseFont

下面我们看一下com.itextpdf.text.pdf.BaseFont的源码

public static BaseFont createFont(String name, String encoding, boolean embedded, boolean cached, byte[] ttfAfm, byte[] pfb, boolean noThrow, boolean forceRead) throws DocumentException, IOException {String nameBase = getBaseName(name);//nameBase值为"S"encoding = normalizeEncoding(encoding);//isBuiltinFonts14=falseboolean isBuiltinFonts14 = BuiltinFonts14.containsKey(name);//看这里//isCJKFont=trueboolean isCJKFont = isBuiltinFonts14 ? false : CJKFont.isCJKFont(nameBase, encoding);//看这里if (!isBuiltinFonts14 && !isCJKFont) {if (encoding.equals("Identity-H") || encoding.equals("Identity-V")) {embedded = true;}} else {embedded = false;}....if (!isBuiltinFonts14 && !name.toLowerCase().endsWith(".afm") && !name.toLowerCase().endsWith(".pfm")) {if (!nameBase.toLowerCase().endsWith(".ttf") && !nameBase.toLowerCase().endsWith(".otf") && nameBase.toLowerCase().indexOf(".ttc,") <= 0) {if (!isCJKFont) {//isCJKFont=trueif (noThrow) {return null;}throw new DocumentException(MessageLocalization.getComposedMessage("font.1.with.2.is.not.recognized", new Object[]{name, encoding}));}fontBuilt = new CJKFont(name, encoding, embedded);//返回结果值........return (BaseFont)fontBuilt;
}

BuiltinFonts14.containsKey(name);与com.lowagie.text.pdf.BaseFont中逻辑相同,这里不再讲述了。

public static boolean isCJKFont(String fontName, String enc) {loadProperties();//看这里if (!registryNames.containsKey("fonts")) {return false;} else if (!((Set)registryNames.get("fonts")).contains(fontName)) {return false;} else if (!enc.equals("Identity-H") && !enc.equals("Identity-V")) {String registry = (String)((HashMap)allFonts.get(fontName)).get("Registry");Set<String> encodings = (Set)registryNames.get(registry);return encodings != null && encodings.contains(enc);} else {return true;}
}private static void loadProperties() {if (!propertiesLoaded) {HashMap var0 = allFonts;synchronized(allFonts) {if (!propertiesLoaded) {try {loadRegistry();//看这里Iterator i$ = ((Set)registryNames.get("fonts")).iterator();while(i$.hasNext()) {String font = (String)i$.next();allFonts.put(font, readFontProperties(font));}} catch (Exception var4) {;}propertiesLoaded = true;}}}
}private static void loadRegistry() throws IOException {//加载cjk_registry.properties文件InputStream is = StreamUtil.getResourceStream("com/itextpdf/text/pdf/fonts/cmaps/cjk_registry.properties");Properties p = new Properties();p.load(is);is.close();Iterator i$ = p.keySet().iterator();while(i$.hasNext()) {Object key = i$.next();String value = p.getProperty((String)key);String[] sp = value.split(" ");Set<String> hs = new HashSet();String[] arr$ = sp;int len$ = sp.length;for(int i$ = 0; i$ < len$; ++i$) {String s = arr$[i$];if (s.length() > 0) {hs.add(s);}}registryNames.put((String)key, hs);}}

cjk_registry.properties如下

Adobe_Japan1=78-EUC-H 78-EUC-V 78-H 78-RKSJ-H 78-RKSJ-V 78-V 78ms-RKSJ-H 78ms-RKSJ-V 83pv-RKSJ-H 90ms-RKSJ-H 90ms-RKSJ-V 90msp-RKSJ-H 90msp-RKSJ-V 90pv-RKSJ-H 90pv-RKSJ-V Add-H Add-RKSJ-H Add-RKSJ-V Add-V Adobe-Japan1-0 Adobe-Japan1-1 Adobe-Japan1-2 Adobe-Japan1-3 Adobe-Japan1-4 Adobe-Japan1-5 Adobe-Japan1-6 EUC-H EUC-V Ext-H Ext-RKSJ-H Ext-RKSJ-V Ext-V H Hankaku Hiragana Katakana NWP-H NWP-V RKSJ-H RKSJ-V Roman UniJIS-UCS2-H UniJIS-UCS2-HW-H UniJIS-UCS2-HW-V UniJIS-UCS2-V UniJIS-UTF16-H UniJIS-UTF16-V UniJIS-UTF32-H UniJIS-UTF32-V UniJIS-UTF8-H UniJIS-UTF8-V UniJIS2004-UTF16-H UniJIS2004-UTF16-V UniJIS2004-UTF32-H UniJIS2004-UTF32-V UniJIS2004-UTF8-H UniJIS2004-UTF8-V UniJISPro-UCS2-HW-V UniJISPro-UCS2-V UniJISPro-UTF8-V UniJISX0213-UTF32-H UniJISX0213-UTF32-V UniJISX02132004-UTF32-H UniJISX02132004-UTF32-V V WP-Symbol
Adobe_Korea1=Adobe-Korea1-0 Adobe-Korea1-1 Adobe-Korea1-2 KSC-EUC-H KSC-EUC-V KSC-H KSC-Johab-H KSC-Johab-V KSC-V KSCms-UHC-H KSCms-UHC-HW-H KSCms-UHC-HW-V KSCms-UHC-V KSCpc-EUC-H KSCpc-EUC-V UniKS-UCS2-H UniKS-UCS2-V UniKS-UTF16-H UniKS-UTF16-V UniKS-UTF32-H UniKS-UTF32-V UniKS-UTF8-H UniKS-UTF8-V
Adobe_GB1=Adobe-GB1-0 Adobe-GB1-1 Adobe-GB1-2 Adobe-GB1-3 Adobe-GB1-4 Adobe-GB1-5 GB-EUC-H GB-EUC-V GB-H GB-V GBK-EUC-H GBK-EUC-V GBK2K-H GBK2K-V GBKp-EUC-H GBKp-EUC-V GBpc-EUC-H GBpc-EUC-V GBT-EUC-H GBT-EUC-V GBT-H GBT-V GBTpc-EUC-H GBTpc-EUC-V UniGB-UCS2-H UniGB-UCS2-V UniGB-UTF16-H UniGB-UTF16-V UniGB-UTF32-H UniGB-UTF32-V UniGB-UTF8-H UniGB-UTF8-V
Adobe_CNS1=Adobe-CNS1-0 Adobe-CNS1-1 Adobe-CNS1-2 Adobe-CNS1-3 Adobe-CNS1-4 Adobe-CNS1-5 Adobe-CNS1-6 B5-H B5-V B5pc-H B5pc-V CNS-EUC-H CNS-EUC-V CNS1-H CNS1-V CNS2-H CNS2-V ETen-B5-H ETen-B5-V ETenms-B5-H ETenms-B5-V ETHK-B5-H ETHK-B5-V HKdla-B5-H HKdla-B5-V HKdlb-B5-H HKdlb-B5-V HKgccs-B5-H HKgccs-B5-V HKm314-B5-H HKm314-B5-V HKm471-B5-H HKm471-B5-V HKscs-B5-H HKscs-B5-V UniCNS-UCS2-H UniCNS-UCS2-V UniCNS-UTF16-H UniCNS-UTF16-V UniCNS-UTF32-H UniCNS-UTF32-V UniCNS-UTF8-H UniCNS-UTF8-V
Adobe_Japan1_Uni=UniJIS-UTF16-H UniJIS-UTF16-V
Adobe_Korea1_Uni=UniKS-UTF16-H UniKS-UTF16-V
Adobe_GB1_Uni=UniGB-UTF16-H UniGB-UTF16-V
Adobe_CNS1_Uni=UniCNS-UTF16-H UniCNS-UTF16-V
fonts=HeiseiMin-W3 HeiseiKakuGo-W5 KozMinPro-Regular STSong-Light STSongStd-Light MHei-Medium MSung-Light MSungStd-Light HYGoThic-Medium HYSMyeongJo-Medium HYSMyeongJoStd-Medium

OK,到此为止,通过查看源码可知com.lowagie.text.pdf.BaseFont去加载不存在的文件而获取不到"STSong-Light"和"UniGB-UCS2-H"配置值而抛出异常;
而com.itextpdf.text.pdf.BaseFont返回的结果为
(BaseFont)new CJKFont(name, encoding, embedded);
时间紧迫,文笔不好。-_-

问题解决:com.lowagie.text.DocumentException: Font 'STSong-Light' with 'UniGB-UCS2-H'相关推荐

  1. 遇到 com.lowagie.text.DocumentException: Font 'STSong-Light' with 'UniGB-UCS2-H' is not recognized.

    在利用iText.jar和iTextAsian.jar生成pdf文件时有时遇到了 Exception in thread "main" com.lowagie.text.Docum ...

  2. 使用JasperReport 引擎PDF报表,因为iText版本升级,由原来的包名com.lowagie.text.pdf.fonts转化为com.itextpdf.text.pdf.fonts

    使用JasperReport 引擎PDF报表,因为iText版本升级,由原来的包名com.lowagie.text.pdf.fonts转化为com.itextpdf.text.pdf.fonts,如果 ...

  3. 关于com.lowagie.text包的报错问题

    关于com.lowagie.text包的报错问题 接手了一个maven项目,导入之后发现一直报错 提示我找不到这个包,在右侧的maven projects一栏中找了一下,也确实没有com.lowagi ...

  4. 解决Java.lang.NoClassDefFoundError:com/lowagie/text/Elemen的问题

    正在写用itext导出word的项目,在pom.xml里写了以下代码下载itext 2-1-7.jar. <dependency><groupId>com.lowagie< ...

  5. 异常记录 之 nested exception is java.lang.NoClassDefFoundError: com/lowagie/text/pdf/PdfContentByte

    nested exception is java.lang.NoClassDefFoundError: com/lowagie/text/pdf/PdfContentByte 在MVC 集成jaspe ...

  6. java 使用itext导出PDF文件,中文不显示问题解决

    之前写的java 使用itext 导出pdf 发现有个问题,在今天使用的时候,发现一个问题,就是当单元格中写中文的时候,导出来的pdf中文不显示. java 使用itext导出PDF文件,图片文字左右 ...

  7. Java 使用jacob实现各类办公文档(ppt,Excel,word,text,imge)转换成PDF

    //代码中都有注释,使用注解的地方大家可以略过 package com.frank.demo.file.common.util; import java.io.ByteArrayOutputStrea ...

  8. java 生成 pdf html转pdf 支持 中文 自定义模板

    java 生成pdf DEMO 开发过程中遇到的坑 /** * 切记 css 要定义在head 里,否则解析失败 * css 要定义字体 * 字体中英文 https://www.cnblogs.com ...

  9. iText和flying saucer结合生成pdf的技术

    原博文地址 http://blog.csdn.net/shanliangliuxing/article/details/6833471 下面是我自己利用flying saucer技术生成pdf文档的实 ...

  10. Spring Boot Freemark HTML 生成 PDF、生成水印Logo、docx文件生成PDF,Jar包运行可读取模板文件、字体文件

    用于通过模板生成PDF,在项目中生成个人授权协议函.个人电子保单.流水报表,数据报表等,将HTML静态模板写出来后,将数据替换成动态数据即可. <!-- html2pdf --> < ...

最新文章

  1. java wait abc_java----wait/notify
  2. 【官宣·第一弹】2021中国肠道大会7条重要消息
  3. 《重构-改善既有代码设计》读书笔记-重构篇
  4. Deploy One Project on Heroku (Week IV)
  5. 亿级搜索系统的基石,如何保障实时数据质量?
  6. P2770 航空路线问题(网络流)
  7. HDU 6178 Monkeys
  8. 谷歌大脑2017总结(Jeff Dean执笔,干货满满,值得收藏)
  9. hive -e执行命令报错
  10. web项目开发的基本流程
  11. xgboost与LightGBM的区别
  12. SqlLoader(Sqlldr) 的用法
  13. 步进电机加减速——梯形算法
  14. jsonrpc(jsonrpc4j)demo
  15. java后台数据传到前台的流程_java serlve后台数据传到前台
  16. 解决Eth0网卡不存在的情况_wuli大世界_新浪博客
  17. Elasticsearch:Hadoop 大数据集成 (Hadoop => Elasticsearch)
  18. 2014Android Demo源码 文件夹 PATH 列表
  19. 大工14春 计算机文化基础 在线测试,大工14春《计算机文化基础》在线测试1
  20. canvas 绘制图片

热门文章

  1. 计算机应用历年高考真题,春季高考历年真题-2013年天津市春季高考计算机试卷...
  2. 计蒜客-英文金曲大赛
  3. IntelliJ IDEA 创建 Vue工程
  4. http权威指南完整版
  5. Myeclipse2017破解:成功解决me Trial expired 0 days ago mgeclipse It's now time to buy the best IDE for yo
  6. 解析bt种子下载 java_使用Java解析Torrent文件(BT种子),基于使用Eclipse ECF中的org.eclipse.bittorrent方案...
  7. windows10镜像下载
  8. 资深大学老师告诉你:嵌入式、单片机开发必备软件有哪些
  9. nbu mysql linux备份软件,NBU备份linux/aix/unix下的db2数据库配置
  10. 金万维异速联服务器重装,金万维异速联客户端常见错误提示及解决办法