【Exception】解决PDF添加水印报错: Font 'STSong-Light' with 'UniGB-UCS2-H' is not recognized
一、问题描述
1、在使用 iText.jar 给PDF文件添加水印的时候,遇到错误: Font 'STSong-Light' with 'UniGB-UCS2-H' is not recognized , 不能识别到对应的字体库。
2、pom.xml 如下:
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext</artifactId>
<version>2.1.7</version>
</dependency>
二、代码展示
package com.itext.itext_test;
import java.awt.Color;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfGState;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;
/**
* description: 演示使用 itext-2.1.7.jar 抛出异常
* @version v1.0
* @author w
* @date 2018年11月22日下午9:22:21
**/
public class WatermarkTest {
public static void setWatermark(String pdfPath, String targetPath,String watermarkContent, float opacity)throws IOException, DocumentException {
PdfReader pr = new PdfReader(new FileInputStream(new File(pdfPath)));
OutputStream outPutStream =new FileOutputStream(new File(targetPath));
int pageSize = pr.getNumberOfPages();
PdfStamper stam = new PdfStamper(pr, outPutStream);
PdfContentByte content = null;
// 设置字体
BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
PdfGState gs = new PdfGState();
// 设置透明度(填充不透明度)
gs.setFillOpacity(opacity);
for (int i = 1; i <= pageSize; i++) {
content = stam.getOverContent(i);// 在内容上方加水印
content.setGState(gs);
content.beginText();
content.setColorFill(new Color(220, 20, 60));
content.setFontAndSize(baseFont, 50);// 设置字体的大小
content.setTextMatrix(70, 200);
content.showTextAligned(Element.ALIGN_CENTER, watermarkContent, 100, 750, 45);// 宽,高,斜度
content.showTextAligned(Element.ALIGN_CENTER, watermarkContent, 250, 650, 45);
content.showTextAligned(Element.ALIGN_CENTER, watermarkContent, 350, 550, 45);
content.showTextAligned(Element.ALIGN_CENTER, watermarkContent, 200, 250, 45);
content.showTextAligned(Element.ALIGN_CENTER, watermarkContent, 350, 150, 45);
content.showTextAligned(Element.ALIGN_CENTER, watermarkContent, 500, 350, 45);
content.endText();
}
stam.close();
}
}
三、问题解决
1、网上这个问题已经有好多解决方案了,更换 itext 的maven坐标到最新的即可。
2、如下,
<!-- https://mvnrepository.com/artifact/com.itextpdf/itextpdf -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.itextpdf/itext-asian -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
3、将代码中 com.lowagie.text.* 的包,统一替换为: com.itextpdf.text.* 的包,即可解决。
四、问题追踪
1、在 maven repository 搜索,itext , 得到一个最新的版本 itext-1.3.1 ,如下图:
2、告诉我们这个组件被移动到: com.lowagie: https://mvnrepository.com/artifact/com.lowagie/itext 如下图:
3、又一次告诉我们这个组件移动到: com.itextpdf ,然后就可以看到最新的maven依赖。
4、添加了上面的 maven 依赖后,依旧抛出异常: com.itextpdf.text.DocumentException: Font 'STSong-Light' with 'UniGB-UCS2-H' is not recognized.
5、通过跟踪 BaseFont的源码定位到 ---> com.itextpdf.text.pdf.CJKFont.loadRegistry() ,该方法的配置文件没有加载到。
6、此刻心情:
7、很荣幸找到这位大神分享的: https://blog.csdn.net/lvqfly/article/details/50681539#commentBox , 添加一个新的maven依赖,然后问题解决。
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
五、总结
1、itext 经历了从: itext.itext ---> com.lowagie ---> com.itextpdf 的三次升级,若使用了旧版本的jar,就会出现错误,还不好发现。
2、多从maven repository 搜索下相关jar包,关注组件的历史变化情况。
3、会debeug,会追踪源码,找到抛出异常的根本原因所在!
参考资料: 觉得Eclipse 黑色主题不错