Recently, I need to write a tool to extract the table structure, similar to the table diagram in PowerDesigner, one of the steps used itext This third-party jar package to generate PDF files, encountered a problem, recorded here.
Problem Description:
The project is built using MAVEN, and the jar package is defined in Pom.xml as follows:
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext</artifactId>
<version>2.1.7</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
Execute Basefont bfchinesebfchinese = Basefont.createfont ("Stsong-light", "unigb-ucs2-h", basefont.not_embedded); Times wrong:
"Font ' stsong-light ' with ' unigb-ucs2-h ' isn't recognized."
Solve
1. First try to replace the Itext version of the package, his location in the Maven Central Library is/maven2/com/lowagie/itext/, the highest version is 4.2.2, but after trying to find the problem remains.
2. Note that the package path of the Itext-asian is com.itextpdf, so continue to find, found in/maven2/com/itextpdf/itextpdf itextpdf of the various versions, from 5.0.6 to 5.5.6.
3. Search for some posts, found itext from the 5.x version, renamed to Itextpdf, in line with the above (1) and (2) corresponding version number.
4. The problem can be determined, that is, the above Itext-asian and itext version does not correspond, instead:
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.4.3</version>
</dependency>
Run again, OK.
Some articles mention that the Itextasian package name is not updated will also cause this problem, but I use the 5.2.0 this package, unzip to see the package name is correct, perhaps the new version has been changed, this does not delve into.
Summary:
Find the CJKFont class and see from it
InputStream is = getResourceStream (RESOURCE_PATH + "cjkfonts.properties");
cjkFonts.load (is);
is.close ();
is = getResourceStream (RESOURCE_PATH + "cjkencodings.properties");
cjkEncodings.load (is);
is.close ();
Both his font and encoding files are loaded from String RESOURCE_PATH = "com / itextpdf / text / pdf / fonts /"; here, and the package name of the old itextasian.jar is com.lowagie.text.pdf.fonts, which should be The package name was renamed, but itextasian.jar was not updated in time, changed the itextasian.jar package path, re-run the program, OK, and it was done.
×××××××××××××××××××××××××××××××××××××××××××
The method of changing the iTextAsian.jar package name is as follows
1) Use winrar decompression program to decompress the original iTextAsian.jar, the directory structure is as follows
iTextAsian
--com
--lowagie
--text
--pdf
--fonts
--... (font property file)
2) Change the package name lowagie in the com directory after pressurization to itextpdf
3) Go to the iTextAsian directory on the command line and repackage it into the iTextAsian.jar file
The command is as follows:
jar cvf iTextAsian.jar com / itextpdf / text / pdf / fonts / *
After execution, add the new iTextAsian.jar to the classpath path
Reference:
http://endual.iteye.com/blog/1623327
http://blog.csdn.net/wang12/article/details/5661106
http://bbs.csdn.net/topics/390283201
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Itext Generate PDF file Error "Font ' stsong-light ' with ' unigb-ucs2-h ' is not recognized."